<template>
  <a-modal
    title="参照预装箱单"
    v-model="advancePackingListModVis"
    :confirmLoading="confirmLoading"
    @ok="onSubmit"
    @cancel="handleCancel"
    width="86%"
    style="top:330px;left:100px;"
    destroyOnClose
  >
    <!-- 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="客户简称">
                  <j-search-select-tag
                    placeholder="请选择客户简称"
                    v-model="queryParam.customerAbbreviation"
                    dict="view_customer,customername,customername">
                  </j-search-select-tag>
                </a-form-item>
              </a-col>
              <!-- <a-col :md="6" :sm="8">
              <a-form-item label="预发货日期">
                <a-date-picker placeholder="请选择预发货日期" v-model="queryParam.preDeliveryDate" format="YYYY-MM-DD"
                @change="deliveryDateChange"></a-date-picker>
              </a-form-item>
            </a-col> -->
            <a-col :md="6" :sm="8">
              <a-form-item label="预发货日期">
                 <a-range-picker
                  :placeholder="['开始时间', '结束时间']"
                  format="YYYY-MM-DD"
                  style="width: 100%"
                  v-model="preDeliveryDate"
                  @change="deliveryDateChange"
                />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="分销点">
                <!-- <a-input placeholder="请输入分销点" v-model="queryParam.distributionPoint"></a-input> -->
                 <j-search-select-tag
                  placeholder="请选择分销点"
                  v-model="queryParam.distributionPoint"
                  dict="view_distributionpoint,distributionpoint,distributionpoint">
                </j-search-select-tag>
              </a-form-item>
            </a-col>
            
            <template v-if="toggleSearchStatus">
              <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.itemNumber"></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
          :columns="advancePackingListColumns"
          :data-source="advancePackingListData"
          :loading="loading"
          :pagination="pagination"
          row-key="itemId"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          @change="handleTableChange"
          bordered
          :scroll="{ x: 2800,y:500 }"
          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 { querySyPreAssembledPackingList } from '@api/document/pre-book.js'

export default {
  name: 'AdvancePackingListModal', // 参照预装箱单 弹框
  mixins: [JeecgListMixin],
  components: { JEllipsis, moment },
  data() {
    let ellipsis = (v, l = 20) => <j-ellipsis value={v} length={l} /> // 省略
    return {
      fatherData:[], //接受父组件的表格数据
      // 查询条件
      queryParam: {
        itemNumber:'',
        customerAbbreviation:'',
        smallPo:'',
        distributionPoint:''
      },
      preDeliveryDate:[],
      selectedRowKeys: [], // 勾选航
      selectedRows: [],
      selectedNumber:[],//勾选条数
      loading: false, // 表格加载
      // 表头
      advancePackingListColumns: [
        {
          title: '款号',
          dataIndex: 'itemNumber',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '客户简称',
          dataIndex: 'customerAbbreviation',
          width: 120,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        {
          title: '预发货日期',
          dataIndex: 'preDeliveryDate',
          width: 120,
          className: 'replacecolor'
        },
        // {
        //   title: '预装箱单单据号',
        //   dataIndex: 'documentNo',
        //   width: 120,
        //   className: 'replacecolor'
        // },
        {
          title: '小PO',
          dataIndex: 'smallPo',
          width: 120,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        //  {
        //   title: '创建时间',
        //   dataIndex: 'createTime',
        //   align: 'center',
        //   sorter: true,
        //   customRender: text => {
        //     return moment(text).format('YYYY-MM-DD')
        //   }
        // },
        
        {
          title: '分销点',
          dataIndex: 'distributionPoint',
          width: 120,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },

        // {
        //   title: '备注',
        //   dataIndex: 'memo',
        //   width: 150,
        //   customRender: t => ellipsis(t),
        //   className: 'replacecolor'
        // },
        {
          title: '客户',
          dataIndex: 'customer',
          width: 250,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        {
          title: '存货名称',
          dataIndex: 'inventoryName',
          width: 200,
          ellipsis: true,
          className: 'replacecolor'
        },
        {
          title: '颜色',
          dataIndex: 'colour',
          width: 120,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        // {
        //   title: '采购/委外订单号',
        //   dataIndex: 'spurOrSubOrder',
        //   width: 200,
        //   customRender: t => ellipsis(t),
        //   className: 'replacecolor'
        // },


        // {
        //   title: '工厂单价',
        //   dataIndex: 'factoryUnitPrice',
        //   width: 120,
        //   className: 'replacecolor'
        // },
        
        {
          title: '箱数',
          dataIndex: 'boxNumber',
          width: 90,
          className: 'replacecolor'
        },
        {
          title: '数量（按合并规则累计）',
          dataIndex: 'totalQuantity',
          width: 200,
          className: 'replacecolor'
        },
        // {
        //   title: '订单类型',
        //   dataIndex: 'orderType',
        //   width: 120,
        //   className: 'replacecolor'
        // },
        // {
        //   title: '集装箱代号',
        //   dataIndex: 'containerCode',
        //   width: 120,
        //   className: 'replacecolor'
        // },

        // {
        //   title: '集装箱号',
        //   dataIndex: 'containerNumber',
        //   width: 120,
        //   className: 'replacecolor'
        // },
        {
          title: '总净重',
          dataIndex: 'totalNetWeight',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '总毛重',
          dataIndex: 'totalGrossWeight',
          width: 120,
          className: 'replacecolor'
        },

        {
          title: '总体积',
          dataIndex: 'totalVolume',
          width: 120,
          className: 'replacecolor'
        },
        // {
        //   title: '总价',
        //   dataIndex: 'totalPrice',
        //   width: 120,
        //   className: 'replacecolor'
        // },
      ],
      advancePackingListData: [],
      error:[], // 选中数据不符合的字段名
      errorFather:[], //选中数据中与父组件表格数据不符合字段名集合

      // orderDataform: this.$form.createForm(this),
      confirmLoading: false,
      advancePackingListModVis: false,
      pagination:{}
    }
  },
  // 接收父组件 方法
  props: {
    father: {
      type: Function,
      default: null
    }
  },
  created() {
  },
  methods: {
    // 弹框查询按钮
    searchQuery() {
      this.toggleSearchStatus = false
      this.$nextTick(() => {
         this.queryParam.pageSize = 100
         this.confirmLoading = true
        //if (this.queryParam.preDeliveryDate != undefined && this.queryParam.preDeliveryDate != "")
        querySyPreAssembledPackingList(this.queryParam).then(res => {
         this.confirmLoading = false
          if (res.success) {
            this.advancePackingListData = res.result.records;
            this.pagination = {
              total: res.result.total,
              current: res.result.current,
              pageSize: res.result.size
            }
          }
        })
      this.defulatSelect()
      })
    },
    // 重置
    searchReset() {
      this.queryParam = {
        itemNumber:'',
        customerAbbreviation:'',
        smallPo:'',
        distributionPoint:''
      }
      this.preDeliveryDate = []
      this.searchQuery()
      this.defulatSelect()
      // this.getShipmentList()
    },
    // 弹框确定
    onSubmit() {
      this.dataSet( this.selectedRows)
      var selectedRow = this.selectedRows[0]
      if(this.fatherData.length !== 0){
        this.fatherDataSet(this.fatherData,selectedRow)
      }
      if (this.selectedRowKeys.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.fatherData.length !== 0 && this.errorFather.length !==0){
        var fatherCc = this.errorFather.toString()
        this.$message.error(fatherCc+'必须相同！');
        this.errorFather = []
      }else{
        var filterDataList = this.advancePackingListData.filter(item=>{
          return this.selectedRowKeys.filter(key=>{
            return item.itemId === key 
          }).length>0
        });
        this.$emit('callback', filterDataList);
        this.filterDataList = []
        this.close()
        // this.advancePackingListModVis = false;
        // this.selectedRowKeys = [];
      }
    },
    
    //勾选两条以上数据进行校验
    dataSet(){
       var distributionPointTest = [],
       customerTest = []
       this.selectedRows.map(item=>{
         distributionPointTest.push(item.distributionPoint)
         customerTest.push(item.customerAbbreviation)
      })
      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.clientAbbreviation !== sons.customerAbbreviation){this.errorFather.push('客户简称')}
    },


    close() {
      this.$emit('close')
      this.advancePackingListModVis = false
      this.error = []
      this.errorFather = []
      this.preDeliveryDate = []
      this.defulatSelect(),
      this.fatherData=[]
    },
    handleCancel() {
      this.close()
    },
     handleTableChange(pagination, filters, sorter) {
      this.queryParam.pageNo = pagination.current
      this.searchQuery()
    },
    // 选中行
    onSelectChange(keys, rows) {
      this.selectedRowKeys = keys;
      this.selectedRows = rows
      this.selectedNumber = rows.length
    },
      //选中行恢复默认
    defulatSelect(){
      this.selectedRowKeys = [];
      this.selectedRows = [];
      this.selectedNumber = 0
    },
    deliveryDateChange(value, dateString) {
      this.queryParam.beginTime = dateString[0]
      this.queryParam.endTime = dateString[1]
    },

  },
  computed: {
  }
}
</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>
