<template>
  <div class="settle-detail-comp">
    <div class="btns">
        <div class="batch-settle-btn" @click="openSettleDialog">生成结算单</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
        @selection-change="changeOptions"
      >
        <el-table-column type="selection" width="55" 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" :rowId="rowId" :id="id" :checkList="checkList" from="detail" @closeSettleDialog="closeSettleDialog" />
  </div>
</template>

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

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

  data() {
    return {
      showSettleDialog: false,
      rowId: null,
      checkList: []
    };
  },

  methods: {
    openSettleDialog() {
      if (!this.checkList.length) {
        this.$message({
          message: '至少选择一条记录',
          type: 'warning',
          showClose: true
        });

        return;
      }

      if (this.checkList.filter(el => el.settleStatus === 4).length > 0) {
        this.$message({
          message: '有结算中的记录，请重新选择',
          type: 'warning',
          showClose: true
        });

        return;
      }

      this.showSettleDialog = true;
    },

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

    batchExport () {
      let params = {
        settleBillId: this.id
      };
      this.bcxId ? params.bcxId = this.bcxId : null
      this.searchInfo.billPeriodStart ? params.billPeriodStart = this.searchInfo.billPeriodStart : null;
      this.searchInfo.billPeriodEnd ? params.billPeriodEnd = this.searchInfo.billPeriodEnd : null;
      this.searchInfo.name ? params.name = this.searchInfo.name : 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(() => {});
    },

    changeOptions(selection) {
      this.checkList = selection;
    },

    down(type) {
      // event.preventDefault()
      var aLink = document.createElement('a');
      aLink.download = 'author-img';
      // 输出
      if (type === 'blob') {
        this.$refs.cropper.getCropBlob((data) => {
          this.downImg = window.URL.createObjectURL(data);
          // aLink.download = this.downImg;
          aLink.href = window.URL.createObjectURL(data);
          aLink.click();
        });
      } else {
        this.$refs.cropper.getCropData((data) => {
          this.downImg = data;
          aLink.href = data;
          aLink.click();
        });
      }
    },

    confirmSettle(rowId) {
      this.rowId = rowId;
      this.showSettleDialog = true;
    },

    uploadVoucher(param) {
      // this.$refs.cropper.getCropData((data) => {
      //   let name = new Date().getTime();
      //   let file = this.dataURLtoFile(data, `${name}.png`);
      //   const datas = {
      //     model: this.modelName ? this.modelName : 'product',
      //     pid: this.tableData.pid ? this.tableData.pid : 0,
      //   };
      //   let formData = new FormData();
      //   formData.append('multipart', file);
      //   this.uploadPic(formData, datas);
      //   // this.onCallback();
      // });
      console.info('result', 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);
    }
  }
}
</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>
