<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix" v-hasPermi="['platform:bcx:performance:report:fxy:list']">
        <div class="container">
          <el-form size="small" label-width="100px">
            <div class="form-item-container">
              <el-form-item label="时间选择：">
                <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="timeVal" value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd" size="small" type="daterange"
                  placement="bottom-end" placeholder="自定义时间"
                  style="width: 250px" @change="onchangeTime" />
              </el-form-item>
            </div>
            <div class="form-item-container">
              <el-form-item label="分销员名称">
                <el-input v-model.trim="searchInfo.name" placeholder="请输入姓名"
                  class="selWidth" size="small" clearable>
                  <el-button v-hasPermi="['platform:bcx:performance:report:fxy:list']"
                    slot="append" icon="el-icon-search" size="small" @click="searchList" />
                </el-input>
              </el-form-item>
              <el-form-item label="结算状态：">
                <el-select v-model="searchInfo.settleStatus" @change="searchList">
                  <el-option label="全部" :value="-1"></el-option>
                  <el-option label="待生效" :value="1"></el-option>
                  <el-option label="冻结中" :value="2"></el-option>
                  <el-option label="待结算" :value="3"></el-option>
                  <el-option label="结算中" :value="4"></el-option>
                  <el-option label="已结算" :value="5"></el-option>
                  <el-option label="无效" :value="6"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="订单号：">
                <el-input v-model.trim="searchInfo.orderNo" placeholder="请输入订单号"
                  class="selWidth1" size="small" clearable>
                  <el-button v-hasPermi="['platform:bcx:performance:report:fxy:list']"
                    slot="append" icon="el-icon-search" size="small" @click="searchList" />
                </el-input>
              </el-form-item>
            </div>
          </el-form>
        </div>
      </div>
      <div class="export-btn" @click="batchExport">批量导出</div>
      <div class="total-amount">总笔数：{{totalCount}}笔 | 总成交金额：{{totalPayPrice}}元 | 总佣金：{{totalBrokeragePrice}}元</div>
      <el-table v-loading="listLoading" :data="reportList"
        style="width: 100%" size="mini" class="table"
        highlight-current-row @selection-change="selectOptions">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="orderNo" label="订单ID" width="150" align="center" />
        <el-table-column prop="orderTime" label="订单时间" min-width="140" align="center" />
        <el-table-column prop="productName" label="商品名称" min-width="130" align="center" />
        <el-table-column prop="payPrice" label="成交金额" min-width="130" align="center" />
        <el-table-column prop="brokeragePrice" label="佣金" min-width="130" align="center" />
        <el-table-column label="分销员类型" min-width="80" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.brokerageUserType === 1 ? '企业' : '个人' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="brokerageUserRealName" label="分销员姓名" min-width="100" align="center" />
        <el-table-column prop="brokerageUserNikeName" label="分销员昵称" min-width="130" align="center" />
        <el-table-column prop="brokerageUserPhone" label="分销员手机号" min-width="130" align="center" />
        <el-table-column prop="brokerageUserOrg" label="分销员所属企业" min-width="130" align="center" />
        <el-table-column label="佣金类型" min-width="100" align="center">
          <template slot-scope="scope">
            <span>{{
              scope.row.brokerageType === 1 ? '比例' :
              scope.row.brokerageType === 2 ? '固定' :
              scope.row.brokerageType === 3 ? '自留' : ''
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="brokerageRatio" label="分佣规则" min-width="130" align="center" />
        <el-table-column prop="orderUserRealName" label="客户(购买人)" min-width="130" align="center" />
        <el-table-column label="结算状态" min-width="100" align="center">
          <template slot-scope="scope">
            <span>{{
              scope.row.settleStatus === 1 ? '待生效' :
              scope.row.settleStatus === 2 ? '冻结中' :
              scope.row.settleStatus === 3 ? '待结算' :
              scope.row.settleStatus === 4 ? '结算中' :
              scope.row.settleStatus === 5 ? '已结算' : '无效'
            }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination :page-sizes="[20, 40, 60, 80]" :page-size="limit"
          :current-page="page" layout="total, sizes, prev, pager, next, jumper"
          :total="total" @size-change="handleSizeChange" @current-change="pageChange" />
      </div>
    </el-card>
  </div>
</template>

<script>
  // +---------------------------------------------------------------------
  // | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
  // +---------------------------------------------------------------------
  // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  // +---------------------------------------------------------------------
  // | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
  // +---------------------------------------------------------------------
  // | Author: CRMEB Team <admin@crmeb.com>
  // +---------------------------------------------------------------------
  import { earningListApi, batchExportApi, reportStatisticsApi } from '@/api/distribution';
  import cardsData from '@/components/cards/index';
  import { checkPermi } from '@/utils/permission'; // 权限判断函数
  export default {
    name: 'distributionPeople',
    components: { cardsData },
    data() {
      return {
        listLoading: false,
        fromList: this.$constants.fromList,
        reportList: [],
        checkList: [],
        timeVal: [],
        searchInfo: {
          name: '',
          dateLimit: '',
          orderNo: '',
          settleStatus: null
        },
        page: 1,
        limit: 20,
        total: 0,
        totalBrokeragePrice: '',
        totalCount: '',
        totalPayPrice: ''
      };
    },
    mounted() {
      if (checkPermi(['platform:bcx:performance:report:fxy:list'])) {
        this.getList()
      }
    },
    methods: {
      checkPermi,
      getList() {
        this.listLoading = true;
        let params = {
          limit: this.limit,
          page: this.page,
          bcxId: this.bcxId,
          dateLimit: this.searchInfo.dateLimit,
          name: this.searchInfo.name,
          orderNo: this.searchInfo.orderNo,
          settleStatus: this.searchInfo.settleStatus
        };
        earningListApi(1, params).then((res) => {
          this.getStatistics()
          this.limit = res.limit;
          this.reportList = res.list;
          this.page = res.page;
          this.total = res.total;
          this.listLoading = false;
        }).catch(() => {
          this.listLoading = false;
        });
      },
      getStatistics() {
        let params = {
          searchType: 5,
          bcxId: this.bcxId,
          dateLimit: this.searchInfo.dateLimit,
          name: this.searchInfo.name,
          orderNo: this.searchInfo.orderNo,
          settleStatus: this.searchInfo.settleStatus
        };
        reportStatisticsApi(params).then((res) => {
          this.totalBrokeragePrice = res.totalBrokeragePrice
          this.totalCount = res.totalCount
          this.totalPayPrice = res.totalPayPrice
        })
      },
      searchList() {
        this.page = 1;
        this.getList();
      },
      // 具体日期
      onchangeTime(e) {
        this.timeVal = e;
        this.searchInfo.dateLimit = e ? this.timeVal.join(',') : '';
        this.getList();
      },
      batchExport() {
        let ids = [];
        if (this.checkList.length) {
          this.checkList.forEach(el => {
            ids.push(el.id);
          });
        }
        let params = {
          bcxId: this.bcxId ? this.bcxId : null,
          dateLimit: this.searchInfo.dateLimit,
          name: this.searchInfo.name,
          orderNo: this.searchInfo.orderNo,
          settleStatus: this.searchInfo.settleStatus
        };
        batchExportApi(1, params).then(res => {
          let fileName = decodeURIComponent(res.headers["content-disposition"].split("=")[1])
          let url = window.URL.createObjectURL(new Blob([res.data], {
            type: 'application/vnd.ms-excel'
          }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName) //对文件进行命名
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link); //下载完成移除元素
          window.URL.revokeObjectURL(url); //释放掉blob对象
        }).catch(() => {});
      },
      selectOptions(selection) {
        this.checkList = selection;
      },
      handleSizeChange(val) {
        this.limit = val;
        this.getList();
      },
      pageChange(page) {
        this.page = page;
        this.getList();
      },
    },
  };
</script>

<style scoped>
  .selWidth {
    width: 200px;
  }

  .selWidth1 {
    width: 350px;
  }

  .el-dropdown-link {
    cursor: pointer;
    color: #409eff;
    font-size: 12px;
  }

  .el-icon-arrow-down {
    font-size: 12px;
  }

  .form-item-container {
    display: flex;
    flex-direction: row;
    align-items: center;
  }

  .export-btn {
    width: 80px;
    height: 30px;
    border: 1px solid #CCCCCC;
    box-sizing: border-box;
    border-radius: 5px;
    font-size: 12px;
    text-align: center;
    line-height: 28px;
    margin-bottom: 20px;
  }

  .total-amount {
    margin-bottom: 20px;
    font-size: 16px;
  }
</style>
