<template>
  <a-modal
    title="参照装箱单"
    v-model="packingListModVis"
    :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.name"></a-input>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="款号">
                <a-input placeholder="请输入款号" v-model="queryParam.itemNumber"></a-input>
              </a-form-item>
            </a-col>

             <template v-if="toggleSearchStatus">
              <a-col :md="6" :sm="8">
              <a-form-item label="分销点">
                <a-input placeholder="请输入分销点" v-model="queryParam.distributionPoint"></a-input>
              </a-form-item>
             </a-col>
             <a-col :md="6" :sm="8">
              <a-form-item label="预发货日期">
                <a-date-picker placeholder="请输入预发货日期" style="width:100%;" v-model="queryParam.preDeliveryDate"/>
              </a-form-item>
             </a-col>
              <a-col :md="6" :sm="8">
              <a-form-item label="小po">
                <a-input placeholder="请输入小po" v-model="queryParam.smallPo"></a-input>
              </a-form-item>
             </a-col>
             <a-col :md="6" :sm="8">
              <a-form-item label="供应商">
                <a-input placeholder="请输入供应商" v-model="queryParam.supplier"></a-input>
              </a-form-item>
             </a-col>
             <a-col :md="6" :sm="8">
              <a-form-item label="预托书号">
                <a-input placeholder="请输入预托书号" v-model="queryParam.depositaryReceiptNo"></a-input>
              </a-form-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>
      </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() {
    let ellipsis = (v, l = 20) => <j-ellipsis value={v} length={l} /> // 省略
    return {
      // 查询条件
      queryParam: {
        pageNo:''
      },
      selectedRowKeys: [], // 勾选航
      loading: false, // 表格加载
      // 表头
      packingListColumns: [
         {
          title: '预托书号',
          dataIndex: 'depositaryReceiptNo',
          width: 120,
          ellipsis: true,
          // fixed: 'left',
          className: 'replacecolor'
        },
         {
          title: '托书号',
          dataIndex: 'shippingOrderNumber',
          width: 120,
         ellipsis: true,
          // fixed: 'left',
          className: 'replacecolor'
        }, 
       {
          title: '集装箱代号',
          dataIndex: 'containerCode',
          ellipsis: true,
          width: 120,
          className: 'replacecolor'
        },

        {
          title: '集装箱号',
          dataIndex: 'containerNumber',
          ellipsis: true,
          width: 120,
          // fixed: 'right',
          className: 'replacecolor'
        },
         {
          title: '单据号',
          dataIndex: 'documentNo',
          ellipsis: true,
          width: 120,
          className: 'replacecolor'
        },
          {
          title: '订单号',
          dataIndex: 'orderNumber',
          ellipsis: true,
          width: 120,
          // fixed: 'left',
          className: 'replacecolor'
        },
        {
          title: '款号',
          dataIndex: 'itemNumber',
          ellipsis: true,
          width: 120,
          // fixed: 'left',
          className: 'replacecolor'
        },
        {
          title: '客户简称',
          dataIndex: 'customerAbbreviation',
          ellipsis: true,
          width: 120,
          // fixed: 'left',
          className: 'replacecolor'
        },
        //  {
        //   title: '创建时间',
        //   dataIndex: 'createTime',
        //   align: 'center',
        //   sorter: true,
        //   customRender: text => {
        //     return moment(text).format('YYYY-MM-DD')
        //   }
        // },
        {
          title: '预发货日期',
          dataIndex: 'preDeliveryDate',
          ellipsis: true,
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '小PO',
          dataIndex: 'smallPo',
           ellipsis: true,
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '分销点',
          dataIndex: 'distributionPoint',
           ellipsis: true,
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '数量（按合并规则累计）',
          dataIndex: 'number',
           ellipsis: true,
          width: 180,
          className: 'replacecolor'
        },
        {
          title: '箱数',
          dataIndex: 'planQuantity',
           ellipsis: true,
          width: 90,
          className: 'replacecolor'
        },
        {
          title: '总净重',
          dataIndex: 'netWeight',
           ellipsis: true,
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '总毛重',
          dataIndex: 'grossWeight',
           ellipsis: true,
          width: 120,
          className: 'replacecolor'
        },

        {
          title: '总体积',
          dataIndex: 'totalVolume',
           ellipsis: true,
          width: 120,
          className: 'replacecolor'
        },

        // {
        //   title: '备注',
        //   dataIndex: 'memo',
        //   width: 120,
        //   className: 'replacecolor'
        // },
        // {
        //   title: '发货日期',
        //   dataIndex: 'shipDate',
        //   width: 120,
        //   className: 'replacecolor'
        // },
        // {
        //   title: '业务员',
        //   dataIndex: 'salesman',
        //   width: 120,
        //   className: 'replacecolor'
        // },
        // {
        //   title: '客户',
        //   dataIndex: 'customer',
        //   width: 120,
        //   className: 'replacecolor'
        // },
        // {
        //   title: '存货名称',
        //   dataIndex: 'inventoryName',
        //   width: 120,
        //   className: 'replacecolor'
        // },

        // {
        //   title: '颜色',
        //   dataIndex: 'colour',
        //   width: 120,
        //   className: 'replacecolor'
        // },
        {
          title: '采购/委外订单号',
          dataIndex: 'purOrSubOrder',
           ellipsis: true,
          width: 140,
          className: 'replacecolor'
        },
         {
          title: '成衣工厂',
          dataIndex: 'garmentFactory',
          width: 140,
           ellipsis: true,
          className: 'replacecolor'
        },
          {
          title: '外销发票号',
          dataIndex: 'exportInvoiceNo',
          width: 130,
           ellipsis: true,
          className: 'replacecolor'
        },
        // {
        //   title: '订单类型',
        //   dataIndex: 'orderType',
        //   width: 120,
        //   className: 'replacecolor'
        // },

        // {
        //   title: '工厂单价',
        //   dataIndex: 'factoryUnitPrice',
        //   width: 120,
        //   className: 'replacecolor'
        // },
        
        // {
        //   title: '总价',
        //   dataIndex: 'totalPrices',
        //   width: 120,
        //   className: 'replacecolor'
        // },
      
      ],
      packingListData: [],
      selectedRows:[],
      error:[],//两条以上数据，不同字段名

      // orderDataform: this.$form.createForm(this),
      confirmLoading: false,
      packingListModVis: false,
      fatherList: [],
      errorFather:[],
      pagination:{}
    }
  },
  // 接收父组件 方法
  props: {
    father: {
      type: Function,
      default: null
    }
  },
  created() {},
  methods: {
    getPackingList(){
      this.$nextTick(() => {
      PackingList(this.queryParam).then(res => {
          if (res.success) {
            res.result.records.map(item =>{
              var str = item.preDeliveryDate
              var n=str.split(" ");
              item.preDeliveryDate = n[0]
            })
           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()
    },
    // 重置
    searchReset() {
      this.queryParam = {}
      this.queryParam.tailoringOrFabric = '0'
      this.getPackingList()
    },
    // 弹框确定
    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){
        var cc =this.error.toString()
        this.$message.error('所勾选数据'+cc+'必须相同！');
        this.error = []
      }else if(this.fatherList.length !== 0 && this.errorFather.length !==0){
        var fatherCc = this.errorFather.toString()
        this.$message.error('所勾选数据与已存在数据'+fatherCc+'必须相同！');
        this.errorFather = []
      }else{
        this.$emit('callback',this.selectedRows)
        this.close()
      }
    },
      //勾选两条以上数据进行校验
    dataSet(){
       var distributionPointTest = [],
            customerTest = []
       this.selectedRows.map(item=>{
         distributionPointTest.push(item.distributionPoint)
         customerTest.push(item.customer)
      })
      if([...new Set(distributionPointTest)].length !== 1){this.error.push('分销点')}
      if([...new Set(customerTest)].length !== 1){this.error.push('客户')}
    },
    
    //父组件列表有值时，进行判断
    fatherDataSet(fathers,sons){
      var father = fathers[0]
      if(father.distributionPoint !== sons.distributionPoint){this.errorFather.push('分销点')}
      if(father.customer !== sons.customer){this.errorFather.push('客户')}
    },

    close() {
      this.$emit('close')
      this.packingListModVis = false
      this.queryParam = {}
      this.packingListData =[]
      this.selectedRowKeys = []
      this.selectedRows = []
    },
    handleCancel() {
      this.close()
    },
    handleTableChange(pagination, filters, sorter) {
      this.queryParam.pageNo = pagination.current
      this.getPackingList()
    },
    // 选中行
    onSelectChange(keys, rows) {
      this.selectedRowKeys = keys
      this.selectedRows = rows
    }
  },
  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>
