<template>
  <div class="settle-detail-comp">
    <div class="btns">
        <div class="batch-settle-btn" @click="openSettleDialog" v-if="settleStatus == 1">确认结算</div>
        <div class="batch-settle-btn" style="margin-left: 10px;" @click="openUploadVoucherDialog" v-else>上传凭证</div>
        <div class="batch-export-btn" @click="batchExport">导出明细</div>
      </div>
      <el-table
        v-loading="listLoading"
        :data="billList"
        style="width: 100%"
        size="mini"
        class="table"
        highlight-current-row
      >
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column prop="orderNo" label="订单号" min-width="180" align="center" />
        <el-table-column prop="orderTime" label="订单时间" min-width="140" 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="130" align="center" v-if="type < 3">
          <template slot-scope="scope">
            <span>{{ scope.row.brokerageUserType === 1 ? '企业' : '个人' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="brokerageUserRealName" label="分销员姓名" min-width="130" align="center" v-if="type < 3" />
        <el-table-column prop="brokerageUserNikeName" label="分销员昵称" min-width="130" align="center" v-if="type < 3" />
        <el-table-column prop="brokerageUserPhone" label="分销员手机号" min-width="130" align="center" v-if="type < 3" />
        <el-table-column prop="brokerageUserOrg" label="所属企业" min-width="130" align="center" v-if="type < 3" />
        <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-column prop="orderTime" label="结算时间" min-width="140" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.settleTime ? scope.row.settleTime : '--/——' }}</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>
      <SettleDialog :showSettleDialog="showSettleDialog" :checkList="checkList" @closeSettleDialog="closeSettleDialog" />
      <UploadVoucherDialog :showUploadVoucherDialog="showUploadVoucherDialog" :settleId="settleId" @closeUploadVoucherDialog="closeUploadVoucherDialog" />
  </div>
</template>

<script>
import { fileImageApi } from '@/api/systemSetting';
import { batchExportApi } from '@/api/distribution';
import SettleDialog from './settleDialog.vue';
import UploadVoucherDialog from './uploadVoucherDialog.vue';

export default {
  props: ['id', 'settleStatus', 'listLoading', 'billList', 'total', 'page', 'limit', 'searchInfo', 'type'],
  components: { SettleDialog, UploadVoucherDialog },

  data() {
    return {
      showSettleDialog: false,
      showUploadVoucherDialog: false,
      settleId: null,
      checkList: [] // 用于结算
    };
  },

  methods: {
    openSettleDialog() {
      this.checkList.push({id: this.id})
      this.showSettleDialog = true;
    },

    closeSettleDialog() {
      this.showSettleDialog = false;
    },

    openUploadVoucherDialog() {
      this.settleId = this.id;
      this.showUploadVoucherDialog = true;
    },

    batchExport () {
      let params = {
        settleId: this.id
      };
      this.bcxId ? params.bcxId = this.bcxId : null
      this.searchInfo.brokerageUserPhone ? params.brokerageUserPhone = this.searchInfo.brokerageUserPhone : null;
      this.searchInfo.dateLimit ? params.dateLimit = this.searchInfo.dateLimit : null;
      this.searchInfo.name ? params.name = this.searchInfo.name : null;
      this.searchInfo.orderNo ? params.orderNo = this.searchInfo.orderNo : null;
      this.searchInfo.settleStatus > 0 ? params.settleStatus = this.searchInfo.settleStatus : null

      batchExportApi(this.type, 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(() => {});
    },

    uploadVoucher(param) {
      const formData = new FormData();
      const data = {
        model: 'user',
        pid: 0,
      };
      formData.append('multipart', param.file);
      this.uploadPic(formData, data);
    },

    uploadPic(formData, data) {
      let loading = this.$loading({
        lock: true,
        text: '上传中，请稍候...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)',
      });
      fileImageApi(formData, data)
        .then((res) => {
          loading.close();
          this.$message.success('上传成功');
          console.info('res', res);
          // this.tableData.page = 1;
          // this.getFileList();
          // this.onCallback();
        })
        .catch((res) => {
          loading.close();
        });
    },

    handleSizeChange(val) {
      this.$emit('handleSizeChange', val);
    },

    pageChange(page) {
      this.$emit('pageChange', page);
    },

    closeSettleDialog(type) {
      if (type) {
        this.$emit('updateList');
      }
      this.showSettleDialog = false;
    },

    closeUploadVoucherDialog() {
      this.showUploadVoucherDialog = false;
    }
  }
}
</script>

<style lang="scss" scoped>
.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;
  }
}

.table-item-btns {
  display: flex;
  flex-direction: row;
  align-items: center;
}
</style>
