<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">
          <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" />
        <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.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-column label="结算凭证" min-width="140" align="center">
          <template slot-scope="scope">
            <span style="color:#409eff;">{{ scope.row.settleBillAttachmentsId ? '下载' : '—/—' }}</span>
          </template>
        </el-table-column> -->
        <!-- <el-table-column label="操作" min-width="150" fixed="right" align="center">
          <template slot-scope="scope">
            <div class="table-item-btns">
            <el-button
              type="text"
              size="small"
              class="mr5"
              v-hasPermi="['platform:retail:store:sub:user:list']"
              @click="confirmSettle(scope.row.id)"
              >确认结算</el-button
            >
            <span class="mr5">|</span>
            <el-upload
              action
              class="upload-demo"
              :limit="1" 
              :http-request="uploadVoucher">
              <el-button size="small" type="text">点击上传</el-button>
            </el-upload>
            </div>
          </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" @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;
      }
      this.showSettleDialog = true;
    },

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

    batchExport () {
      let params = {};
      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 => {
        const blob = new Blob([res], {
          type: 'application/vnd.ms-excel;charset=utf-8'
        });
        const a = document.createElement('a');
        const href = window.URL.createObjectURL(blob);
        a.href = href;
        a.download = localStorage.getItem('filename');
        localStorage.removeItem('filename');
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        window.URL.revokeObjectURL(href);
      }).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>