<template>
  <div>
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="handleExportXls('比对')">导出</a-button>
    </div>
    <a-table
      ref="table"
      size="middle"
      :scroll="{x:true}"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      class="j-table-force-nowrap"
      @change="handleTableChange">
    </a-table>
  </div>
</template>
<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getDbClList } from '@/api/kzksApi.js'
import { downFile } from '@/api/manage'

var columns = [
  {
    title: '序号',
    dataIndex: '',
    key:'rowIndex',
    width:60,
    align:"center",
    customRender:function (t,r,index) {
      return parseInt(index)+1;
    },
    // fixed:"left",
  },
  {
    title: '规格型号',
    dataIndex: 'ggxh',
    width: 100,
    align: 'center',
    // fixed:"left",
  },
  {
    title: '物料编码',
    dataIndex: 'wlbm',
    width: 100,
    align: 'center',
  },
  {
    title: '物料名称',
    dataIndex: 'wlmc',
    width: 100,
    align: 'center'
  },
]

export default {
  mixins:[JeecgListMixin, mixinDevice],
  // props: {
  //   // 对比表格数据
  //   // duibiclList: {
  //   //   type: Array,
  //   //   default: ()=>{},
  //   //   required: false
  //   // }
  //   chooseRowType: {
  //     type: Number,
  //     default: 1
  //   },
  //   duibiTasknos: {
  //     type: String,
  //     default: ''
  //   }
  // },
  props: ['chooseRowType', 'duibiTasknos'],
  data() {
    return {
      name: '',
      dataSource: [],
      // 表头
      columns: [],
      url: {
        exportXlsUrl: "/wzOutboundOrder/wzOutboundOrderH/compareCLFDetailListExportExcel",
      },
    };
  },
  created(){
  },
  methods: {
    // 数据处理
    loadData(arg){
      // console.log(this.duibiTasknos)
      this.dataSource = []
      var params = {
        tasknos: this.duibiTasknos,
        pageNo: this.ipagination.current,
        pageSize: this.ipagination.pageSize,
      }
      getDbClList(params).then((res) => {
        // console.log(res)
        // console.log(this.columns)
        if(res.success){
          // 分页
          if(res.result.total) {
            this.ipagination.total = res.result.total;
          } else {
            this.ipagination.total = 0;
          }
          // 处理数据
          var arrList = res.result.records
          // 头部
          this.columns = [...columns]
          arrList[0].taskVoList.forEach(item => {
            this.columns.push({
              title: item.taskno,
              // dataIndex: Object.keys(item)[1],
              children: [
                {
                  title: '是否含有',
                  align: 'center',
                  width: 80,
                  dataIndex: 'flag_' + item.taskno,
                },
                {
                  title: '实发总数量',
                  align: 'center',
                  width: 90,
                  dataIndex: 'totalCount_' + item.taskno,
                  customCell: this.setNumCustomCell,
                },
                {
                  title: '归一化单价(元)',
                  align: 'center',
                  width: 110,
                  dataIndex: 'unitPrice_' + item.taskno,
                  customCell: this.setPriceCustomCell,
                }
              ]
            })
          });
          // 表格数据处理
          arrList.forEach(item=>{
            // console.log(item)
            var objItem = {}
            objItem.id = item.id
            objItem.ggxh = item.ggxh
            objItem.wlbm = item.wlbm
            objItem.wlmc = item.wlmc
            objItem.amountEqual = item.amountEqual
            objItem.countEqual = item.countEqual
            item.taskVoList.forEach(ele=>{
              // console.log(ele)
              objItem['flag_'+ele.taskno] = ele.flag==='yes'?'√':'×'
              objItem['totalCount_'+ele.taskno] = ele.totalCount?ele.totalCount:'-'
              objItem['unitPrice_'+ele.taskno] = ele.unitPrice?ele.unitPrice:'-'
            })
            // console.log(objItem)
            this.dataSource.push(objItem)
          })
          // console.log(this.dataSource)
        }else{
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.loading = false
      })
    },
    setPriceCustomCell(record, index){
      // console.log(record, index)
      // var arr = []
      // for (const item in record) {
      //   if(item.includes('unitPrice')){
      //     console.log(record[item])
      //     arr.push(record[item])
      //   }
      // }
      // console.log(arr)
      if(!record.amountEqual){
        return {
          style: {
            'color': '#ff1832',
            // 'color': '#1890ff',
          },
        }
      }
    },
    setNumCustomCell(record, index){
      console.log(record)
      if(!record.countEqual){
        return {
          style: {
            'color': '#ff1832',
            // 'color': '#1890ff',
          },
        }
      }
    },
    handleExportXls(fileName){
      if(!fileName || typeof fileName != "string"){
        fileName = "导出文件"
      }
      let param = {};
      // let param = this.getQueryParams();
      // if(this.selectedRowKeys && this.selectedRowKeys.length>0){
      //   param['selections'] = this.selectedRowKeys.join(",")
      // }
      param['tasknos'] = this.duibiTasknos
      console.log("导出参数",param)
      downFile(this.url.exportXlsUrl,param).then((data)=>{
        if (!data) {
          this.$message.warning("文件下载失败")
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName+'.xls')
        }else{
          let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName+'.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link); //下载完成移除元素
          window.URL.revokeObjectURL(url); //释放掉blob对象
        }
      })
    },
  }
};
</script>
<!-- <style >
::v-deep .a-table__fixed-right { height: 100% !important; }
</style> -->