<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" label-width="100px" :inline="true">
            <el-form-item label="账期：">
              <el-date-picker
                v-model="timeVal"
                value-format="yyyy-MM"
                format="yyyy-MM"
                size="small"
                type="monthrange"
                placement="bottom-end"
                start-placeholder="账单开始时间"
                end-placeholder="账单结束时间"
                style="width: 300px"
                @change="onchangeTime" />
            </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-select>
            </el-form-item>
            <el-form-item label="渠道商名称：">
              <el-input
                v-model.trim="searchInfo.name"
                placeholder="请输入渠道商名称"
                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>
        <div class="total-amount">总笔数：{{totalCount}}笔 | 总成交金额：{{totalPayPrice}}元 | 总佣金：{{totalBrokeragePrice}}元</div>
      </div>
      <el-table
        v-loading="listLoading"
        :data="billList"
        style="width: 100%"
        size="mini"
        class="table"
        highlight-current-row
        @selection-change="selectOptions">
        <el-table-column type="selection" width="55" :selectable="selectable" align="center"></el-table-column>
        <el-table-column prop="brokerageUserRealName" label="渠道商" min-width="180" align="center" />
        <el-table-column prop="billPeriod" label="账期" min-width="130" align="center" />
        <el-table-column prop="orderCount" label="订单数(笔)" min-width="100" align="center" />
        <el-table-column prop="orderTotalPrice" label="实付总金额(元)" min-width="130" align="center" />
        <el-table-column prop="brokerageTotalPrice" 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 ? '部分结算' : '已结算' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="账单生成时间" min-width="130" align="center" />
        <el-table-column label="操作" min-width="150" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="navigateToSettleDetail(scope.row.id)">账单详情</el-button>
          </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>
  import { channelPerformanceBillApi, billStatisticsApi } from '@/api/distribution';
  import cardsData from '@/components/cards/index';
  import { checkPermi } from '@/utils/permission'; // 权限判断函数

  export default {
    name: 'performanceChannelBill',
    components: { cardsData },
    data() {
      return {
        listLoading: false,
        timeVal: [],
        billList: [],
        checkList: [],
        searchInfo: {
          name: '',
          settleStatus: null,
          billPeriodStart: '',
          billPeriodEnd: ''
        },
        limit: 20,
        page: 1,
        total: 0,
        bcxId: null,
        totalBrokeragePrice: '',
        totalCount: '',
        totalPayPrice: ''
      };
    },
    mounted() {
      this.bcxId = this.$route.query.bcxId;
      this.getList();
    },
    methods: {
      checkPermi,
      getList() {
        this.listLoading = true;
        let params = {
          limit: this.limit,
          page: this.page,
          bcxId: this.bcxId ? this.bcxId : null,
          billPeriodStart: this.searchInfo.billPeriodStart ? this.searchInfo.billPeriodStart : null,
          billPeriodEnd: this.searchInfo.billPeriodEnd ? this.searchInfo.billPeriodEnd : null,
          settleStatus: this.searchInfo.settleStatus,
          name: this.searchInfo.name,
        };
        channelPerformanceBillApi(params).then(res => {
          this.getStatistics()
          this.limit = res.limit;
          this.billList = res.list;
          this.page = res.page;
          this.total = res.total;
          this.listLoading = false;
        }).catch(() => {
          this.listLoading = false;
        })
      },
      getStatistics() {
        let params = {
          searchType: 1,
          bcxId: this.bcxId,
          dateLimit: this.searchInfo.dateLimit,
          name: this.searchInfo.name,
          orderNo: this.searchInfo.orderNo,
          settleStatus: this.searchInfo.settleStatus
        };
        billStatisticsApi(params).then((res) => {
          this.totalBrokeragePrice = res.totalBrokeragePrice
          this.totalCount = res.totalCount
          this.totalPayPrice = res.totalPayPrice
        })
      },
      searchList() {
        this.page = 1;
        this.getList();
      },
      // 账单周期
      onchangeTime() {
        [this.searchInfo.billPeriodStart, this.searchInfo.billPeriodEnd] = this.timeVal
        this.getList();
      },
      selectOptions(selection) {
        this.checkList = selection;
      },
      selectable(row) {
        return this.billList.findIndex(item => row.settleStatus === 3);
      },
      navigateToSettleDetail(id) {
        this.$router.push({
          path: '/distribution/performanceBill/performanceBillDetail',
          query: {
            id,
            bcxId: this.bcxId,
            type: 3
          }
        });
      },
      handleSizeChange(val) {
        this.limit = val;
        this.page = 1;
        this.getList();
      },
      pageChange(page) {
        this.page = page;
        this.getList();
      },
    },
  };
</script>

<style lang="scss" scoped>
  .selWidth {
    width: 350px;
  }

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

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

  /* update  */
  .time-keyword-search {
    display: flex;
    flex-direction: row;
    align-items: center;
  }

  .btns {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-bottom: 20px;

    .batch-settle-btn {
      width: 100px;
      height: 30px;
      background-color: #409eff;
      border-radius: 5px;
      box-sizing: border-box;
      font-size: 12px;
      color: white;
      text-align: center;
      line-height: 30px;
    }

    .batch-export-btn {
      width: 100px;
      height: 30px;
      border-radius: 5px;
      border: 1px solid #CCCCCC;
      box-sizing: border-box;
      font-size: 12px;
      color: #333333;
      text-align: center;
      line-height: 30px;
      margin-left: 15px;
    }
  }

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