<template>
  <a-modal
    title="参照装箱单"
    v-model="packingListModalFabrics"
    :confirmLoading="confirmLoading"
    @ok="onSubmit"
    @cancel="handleCancel"
    width="86%"
    style="top:330px;left:100px;"
  >
    <!-- tabel 加载 -->
    <a-spin :spinning="confirmLoading">
      <!-- 查询  -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
              <a-form-item label="订单号">
                <a-input placeholder="请输入订单号" v-model="queryParam.orderNumber"></a-input>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="外销发票号">
                <a-input placeholder="请输入外销发票号" v-model="queryParam.exportInvoiceNo"></a-input>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="集装箱号">
                <a-input placeholder="请输入集装箱" v-model="queryParam.containerNumber"></a-input>
              </a-form-item>
            </a-col>
            <template v-if="toggleSearchStatus">
               <a-col :md="6" :sm="8">
                <a-form-model-item label="类型">
                  <a-select v-model="queryParam.type">
                    <a-select-option value="">请选择</a-select-option>
                    <a-select-option value="辅料">辅料</a-select-option>
                    <a-select-option value="面料">面料</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </template>

            <a-col :md="6" :sm="8">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                <a @click="handleToggleSearch" style="margin-left: 8px">
                  {{ toggleSearchStatus ? '收起' : '展开' }}
                  <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>


      <!-- table , y: 300 -->
      <div class="anotherTable">
        <a-table
          v-if="packingListData"
          :columns="packingListColumns"
          :data-source="packingListData"
          :loading="loading"
          :pagination="pagination"
          :row-key="record => record.itemId"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          @change="handleTableChange"
          bordered
          :scroll="{ x: 1500 }"
          size="small"
        >
        </a-table>
        <h3>共选择 {{selectedNumber}} 条</h3>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JEllipsis from '@/components/jeecg/JEllipsis'
import moment from 'moment'
import {PackingList} from '@api/document/book.js'

export default {
  name: 'PackingListModal', // 参照装箱单 弹框
  mixins: [JeecgListMixin],
  components: { JEllipsis, moment },
  data() {
    return {
      // 查询条件
      queryParam: {
        pageNo:''
      },
      packingListModalFabrics:false,
      selectedRowKeys: [], // 勾选航
      selectedRows:[],
      selectedNumber:0,//选中条数
      loading: false, // 表格加载
      fatherList:[],
      // 表头
      packingListColumns: [
        {
          title: '装箱单单据号',
          dataIndex: 'documentNo',
          width: 120,
          ellipsis: true,
          // fixed: 'left',
          className: 'replacecolor'
        },
        {
          title: '装箱单制单人',
          dataIndex: 'createBy',
          width: 120,
          ellipsis: true,
          // fixed: 'left',
          className: 'replacecolor'
        },
        //  {
        //   title: '创建时间',
        //   dataIndex: 'createTime',
        //   align: 'center',
        //   sorter: true,
        //   customRender: text => {
        //     return moment(text).format('YYYY-MM-DD')
        //   }
        // },
        {
          title: '外销发票号',
          dataIndex: 'exportInvoiceNo',
          width: 120,
          ellipsis: true,
          className: 'replacecolor'
        },
        {
          title: '集装箱号',
          dataIndex: 'containerNumber',
          width: 120,
          ellipsis: true,
          className: 'replacecolor'
        },
        {
          title: '成衣工厂',
          dataIndex: 'garmentFactory',
          width: 120,
          ellipsis: true,
          className: 'replacecolor'
        },

        {
          title: '供应商',
          dataIndex: 'supplierPrintingPlant',
          width: 120,
          ellipsis: true,
          className: 'replacecolor'
        },
        {
          title: '铅分号',
          dataIndex: 'plumbumNo',
          width: 120,
          ellipsis: true,
          className: 'replacecolor'
        },
        {
          title: '总毛重',
          dataIndex: 'grossWeight',
          width: 120,
          ellipsis: true,
          className: 'replacecolor'
        },
        {
          title: '总件数',
          dataIndex: 'total',
          width: 120,
          ellipsis: true,
          className: 'replacecolor'
        },
      ],
      packingListData: [],
      error:[],
      fatherList:[],
      errorFather:[],
      // orderDataform: this.$form.createForm(this),
      confirmLoading: false,
      packingListModalFabrics: false,
      pagination:{}
    }
  },
  // 接收父组件 方法
  props: {
    father: {
      type: Function,
      default: null
    }
  },
  created() {},
  methods: {
    getPackingList(){
      this.$nextTick(() => {
      PackingList(this.queryParam).then(res => {
          if (res.success) {
           this.packingListData = res.result.records       
           this.pagination = {
              total: res.result.total,
              current: res.result.current,
              pageSize: res.result.size
            }
          }else {
             this.$message.error(res.message);
          }
        })
        
      })
    },
    // 弹框查询按钮
    searchQuery() {
      this.toggleSearchStatus = false
      this.getPackingList()
      this.defulatSelect()
    },
    // 重置
    searchReset() {
      this.queryParam = {}
      this.queryParam.tailoringOrFabric = '1'
      this.getPackingList()
      this.defulatSelect()
      // this.getShipmentList()
    },
    // 弹框确定
    onSubmit() {

      console.log(this.selectedRows)
      this.dataSet()
      var selectedRow = this.selectedRows[0]
      if(this.fatherList.length !== 0){
        this.fatherDataSet(this.fatherList,selectedRow)
      }
      if(this.selectedRows.length === 0){
        this.$message.error('请选择数据！');
      }else if(this.selectedRows.length !==1 && this.error.length !==0){
        this.$message.error('所勾选数据的成衣工厂必须相同！');
        this.error = []
      }else if(this.fatherList.length !== 0 && this.errorFather.length !==0){
        var fatherCc = this.errorFather.toString()
        this.$message.error('所勾选的数据必须与已存在数据成衣工厂必须相同！');
        this.errorFather = []
      }else{
        this.$emit('callback',this.selectedRows,'面辅料')
        this.close()
      }
    },

     //勾选两条以上数据进行校验
    dataSet(){
       var garmentFactoryTest = []
       this.selectedRows.map(item=>{
         garmentFactoryTest.push(item.garmentFactory)
      })
      if([...new Set(garmentFactoryTest)].length !== 1){this.error.push('成衣')}
    },

    
    //父组件列表有值时，进行判断
    fatherDataSet(fathers,sons){
      var father = fathers[0]
      if(father.garmentFactory !== sons.garmentFactory){this.errorFather.push('成衣工厂')}
    },

    close() {
      this.$emit('close')
      this.packingListModalFabrics = false
      this.queryParam = {}
      this.packingListData =[]
      this.defulatSelect()
    },
    handleCancel() {
      this.close()
    },
    handleTableChange(pagination, filters, sorter) {
      this.queryParam.pageNo = pagination.current
      this.getPackingList()
    },
    // 选中行
    onSelectChange(keys, rows) {
      this.selectedRowKeys = keys
      this.selectedRows = rows
      this.selectedNumber = rows.length
    },
    //选中行恢复默认
    defulatSelect(){
      this.selectedRowKeys = [];
      this.selectedRows = [];
      this.selectedNumber = 0
    },
  },
  computed: {
    // 选中项
    rowSelection() {
      return {
        onChange: (selectedRowKeys, selectedRows) => {
          console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows)
        },
        getCheckboxProps: record => ({
          props: {
            disabled: record.title === 'Disabled User',
            // Column configuration not to be checked
            title: record.title
          }
        })
      }
    }
  }
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
@import '~@assets/less/overwriter.less';
/deep/ .ant-table-thead > tr > th {
  text-align: center;
  // font-weight: 700;
}

/deep/ .ant-table-tbody {
  text-align: center;
}

// /deep/ th.replacecolor {
//   background-color: #ccc;
// }
</style>
