// 主界面查询表单
import {getBranchItems, getConsignorItems} from "@/views/kzyWMS/orderData/purchaseOrderManage/js/formItems";
import {upcloud_base_common} from "@/config/serviceSettings";
import {genSupplierSearchModal} from "@/views/kzyWMS/SearchModal";

const grid = {
  labelCol: {span: 8}, wrapperCol: {span: 16}, span: 6,
}

export function items(self) {
  return [
    // 货主
    ...getConsignorItems((event, targetName, selectRecords, form) => {
      const selectRecord = selectRecords[0] || {}
      const {companyCode = '', companyName = '', companyId = ''} = selectRecord
      form.setFieldsValue({consignorcode: companyCode, consignorname: companyName, consignorid: companyId})
    }),
    // 仓库
    ...getBranchItems((event, targetName, selectRecords, form) => {
      const selectRecord = selectRecords[0] || {}
      const {companyCode = '', companyName = '', companyId = ''} = selectRecord
      form.setFieldsValue({branchcode: companyCode, branchname: companyName, branchid: companyId})
    }),
    {
      type: 'select', name: 'rcvType', label: '订单类型', value: '', autoLoad: true,
      remoteUrl: {url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=oms_asn_bill_type', method: 'post'},
      replaceFields: {value: 'dictCode', label: 'dictName'},
      doneBefore: (data) => {
        const rcvTypes = self.rcvTypes
        data.data = data.data.filter(_ => rcvTypes.includes(_.dictCode))
        return data
      },
      done(data) {
        console.log(data)
        if (data.length === 1) {
          const {dictCode: rcvType} = data[0] || {}
          self.$refs.formTarget.form.setFieldsValue({rcvType})
        }
      },
      ...grid
    },
    // 供应商
    ...genSupplierSearchModal(1, false, false, function (vnode, p) {
      let vals = self.$refs.formTarget.form.getFieldsValue();
      p.data.cargoOwnerId = vals.cargoOwnerId;
      return p;
    }, null, grid),
    {type: 'input', name: 'asnAutoBillNum', label: 'asn单号', value: '', ...grid},
    {type: 'input', name: 'billno', label: '关联单号', value: '', ...grid},
    {
      type: 'select', name: 'status', label: '订单状态', value: '', autoLoad: true,
      remoteUrl: {url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=po_billh_status', method: 'post'},
      replaceFields: {value: 'dictCode', label: 'dictName'},
      ...grid
    },
    {type: 'input', name: 'remark', label: '备注', value: '', ...grid},
    {hidden: true, type: 'input', name: 'sAttainDate', label: '预定到货日期 开始', value: '', ...grid},
    {hidden: true, type: 'input', name: 'eAttainDate', label: '预定到货日期 结束', value: '', ...grid},
    {
      type: 'a-range-picker',
      name: "bdata",
      label: "预计到货时间",
      value: [],
      showTime: true,
      className: 'date-range',
      placeholder: ['起始预计到货时间', '结束预计到货时间'],
      ...grid,
      format: "YYYY-MM-DD HH:mm:ss",
      on: {
        change(date, dateString) {
          self.$refs.formTarget.form.setFieldsValue({
            sAttainDate: dateString[0],
            eAttainDate: dateString[1]
          })
        },
      }
    },
    {hidden: true, type: 'input', name: 'sdate', label: '开始日期', value: '', ...grid},
    {hidden: true, type: 'input', name: 'edate', label: '结束日期', value: '', ...grid},
    {
      type: 'a-range-picker',
      name: "adata",
      label: "开票时间",
      value: [],
      showTime: true,
      className: 'date-range',
      placeholder: ['起始采购订单日期', '结束采购订单日期'],
      ...grid,
      format: "YYYY-MM-DD HH:mm:ss",
      on: {
        change(date, dateString) {
          console.log(date, dateString, 'date, dateString');
          self.$refs.formTarget.form.setFieldsValue({
            sdate: dateString[0],
            edate: dateString[1]
          })
        },
      }
    },
    {type: 'input', name: 'buyerName', label: '开票员', value: '', ...grid},
    {
      type: 'buttonGroup',
      wrapperCol: {span: 16, offset: 8},
      options: [
        {
          value: '查询',
          icon: 'search',
          btnType: 'primary',
          style: 'margin-left:10px',
          htmlType: 'submit'
        },
        {
          value: '重置',
          icon: 'redo',
          style: 'margin-left:10px',
          on: {
            click: () => {
              self.$refs.formTarget.form.resetFields()
            }
          }
        }
      ]
    }
  ]
}