import { genWarehouseAuthSearchModal, genCargoownerAuthSearchModal, genItemRelatedSearchModal } from "../../../../SearchModal";
import { api } from '@/views/kzyWMS/api/common/dictionaries-type'
import { upcloud_base_item, upcloud_base_common } from "@/config/serviceSettings";


let grid = {
  labelCol: { span: 5 }, span: 6,
};

let grid1 = {
  labelCol: { span: 8 }, span: 8,
};

export function setFormItems(self) {
  let _this = self
  return [
    ...genWarehouseAuthSearchModal(1, true, false, null, null, grid),
    ...genCargoownerAuthSearchModal(1, true, false, null, null, grid,),
    { type: "input", name: "stName", label: "策略名称", value: "", ...grid, },
    { type: "input", name: "stNum", label: "策略编号", value: "", ...grid, },
    // { type: "input", name: "field-input", label: "输入框", value: "", ...grid, },
    // { type: "input", name: "field-input", label: "输入框", value: "", ...grid, },
    {
      type: 'buttonGroup',
      span: 24,
      options: [
        {
          value: '查询',
          icon: 'search',
          btnType: 'primary',
          style: 'margin-left:10px',
          htmlType: 'submit'
        },
        {
          value: '重置',
          icon: 'redo',
          style: 'margin-left:10px',
          on: {
            click: () => {
              self.$refs.getForm.form.resetFields()
            }
          }
        }
      ]
    }
  ]
}

export const columns = [
  { title: 'rqcStId', hidden: true, dataIndex: 'rqcStId', },
  { hidden: true, title: '商品id', dataIndex: 'itemId', },
  { hidden: true, title: '货主id', dataIndex: 'mtlOwnerId', },
  { hidden: true, title: '供应商id', dataIndex: 'vendorId', },
  { hidden: true, title: '仓库id', dataIndex: 'warehouseId', className: 'v_top', },
  { type: 'txt', title: '策略名称', dataIndex: 'stName', key: "stName", className: 'v_top', width: 150 },
  { type: 'txt', title: '策略编号', dataIndex: 'stNum', key: "stNum", className: 'v_top', width: 100 },
  {
    type: 'txt', title: '策略类型', dataIndex: 'stType', key: "stType", customRender(text) {
      return text == '1' ? '非通用' : '通用'
    }, width: 100
  },
  {
   title: "商品大类", dataIndex: "itemCategoryCode", width: 140,
    autoLoad: true, remoteUrl: { url: api.queryDictList + '?dicttypeCodes=item_health_category', method: 'post' },
    replaceFields: {
      value: 'dictCode',
      label: 'dictName'
    },
  },
  { type: 'txt', title: '商品编码', dataIndex: 'itemCode', key: "itemCode", width: 150 },
  { type: 'txt', title: '商品名称', dataIndex: 'itemName', key: "itemName", className: 'v_top', width: 200 },

  {
    type: 'txt', title: "状态", dataIndex: "status", key: "status", customRender(text) {
      return text == '1' ? '是' : '否'
    }, width: 120
  },
  // { title: '收货员', dataIndex: 'executorUser', key: "executorUser", className: 'v_top', },
  { type: 'txt', title: '备注说明', dataIndex: 'remark', key: "remark", className: 'v_top', width: 260 },
  { title: '操作', dataIndex: 'operation', scopedSlots: { customRender: 'operation' }, },
]

export const tableColumns = [
  { title: 'ruleId', hidden: true, dataIndex: '质检规则主键id', },
  { hidden: true, title: '商品id', dataIndex: 'itemId', },
  { hidden: true, title: '货主id', dataIndex: 'mtlOwnerId', },
  { hidden: true, title: '供应商id', dataIndex: 'vendorId', },
  { hidden: true, title: '仓库id', dataIndex: 'warehouseId', className: 'v_top', },
  { title: '质检规则名称', dataIndex: 'ruleName', key: "ruleName", className: 'v_top', width: 180 },
  { title: '查询条件表达式', dataIndex: 'searchRegexp', key: "searchRegexp", className: 'v_top', width: 180 },
  { title: '匹配表达式', dataIndex: 'matchRegexp', key: "matchRegexp", className: 'v_top', width: 180 },
  {
    title: '状态', dataIndex: 'status', key: "status", width: 80, customRender(text) {
      return text == '1' ? '是' : '否'
    }
  },
  { title: '规则排序', dataIndex: 'sortIndex', className: 'v_top' },
  { title: '备注', dataIndex: 'remark', className: 'v_top' },
  { title: '操作', dataIndex: 'operation', fixed: 'right', scopedSlots: { customRender: 'operation' }, },

]

export function mainItems(self) {
  let _this = self
  return [
    { hidden: true, type: "input", name: "rqcStId", label: "质检策略主键id", value: "", ...grid1, },
    { hidden: true, type: "input", name: "itemId", label: "itemId", value: "", ...grid1, },
    { hidden: true, type: "input", name: "itemCode", label: "itemCode", value: "", ...grid1, },
    { hidden: true, type: "input", name: "mtlOwnerId", label: "mtlOwnerId", value: "", ...grid1, },

    ...genWarehouseAuthSearchModal(1, true, false, null, null, grid1),
    ...genCargoownerAuthSearchModal(1, true, false, null, null, grid1,),
    { type: "input", name: "stName", label: "策略名称", value: "", ...grid1, required: true, },
    { type: "input", name: "stNum", label: "策略编号", value: "", ...grid1, required: true, },
    {
      type: "select", name: "stType", label: "策略类型", value: "", ...grid1, allowClear: false, required: true,
      options: [
        { label: "通用", value: "0" },
        { label: "非通用", value: "1" },
      ],
      on: {
        change(value, label, option) {
          console.log(value, label, option, '1111111111111');
          if (value != '1') {
            self.$refs.mainForm.form.hiddenItem(["itemCategoryCode", 'itemName'])
            self.$refs.mainForm.form.setFieldsValue({
              itemCategoryCode: '',
              itemName: "",
              itemId: "",
              itemCode: "",
            })
            return
          } else {
            self.$refs.mainForm.form.unhiddenItem(["itemCategoryCode", 'itemName'])
          }
        }
      }
    },
    // billType
    {
      type: 'select', name: 'billType', label: '入库类型', value: '', autoLoad: true, required: true,
      remoteUrl: { url: upcloud_base_common + '/dict/listbytype?dicttypeCodes=oms_asn_bill_type', method: 'post' },
      replaceFields: { value: 'dictCode', label: 'dictName' },
      ...grid1
    },
    {
      type: 'select', name: 'itemCategoryCode', label: '商品大类', value: '', autoLoad: true, hidden: true, ...grid1,
      remoteUrl: { url: api.queryDictList + '?dicttypeCodes=item_health_category', method: 'post' },
      replaceFields: { value: 'dictCode', label: 'dictName' }
    },
    {
      name: 'itemName',
      label: '商品名称',
      type: 'search',
      targetValueKey: 'itemName',
      targetClick: true,
      ...grid1,
      modalOpenChange: (open) => {
        let values = self.$refs.mainForm.form.getFieldsValue()
        if (!values.cargoOwnerId) {
          // 货主
          self.$message.warn('请选择货主')
          return false
        } else if (!values.itemCategoryCode) {
          // 商品大类
          self.$message.warn('请先选择商品大类')
          return false
        } else {
          return true
        }
      },
      toolBar: {
        items: [
          {
            span: 16,
            type: 'input',
            name: 'condition',
            label: '商品名称/编码/助记码',
            value: ''
          }
        ],
      },
      tableConfig: {
        rowKey: 'itemId',
        remoteUrl: { url: upcloud_base_item + '/item/pagebyRelated', method: 'post' },
        remoteParams: {
          cargoOwnerId: self.cargoOwnerId,
          // warehouseId: self.warehouseId,
          itemCategoryCode: self.itemCategoryCode
        },
        remoteBefore: (res, remoteParams, form, val1, val2) => {
          let values = self.$refs.mainForm.form.getFieldsValue()
          remoteParams.data = {
            ...remoteParams.data,
            cargoOwnerId: values.cargoOwnerId,
            // warehouseId: values.warehouseId,
            itemCategoryCode: values.itemCategoryCode,

          }
          console.log(remoteParams, 'remoteParamsremoteParamsremoteParams')
          return remoteParams
        },
        columns: [
          {
            dataIndex: 'itemName',
            title: '商品名称',
            key: 'itemName',
          },
          {
            dataIndex: 'itemCode',
            title: '商品编码',
            key: 'itemCode',
          },
          {
            dataIndex: 'itemId',
            title: '商品id',
            key: 'itemId',
          },
        ],
      },
      on: {
        ok(event, value, rowData, form, name, index, tableRowData) {
          console.log(arguments, '6666666666')
          form.setFieldsValue({
            itemId: rowData[0].itemId,
            itemCode: rowData[0].itemCode,
          })
        },
        change(event, value, rowData, form) {
          console.log(arguments, '8888888')
          if (!value) {
            form.setFieldsValue({
              itemId: '',
              itemCode: '',
            })
          }
        },
      },
    },
    {
      type: "select", name: "status", label: "状态", value: "", ...grid1, allowClear: false, required: true, options: [
        { label: "否", value: "0" },
        { label: "是", value: "1" },
      ],
    },

    { type: "input", name: "remark", label: "备注说明", value: "", ...grid1, },

  ]
}

export function detailItems(self) {
  let _this = self
  return [
    { hidden: true, type: "input", name: "rqcStId", label: "质检策略id", value: "", ...grid1, },
    { hidden: true, type: "input", name: "ruleId", label: "质检规则主键id", value: "", ...grid1, },
    { type: "input", name: "ruleName", label: "质检规则名称", value: "", ...grid1, required: true, },
    { type: "input", name: "sortIndex", label: "规则排序", value: "", ...grid1, },
    { type: "input", name: "searchRegexp", label: "查询条件表达式", value: "", ...grid1, required: true, },
    { type: "input", name: "matchRegexp", label: "匹配表达式", value: "", ...grid1, required: true, },
    {
      type: "select", name: "status", label: "状态", value: "", ...grid1, allowClear: false, options: [
        { label: "否", value: "0" },
        { label: "是", value: "1" },
      ],
    },

    { type: "input", name: "remark", label: "备注", value: "", ...grid1, },

  ]
}