<template>
  <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'

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: []
    };
  },
  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: 100,
                  dataIndex: 'flag_' + item.taskno,
                },
                {
                  title: '实发总数量',
                  align: 'center',
                  dataIndex: 'totalCount_' + item.taskno,
                },
                {
                  title: '归一化单价(元)',
                  align: 'center',
                  dataIndex: 'unitPrice_' + item.taskno,
                  customCell: this.setCustomCell,
                }
              ]
            })
          });
          // 表格数据处理
          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.equal = item.equal
            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
      })
    },
    // handleTableChange(pagination, filters, sorter) {
    //   //分页、排序、筛选变化时触发
    //   //TODO 筛选
    //   console.log(pagination)
    //   if (Object.keys(sorter).length > 0) {
    //     this.isorter.column = sorter.field;
    //     this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
    //   }
    //   this.ipagination = pagination;
    //   this.loadData(2);
    // },
    setCustomCell(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.equal){
        return {
          style: {
            'color': '#ff1832',
            // 'color': '#1890ff',
          },
        }
      }
    },
  }
};
</script>