<template>
  <div class="promoter-list">
    <el-dialog :title="titleName + '列表'" :visible.sync="showPromoterListDialog" width="900px" :before-close="handleClose">
      <div class="container">
        <el-form size="small" label-width="100px">
          <el-form-item key="1" label="时间选择：" class="width100" v-if="onName !== 'man'">
            <el-radio-group
              v-model="searchInfo.dateLimit"
              type="button"
              class="mr20"
              size="small"
              @change="searchList"
            >
              <el-radio-button v-for="(item, i) in fromList.fromTxt" :key="i" :label="item.val">{{
                item.text
              }}</el-radio-button>
            </el-radio-group>
            <el-date-picker
              v-model="timeValSpread"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              size="small"
              type="daterange"
              placement="bottom-end"
              placeholder="自定义时间"
              style="width: 250px"
              @change="onchangeTimeSpread"
            />
          </el-form-item>
          <el-form-item label="用户类型：">
            <el-radio-group v-model="searchInfo.type" size="small" @change="searchList">
              <el-radio-button label="0">全部</el-radio-button>
              <el-radio-button label="1">一级推广人</el-radio-button>
              <el-radio-button label="2">二级推广人</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="关键字：" class="width100">
            <el-input
              v-model.trim="searchInfo.keywords"
              :placeholder="onName === 'order' ? '请输入订单号' : '请输入姓名、电话、UID'"
              class="selWidth"
              size="small"
              clearable
            >
              <el-button slot="append" icon="el-icon-search" size="small" @click="searchList" />
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <el-table
        v-if="onName === 'man'"
        key="men"
        v-loading="spreadLoading"
        :data="spreadList"
        style="width: 100%"
        size="mini"
        class="table"
        highlight-current-row
      >
        <el-table-column prop="id" label="ID" width="60" />
        <el-table-column label="头像" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="nickname" label="用户信息" min-width="130" />
        <el-table-column prop="is_promoter" label="是否推广员" min-width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.isPromoter | filterYesOrNo }}</span>
          </template>
        </el-table-column>
        <el-table-column sortable label="推广人数" min-width="120" prop="spreadCount" />
        <el-table-column sortable label="订单数" min-width="120" prop="payCount" />
      </el-table>
      <el-table
        v-if="onName === 'order'"
        key="order"
        v-loading="spreadLoading"
        :data="spreadList"
        style="width: 100%"
        size="mini"
        class="table"
        highlight-current-row
      >
        <el-table-column prop="orderNo" label="订单ID" min-width="120" />
        <el-table-column label="用户信息" min-width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.nickname }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="时间" min-width="150" />
        <el-table-column sortable label="返佣金额" min-width="120" prop="price" />
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[10, 20, 30, 40]"
          :page-size="limit"
          :current-page="page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChangeSpread"
          @current-change="pageChangeSpread"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { spreadListApi, spreadOrderListApi } from '@/api/distribution';

export default {
  props: ['showPromoterListDialog', 'titleName', 'uid', 'onName'],

  data() {
    return {
      spreadLoading: false,
      timeValSpread: [],
      fromList: this.$constants.fromList,
      spreadList: [],
      searchInfo: {
        dateLimit: '',
        keywords: '',
        type: 0
      },
      total: 0,
      limit: 20,
      page: 1
    };
  },

  watch: {
    showPromoterListDialog(newVal) {
      if (!newVal) return;
      this.onName === 'man' ? this.getSpreadList() : this.getSpreadOrderList();
    }
  },

  methods: {
    getSpreadList() {
      this.spreadLoading = true;
      let params = {
        limit: this.limit,
        page: this.page,
        uid: this.uid,
        type: this.searchInfo.type
      }

      this.searchInfo.dateLimit ? params.dateLimit = this.searchInfo.dateLimit : null;
      this.searchInfo.keywords ? params.keywords = this.searchInfo.keywords : null;

      spreadListApi(params).then(res => {
        let { limit, page, total, totalPage, list } = res;
        
        this.limit = limit > 0 ? limit : this.limit;
        this.page = page;
        this.total = total;
        this.totalPage = totalPage;
        this.spreadList = list;
        this.spreadLoading = false;
        console.info('res', res);
      }).catch(() => {
        this.spreadLoading = false;
      });
    },

    getSpreadOrderList() {
      this.spreadLoading = true;
      let params = {
        limit: this.limit,
        page: this.page,
        uid: this.uid,
        type: this.searchInfo.type
      }

      this.searchInfo.dateLimit ? params.dateLimit = this.searchInfo.dateLimit : null;
      this.searchInfo.keywords ? params.keywords = this.searchInfo.keywords : null;

      spreadOrderListApi(params)
        .then((res) => {
          let { limit, page, total, totalPage, list } = res;
        
          this.limit = limit > 0 ? limit : this.limit;
          this.page = page;
          this.total = total;
          this.totalPage = totalPage;
          this.spreadList = list;
          this.spreadLoading = false;
        }).catch(() => {
          this.spreadLoading = false;
        });
    },

    handleClose() {
      this.$emit('closePromoterListDialog')
    },

    searchList() {
      this.page = 1;
      this.onName === 'man' ? this.getSpreadList() : this.getSpreadOrderList();
    },

    // 具体日期
    onchangeTimeSpread(e) {
      this.timeValSpread = e;
      this.searchInfo.dateLimit = e ? this.timeValSpread.join(',') : '';
      this.page = 1;
      this.searchList();
    },

    pageChangeSpread(page) {
      this.page = page;
      this.onName === 'man' ? this.getListSpread(this.uid) : this.getSpreadOrderList(this.uid);
    },

    handleSizeChangeSpread(val) {
      this.limit = val;
      this.onName === 'man' ? this.getListSpread(this.uid) : this.getSpreadOrderList(this.uid);
    },
  }
}
</script>
