<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix" v-hasPermi="['platform:bcx:performance:report:department:member:list']">
        <div class="container">
          <el-form size="small" label-width="100px">
            <div class="time-keyword-search">
              <div>
                <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>
              </div>
              <div class="keyword-search">
                <el-form-item label="结算状态">
                  <el-select v-model="searchInfo.settleStatus" @change="seachList">
                    <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>
              </div>
            </div>
            <el-form-item :label="tabActive == 0 ? '部门名称：' : '成员姓名：'">
              <el-input v-model.trim="searchInfo.name" placeholder="请输入成员姓名"
                class="selWidth" size="small" clearable>
                <el-button v-hasPermi="['platform:bcx:performance:report:department:member:list']"
                  slot="append" icon="el-icon-search" size="small" @click="seachList" />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <tableComp :bcxId="bcxId" :billList="billList"
        :listLoading="listLoading" :tabActive="tabActive"
        :page="page" :total="total" :limit="limit"
        :totalBrokeragePrice="totalBrokeragePrice"
        :totalCount="totalCount" :totalPayPrice="totalPayPrice"
        :searchInfo="searchInfo" @updateList="getList"
        @handleSizeChange="handleSizeChange" @pageChange="pageChange" />
    </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 { departmentPerformanceBillApi, settleBillApi } from '@/api/distribution';
  import { checkPermi } from '@/utils/permission'; // 权限判断函数
  import cardsData from '@/components/cards/index';
  import tableComp from './component/tableComp'

  export default {
    name: 'performanceMemberBill',
    components: { cardsData, tableComp },
    data() {
      return {
        listLoading: false,
        tabActive: 1,
        timeVal: [],
        billList: [],
        searchInfo: {
          name: '',
          settleStatus: null,
          billPeriodStart: '',
          billPeriodEnd: ''
        },
        limit: 20,
        page: 1,
        total: 0,
        bcxId: null,
        totalBrokeragePrice: '',
        totalCount: '',
        totalPayPrice: ''
      };
    },
    mounted() {
      if (checkPermi(['platform:bcx:performance:report:department:member:list'])) {
        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,
        };
        departmentPerformanceBillApi(this.tabActive, params).then(res => {
          this.getStatistics()
          this.limit = res.limit;
          this.total = res.total;
          this.page = res.page;
          this.billList = res.list;
          this.listLoading = false;
        }).catch(() => {
          this.listLoading = false;
        })
      },
      getStatistics() {
        let params = {
          searchType: 3,
          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,
        };
        settleBillApi(params).then((res) => {
          this.totalBrokeragePrice = res.totalBrokeragePrice
          this.totalCount = res.totalCount
          this.totalPayPrice = res.totalPayPrice
        })
      },
      seachList() {
        this.page = 1;
        this.getList();
      },
      // 账单周期
      onchangeTime() {
        [this.searchInfo.billPeriodStart, this.searchInfo.billPeriodEnd] = this.timeVal
        this.getList();
      },
      changeTab() {
        this.getList();
      },
      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;
  }
</style>
