<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="75px"
    >
      <el-form-item label="门店名称" prop="siteName" label-width="90px">
        <el-input
          v-model="queryParams.siteName"
          placeholder="请输入门店名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="推广员姓名" prop="name" label-width="90px">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入推广员姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间">
        <el-date-picker @clear="handleQuery()" v-model="registeredTimeArr" type="datetimerange" size="small" value-format="timestamp" range-separator="至" start-placeholder="时间选择" end-placeholder="时间选择" @change="tardeTime">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @sort-change="sortChannelId">
      <!-- <el-table-column type="selection" width="55"  /> -->
      <el-table-column label="推广员编号" sortable="custom" prop="channelId" min-width="110px"/>
      <el-table-column label="推广员姓名" show-overflow-tooltip  prop="name" min-width="110px" >
        <template slot-scope="scope">
          <pre>{{scope.row.name}}</pre>
        </template>
      </el-table-column>
      <el-table-column label="手机号码"  prop="mobile" min-width="120px"/>
      <el-table-column label="佣金比例" sortable="custom" prop="commRate" min-width="100px">
        <template slot-scope="{ row, column }">
          <span>{{row.commRate}}%</span>
        </template>
      </el-table-column>
      <el-table-column label="门店名称" prop="siteName" min-width="100px" />
      <el-table-column label="盲票销量" sortable="custom" prop="saleNum" min-width="100px"/>
      <el-table-column label="销售额" sortable="custom" prop="saleAmt" min-width="100px">
        <template slot-scope="{ row }">
          <div>{{ $numberFormat(row.saleAmt) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="佣金" sortable="custom" prop="commAmt" min-width="100px">
        <template slot-scope="{ row }">
          <div>{{ $numberFormat(row.commAmt) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" prop="createdTime" min-width="160px">
        <template slot-scope="{ row }">
          {{ parseTime(row.createdTime) }}
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageParams.pageNum"
      :limit.sync="pageParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { promotersList } from "@/api/admin/promoters";
export default {
  name: "Promoters",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门店表格数据
      list: [],
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        endDate: "",
        startDate: "",
        name: "",
        mobile: "",
        siteName: ""
      },
      pageParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 't1.channelId',
        isAsc: 'desc',
      },
      // 注册时间
      registeredTimeArr: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询门店列表 */
    getList() {
      this.loading = true;
      promotersList('pageNum='+this.pageParams.pageNum + '&pageSize='+this.pageParams.pageSize+'&orderByColumn='+ this.pageParams.orderByColumn +'&isAsc='+ this.pageParams.isAsc +'&',this.queryParams).then(res => {
        this.loading = false;
        if(res.code === 0) {
          this.list = res.rows;
          this.total = res.total;
        }
      }).catch(res => {
        this.loading = false;
      });
    },

    // 切换排序
    sortChannelId(row){
      if (row){
        let prop =  row.prop == 'channelId' || row.prop == 'commRate' ? ('t1.'+ row.prop) : row.prop;
        this.pageParams.orderByColumn = prop
        this.pageParams.isAsc = row.order=='ascending'?"asc":"desc";
        this.getList()
      }
    },

    // 岗位状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 选择注册时间
    tardeTime(e) {
      if (e) {
        this.queryParams.startTime = e[0];
        this.queryParams.endTime = e[1];
        this.handleQuery();
      } else {
        this.queryParams.startTime = "";
        this.queryParams.endTime = "";
        this.handleQuery();
      }
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.registeredTimeArr = [];
      this.queryParams= {
        endDate: "",
        startDate: "",
        name: "",
        mobile: "",
        siteName: ""
      },
        this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</script>
