<template>
  <a-modal
    title="参照订单数据"
    v-model="referOrderDataModVis"
    :confirmLoading="confirmLoading"
    @ok="onSubmit"
    @cancel="handleCancel"
    width="76%"
  >
    <!-- 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="账套">
                <j-search-select-tag
                  placeholder="请选择账套"
                  v-model="queryParam.account"
                  dict="view_account,account,account">
                </j-search-select-tag>
                <!-- <a-select placeholder="请选择账套" v-model="queryParam.account">
                  <a-select-option value="">请选择</a-select-option>
                  <a-select-option :value="0">香港森语(101)</a-select-option>
                </a-select> -->
              </a-form-item>
            </a-col>

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

      <!-- 勾选表  :row-key="record => record.id" { pageSize: 50 } , y: 800  -->
      <a-table
        bordered
        size="small"
        rowKey="id"
        v-if="orderListData"
        :columns="referOerderColumns"
        :data-source="orderListData"
        :loading="loading"
        :pagination="pagination"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange"
        :scroll="{ x: 1500, y:500 }"
      >
      </a-table>
    </a-spin>
  </a-modal>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JEllipsis from '@/components/jeecg/JEllipsis'
import moment from 'moment'
import { randomUUID } from '@/utils/util'

import { queryOrderData, confimOrderData } from '@api/document/shipmentList'

export default {
  name: 'ReferOrderDataModal', // 参照订单数据弹框
  mixins: [JeecgListMixin],
  components: { JEllipsis, moment },

  data() {
    let ellipsis = (v, l = 20) => <j-ellipsis value={v} length={l} /> // 省略

    return {
      selectedRowKeys: [],
      selectedRows: {}, // 勾选项
      loading: false, // 表格加载
      // 表头
      referOerderColumns: [
        {
          title: '订单号',
          dataIndex: 'orderNumber',
          width: 180,
          fixed: 'left',
          className: 'replacecolor'
        },
        {
          title: '款号',
          dataIndex: 'itemNumber',
          width: 150,
          fixed: 'left',
          className: 'replacecolor'
        },

        {
          title: '小po',
          dataIndex: 'smallPo',
          width: 100,
          className: 'replacecolor'
        },
        // 账套替换
        {
          title: '账套',
          dataIndex: 'account',
          width: 80,
          className: 'replacecolor'
        },
        {
          title: '订单日期',
          dataIndex: 'orderDate',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: 'packId',
          dataIndex: 'packId',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '分销点',
          dataIndex: 'distributionPoint',
          width: 120,
          className: 'replacecolor'
        },

        {
          title: '存货编码',
          dataIndex: 'inventoryCode',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '存货名称',
          dataIndex: 'inventoryName',
          customRender: t => ellipsis(t),
          width: 250,
          className: 'replacecolor'
        },
        {
          title: '颜色',
          dataIndex: 'colour',
          width: 160,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        {
          title: '尺码',
          dataIndex: 'size',
          width: 100,
          className: 'replacecolor'
        },
        {
          title: '配码规格',
          dataIndex: 'codingRules',
          width: 180,
          className: 'replacecolor'
        },
        {
          title: '箱数',
          dataIndex: 'boxNumber',
          width: 100,
          className: 'replacecolor'
        },
        {
          title: '数量',
          dataIndex: 'quantity',
          width: 100,
          className: 'replacecolor'
        },
        {
          title: '订单剩余数量',
          dataIndex: 'surplusNum',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '含税单价',
          dataIndex: 'unitPriceIncludingTax',
          width: 100,
          className: 'replacecolor'
        },
         {
          title: '成衣工厂',
          dataIndex: 'garmentFactory',
          width: 100,
          className: 'replacecolor'
        },
        {
          title: '成衣件数',
          dataIndex: 'garmentNmb',
          width: 100,
          className: 'replacecolor'
        },
        {
          title: '业务类型',
          dataIndex: 'businessTypeText',
          width: 100,
          className: 'replacecolor'
        },
        {
          title: '客户订单',
          dataIndex: 'customerOrderNumber',
          width: 100,
          className: 'replacecolor'
        },
        {
          title: '销售类型',
          dataIndex: 'salesTypeText',
          width: 100,
          className: 'replacecolor'
        },

        {
          title: '客户简称',
          dataIndex: 'customerAbbreviation',
          width: 220,
          align: 'left',
          className: 'replacecolor'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
           customRender: t => ellipsis(t),
          width: 220,
          className: 'replacecolor'
        },
         {
          title: '供应商编码',
          dataIndex: 'supplierCode',
          width: 100,
          className: 'replacecolor'
        },
        {
          title: '供应商',
          dataIndex: 'supplier',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '汇率',
          dataIndex: 'exchangeRate',
          width: 100,
          className: 'replacecolor'
        },
        {
          title: '整单合计',
          dataIndex: 'wholeOrderTotal',
          width: 100,
          className: 'replacecolor'
        },

        {
          title: '销售部门',
          dataIndex: 'salesDepartment',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '业务员',
          dataIndex: 'salesman',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '币种',
          dataIndex: 'currencyText',
          width: 100,
          className: 'replacecolor'
        },

        {
          title: '品牌方',
          dataIndex: 'brandSide',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '第三方',
          dataIndex: 'thirdParty',
          width: 220,
          align: 'left',
          className: 'replacecolor'
        },
        {
          title: '定金比例(%)',
          dataIndex: 'depositRatio',
          width: 100,
          className: 'replacecolor'
        },
        {
          title: '定金',
          dataIndex: 'deposit',
          width: 100,
          className: 'replacecolor'
        },
        {
          title: '协同路线',
          dataIndex: 'collaborativeRoute',
          width: 100,
          className: 'replacecolor'
        },
        {
          title: '付款条件',
          dataIndex: 'termOfPayment',
          width: 120,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        {
          title: '最终客户',
          dataIndex: 'endCustomer',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '订单备注',
          dataIndex: 'orderRemarks',
          width: 280,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        {
          title: '价格备注',
          dataIndex: 'priceRemarks',
          width: 280,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
        {
          title: '订单变更说明',
          dataIndex: 'orderChangeDescription',
          width: 220,
          customRender: t => ellipsis(t),
          className: 'replacecolor'
        },
         {
          title: '订单日期',
          dataIndex: 'orderDate',
          width: 120,
          className: 'replacecolor'
        },
        {
          title: '订单主表ID',
          dataIndex: 'syOrderDataId',
          width: 120,
          // fixed: 'right',
          className: 'replacecolor1'
        },
        {
          title: '订单子表ID',
          dataIndex: 'syOrderDataItemId',
          width: 450,
          // fixed: 'right',
          className: 'replacecolor'
        }
      ],
      orderListData: [], //订单数据
      confirmLoading: false,
      referOrderDataModVis: false,

      // 查询条件
      queryParam: {
        orderNumber: '', // 订单号
        account: ''
        // pageNo: '', // 初始页
        // pageSize: '-1' // 每页显示条数
      },
      // 分页
      pagination: {},
      confimList: [], //勾选的订单数组
      propList: [], //需要给新增页的数据
    }
  },
  // 接收父组件 方法
  // props: {},

  created() {
    this.getOrderList()
  },
  methods: {
    // 参照订单数据 查询
    getOrderList() {
      this.confirmLoading = true;
      this.$nextTick(() => {
        queryOrderData(this.queryParam).then(res => {
          this.confirmLoading = false;
          if (res.success) {
            for (var i=0; i<res.result.records.length;i++){
              res.result.records[i].id = randomUUID();
            }
            this.orderListData = res.result.records
            this.pagination = {
              total: res.result.total,
              current: res.result.current,
              pageSize: res.result.size
            }
          }
        })
      })
    },

    // 弹框确定
    onSubmit() {
      if (this.selectedRowKeys.length == 0) {
        this.$message.error('请勾选订单数据')
      } else {
        this.selectedRows.forEach(item => {
          this.confimList.push(item.syOrderDataItemId);
        })

        confimOrderData(this.confimList).then(res => {
          if (res.success) {
            this.propList = res.result;
            this.$emit('bao', this.propList);
          }
        })

        this.referOrderDataModVis = false;
        this.selectedRowKeys = [];
        this.selectedRows = [];
        this.searchReset();
      }
    },
    handleTableChange(pagination, filters, sorter) {
      this.queryParam.pageNo = pagination.current;
      this.getOrderList();
    },

    searchQuery() {
      this.queryParam.pageNo = '';
      this.getOrderList();
    },

    // 重置
    searchReset() {
      this.queryParam.orderNumber = '';
      this.queryParam.account = '';
      this.getOrderList();
    },
    // 选中行
    onSelectChange(keys, rows) {
      console.log(keys);
      console.log(rows);
      this.selectedRowKeys = keys;
      this.selectedRows = rows;
    },

    close() {
      this.$emit('close')
      this.referOrderDataModVis = false;
      this.selectedRowKeys = [];
      this.orderListData = [];
      this.searchReset();
    },
    handleCancel() {
      this.close();
    },
  },
  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';
/deep/ .ant-table-thead > tr > th {
  text-align: center;
  // font-weight: 700;
}
/deep/ .ant-table-tbody {
  text-align: center;
}
// /deep/ th.replacecolor1 {
// display: none;
// }
</style>
