import API from '@/views/kzyWMS/bound/api/default'
import {
  genWarehouseAuthSearchModal,
  genCargoownerAuthSearchModal,
} from '@/views/kzyWMS/SearchModal'

const items = function(self) {
  let grid = {
    span: 8
  }
  return [
    { type: 'input', name: 'dcId', label: '仓配中心ID', hidden: true },
    { type: 'input', name: 'warehouseId', label: '仓库ID', hidden: true },
    ...genWarehouseAuthSearchModal(
      1,
      true,
      false,
      null,
      (modalRowData, form) => {
        console.log(modalRowData, form, 'modalRowData,formmodalRowData,form')
        if (modalRowData) {
          self.dcId = modalRowData[0].dcId
          self.remoteParams.warehouseId = modalRowData[0].warehouseId ? modalRowData[0].warehouseId : modalRowData[0].companyId
        } else {
          self.dcId = ''
          self.remoteParams.warehouseId = ''
        }
      },
      grid
    ),
    ...genCargoownerAuthSearchModal(1, true, false, null, null, grid),
    {
      type: 'select',
      name: 'rejectHandleWay',
      label: '物流退货处理方法',
      value: '',
      autoLoad: true,
      remoteUrl: API.remoteUrlFun('rejectHandleWay'),
      replaceFields: {
        label: 'dictName',
        value: 'dictCode'
      },
    },
    { type: 'input', name: 'srcCode', label: '单据编号', value: '' },
    { type: 'input', name: 'shipperCode', label: '单位编号', value: '' },
    { type: 'input', name: 'startDate', label: '开始日期', value: '',hidden:true },
    { type: 'input', name: 'endDate', label: '结束日期', value: '',hidden:true  },
    {
      type: 'a-range-picker',
      name: "adata",
      label: "日期",
      value: [],
      dropdownClassName: 'dropdownClassName1',
      ...grid,
      placeholder: ['起始日期','结束日期'],
      format: "YYYY-MM-DD",
      on: {
        change(date, dateString) {
          self.queryRefFromName('main').form.setFieldsValue({
            startDate: dateString[0],
            endDate: dateString[1]
          })
        },
      }
    },
  ]
}
const columns = function (self) {
  return [
    { dataIndex: 'billDate', title: '日期', key: 'billDate', width: 100 },
    { dataIndex: 'consignorCode', title: '货主编号', key: 'consignorCode', width: 80 },
    { dataIndex: 'consignorName', title: '货主名称', key: 'consignorName', width: 80 },
    { dataIndex: 'shipperId', title: '单位编号', key: 'shipperId', width: 80 },
    { dataIndex: 'shipperName', title: '单位名称', key: 'shipperName', width: 80 },
    { dataIndex: 'salespersonName', title: '业务员', key: 'salespersonName', width: 80 },
    { dataIndex: 'srcCode', title: '单据编号', key: 'srcCode', width: 80 },
    { dataIndex: 'remark', title: '备注', key: 'remark', width: 80 },
    { dataIndex: 'rejectHandleTypeName', title: 'ERP退货处理方法', key: 'rejectHandleTypeName', width: 80 },
    {
      type: 'select',
      dataIndex: 'rejectHandleWay',
      title: '物流退货处理方法',
      autoLoad:true,
      remoteUrl: API.remoteUrlFun('rejectHandleWay'),
      required:true,
      replaceFields: {
        label: 'dictName',
        value: 'dictCode'
      },
    },
    { dataIndex: 'receiveWayName', title: '提货方式', key: 'receiveWayName', width: 100 },
  ]
}

const detailColumns = function (self) {
  return [
    { dataIndex: 'itemCode', title: '商品编号', key: 'itemCode', width: 100 },
    { dataIndex: 'itemName', title: '商品名称', key: 'itemName', width: 80 },
    { dataIndex: 'itemSpecs', title: '商品规格', key: 'itemSpecs', width: 80 },
    { dataIndex: 'conversionRate', title: '包装数', key: 'conversionRate', width: 80 },
    { dataIndex: 'pieceQty', title: '数量', key: 'pieceQty', width: 80 },
    { dataIndex: 'totalPcs', title: '件数', key: 'totalPcs', width: 80 },
    { dataIndex: 'oddPieceQty', title: '零散数', key: 'oddPieceQty', width: 80 },
    { dataIndex: 'manufacturerLotNum', title: '批号', key: 'manufacturerLotNum', width: 80 },
    { dataIndex: 'batchProductionDateShow', title: '生产日期', key: 'batchProductionDateShow', width: 80 },
    { dataIndex: 'batchExpiredDateShow', title: '有效期至', key: 'batchExpiredDateShow', width: 80 },
    { dataIndex: 'rejectHandleTypeName', title: 'ERP退货处理方法', key: 'rejectHandleTypeName', width: 80 },
    {
      type: 'select',
      dataIndex: 'rejectHandleWay',
      title: '物流退货处理方法',
      autoLoad:true,
      remoteUrl: API.remoteUrlFun('rejectHandleWay'),
      required:true,
      replaceFields: {
        label: 'dictName',
        value: 'dictCode'
      },
    },
  ]
}

export function detailConfig(self) {
  return [
    {
      key: 'one',
      checkedKey: 'main',
      btns: [
        {
          label: '查询',
          click() {
            let refMain = self.queryRefFromName('main')
            let formsVal = refMain.form.getFieldsValue()
            if (!formsVal.warehouseId) return self.$message.warn('请选择仓库')
            if (!formsVal.cargoOwnerId) return self.$message.warn('请选择货主')
            self.queryDetailList()
          }
        },
        {
          label: '重置',
          click() {
            let refMain = self.queryRefFromName('main')
            let ref = self.queryRefFromName('baseMtlItemUomConversionsList')
            // refMain.form.resetFields()
            refMain.form.setFieldsValue({
              dcId: '',
              warehouseId: '',
              warehouseName: '',
              cargoOwnerId: '',
              cargoOwnerName: '',
            })
          }
        },
        {
          label: '出库确认',
          click() {
            let ref = self.queryRefFromName('baseMtlItemUomConversionsList')
            self.saveData(ref.getFieldsValue(true))
          }
        }
      ],
      list: [
        {
          key: 'main',
          tab: '主要信息',
          btns: [
            // { label: '辅助功能1', click() { } },
          ],
          item: { type: 'p-grid-form', config: items(self) }
        },
        // {
        //   key: 'baseMtlItemHealth',
        //   tab: '操作信息',
        // }
      ]
    },
    {
      key: 'two',
      checkedKey: 'baseMtlItemUomConversionsList',
      list: [
        {
          key: 'baseMtlItemUomConversionsList',
          tab: '主表',
          btns: [],
          item: {
            type: 'p-table-sheet',
            config: columns(self),
            rowKey: 'id',
            rowSelectType: 2,
            hasRowSelectCheck:true,
            colChange({ record, key, value, index,dataSource }) {

            },
            onClick({ index, record, key, dataSource,checkedStatus }) {
              // if (!record) return;
              console.log(index, record, key, dataSource, '44444444444')
              self.querydetailData(record,checkedStatus)
            },
          }
        }
      ]
    },
    {
      key: 'three',
      checkedKey: 'baseMtlItemUomConversionsList1',
      list: [
        {
          key: 'baseMtlItemUomConversionsList1',
          tab: '明细表',
          btns: [],
          item: {
            type: 'p-table-sheet',
            config: detailColumns(self),
            rowKey: '_row_count',
            rowSelectType: 2,
            hasRowSelectCheck:true,
            colChange({ record, key, value, index,dataSource }) {

            }
          }
        }
      ]
    },
  ]
}
