import type { CrudSchema } from '@/hooks/web/useCrudSchemas'
import { TableColumn } from '@/types/table'

import { dateFormatter } from '@/utils/formatTime'
import * as getRequestsettingApi  from '@/api/wms/requestsetting/index'

import * as DeliverPlanMainApi from '@/api/wms/deliverPlanMain'
import { DeliverPlanMain } from '../../deliverplan/deliverPlanMain/deliverPlanMain.data'

import * as  locationApi from '@/api/wms/location'
import { Location } from '../../../basicDataManage/factoryModeling/location/location.data'
import * as confgiApi  from '@/api/infra/config'
import * as  ItemBasicApi from '@/api/wms/itembasic'
import { Itembasic } from '../../../basicDataManage/itemManage/itembasic/itembasic.data'
import * as WorkstationApi from '@/api/wms/workstation'
const { t } = useI18n() // 国际化

// 获取自动提交自动通过自动执行,跳过任务直接删生成记录的默认值
const queryParams = {
  pageSize:10,
  pageNo:1,
  code:'StockupRequest'
}
 const data = await getRequestsettingApi.getRequestsettingPage(queryParams)
 const requestsettingData =data?.list[0]||{}
 const confgiDataOne = await confgiApi.queryByKey("Stockup-areaType")
  // 获取当前操作人的部门
import { useUserStore } from '@/store/modules/user'
const userStore = useUserStore()
const userDept = userStore.userSelfInfo.dept
 // id 转str 否则form回显匹配不到
 userDept.id = userDept.id.toString()
const userDeptArray:any = [userDept]

// 表单校验
export const StockupMainRequestRules = reactive({
  // deliverPlanNumber: [
  //   { required: true, message: '请选择客户代码', trigger: 'change' }
  // ],
})

export const StockupMainRequest = useCrudSchemas(reactive<CrudSchema[]>([
  {
    label: '单据号',
    field: 'number',
    sort: 'custom',
    isSearch: true,
    table: {
      width: 180,
      fixed: 'left'
    },
    isForm: false,
  },
  {
    label: '发货计划单号',
    field: 'deliverPlanNumber',
    sort: 'custom',
    table: {
      width: 180
    },
    form: {
      // labelMessage: '信息提示说明!!!',
      componentProps: {
        enterSearch:true,
        isSearchList: true, // 开启查询弹窗
        searchListPlaceholder: '请选择发货计划单号', // 输入框占位文本
        searchField: 'number', // 查询弹窗赋值字段
        searchTitle: '发货计划信息', // 查询弹窗标题
        searchAllSchemas: DeliverPlanMain.allSchemas, // 查询弹窗所需类
        searchPage: DeliverPlanMainApi.getDeliverPlanMainPage, // 查询弹窗所需分页方法
        searchCondition: [{
          key: 'available',
          value: 'TRUE',
          isMainValue: false
        },
        {
          key: 'status',
          value: '6',
          isMainValue: false
          }],
        verificationParams: [{
          key: 'number',
          action: '==',
          value: '',
          isMainValue: false,
          isSearch: true,
          isFormModel: true,
        }], // 失去焦点校验参数
      }
    }
  },
  {
    label: '从仓库',
    field: 'fromWarehouseCode',
    sort: 'custom',
    isSearch: false,
    table: {
      width: 120
    },
    sortTableDefault:1000,
    // form: {
    //   // labelMessage: '信息提示说明!!!',
    //   componentProps: {
    //     isSearchList: true, // 开启查询弹窗
    //     searchListPlaceholder: '请选择从仓库代码', // 输入框占位文本
    //     searchField: 'number', // 查询弹窗赋值字段
    //     searchTitle: '仓库信息', // 查询弹窗标题
    //     searchAllSchemas: Itemwarehouse.allSchemas, // 查询弹窗所需类
    //     searchPage: WarehouseApi.getWarehousePage, // 查询弹窗所需分页方法
    //     searchCondition: [{
    //       key: 'available',
    //       value: 'TRUE',
    //       isMainValue: false
    //     }]
    //   }
    // }
    isForm: false
  },
  {
    label: '到仓库',
    field: 'toWarehouseCode',
    sort: 'custom',
    isSearch: false,
    table: {
      width: 120
    },
    isForm: false,
    sortTableDefault:1000,

  },
  {
    label: '从库区类型范围',
    field: 'fromAreaTypes',
    dictType: DICT_TYPE.AREA_TYPE,
    dictClass: 'string',
    isTable: false,
    sort: 'custom',
    table: {
      width: 150
    },
    isForm: false,
  },
  {
    label: '从库区代码范围',
    field: 'fromAreaCodes',
    sort: 'custom',
    isSearch: false,
    table: {
      width: 160
    },
    isTable: false,
    isForm: false
  },
  {
    label: '业务类型',
    field: 'businessType',
    sort: 'custom',
    isSearch: false,
    isTable: false,
    form: {
      value: 'Stockup',
      componentProps: {
        disabled: true,
      }
    },
    table: {
      width: 110
    },
    isForm: false,
  },
  {
    label: '创建时间',
    field: 'createTime',
    sort: 'custom',
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    isSearch: true,
    search: {
      component: 'DatePicker',
      componentProps: {
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        type: 'daterange',
        defaultTime: [new Date('1 00:00:00'), new Date('1 23:59:59')]
      }
    },
    isTable: true,
    table: {
      width: 180
    },
    isForm: false,
  },
  {
    label: '创建者',
    field: 'creator',
    table: {
      width: 130
    },
    isForm: false,
    isTable: true
  },
  {
    label: '最后更新时间',
    field: 'updateTime',
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    sort: 'custom',
    table: {
      width: 180
    },
    isTable: false,
    form: {
      component: 'DatePicker',
      componentProps: {
        style: { width: '100%' },
        type: 'datetime',
        dateFormat: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x'
      }
    },
    isForm: false
  },
  {
    label: '最后更新者',
    field: 'updater',
    sort: 'custom',
    table: {
      width: 150
    },
    isTable: false,
    isForm: false
  },
  {
    label: '发货日期',
    field: 'deliverDate',
    sort: 'custom',
    formatter: dateFormatter,
    isSearch: false,
    isTable: false,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    search: {
      component: 'DatePicker',
      componentProps: {
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        type: 'daterange',
        defaultTime: [new Date('1 00:00:00'), new Date('1 23:59:59')]
      }
    },
    form: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetime',
        valueFormat: 'x'
      }
    },
    table: {
      width: 180
    },
  },
  {
    label: '申请时间',
    field: 'requestTime',
    sort: 'custom',
    formatter: dateFormatter,
    isSearch: false,
    isTable: false,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    search: {
      component: 'DatePicker',
      componentProps: {
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        type: 'daterange',
        defaultTime: [new Date('1 00:00:00'), new Date('1 23:59:59')]
      }
    },
    form: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetime',
        valueFormat: 'x'
      }
    },
    table: {
      width: 180
    },
  },
  {
    label: '截止时间',
    field: 'dueTime',
    sort: 'custom',
    formatter: dateFormatter,
    isSearch: false,
    isTable: false,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    search: {
      component: 'DatePicker',
      componentProps: {
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        type: 'daterange',
        defaultTime: [new Date('1 00:00:00'), new Date('1 23:59:59')]
      }
    },
    form: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetime',
        valueFormat: 'x'
      }
    },
    table: {
      width: 180
    },
  },
  {
    label: '部门',
    field: 'departmentCode',
    sort: 'custom',
    table: {
      width: 150
    },
    isTable: false,
    formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
      return userDeptArray.find((account) => account.id == cellValue)?.name
    },
    isForm:false,
    form: {
      value: userDept.id,
      component: 'Select',
      api: () => userDeptArray,
      componentProps: {
        disabled: true,
        optionsAlias: {
          labelField: 'name',
          valueField: 'id'
        }
      }
    }
  },
  {
    label: '状态',
    field: 'status',
    dictType: DICT_TYPE.REQUEST_STATUS,
    dictClass: 'string',
    isSearch: true,
    isForm: false,
    isTable: true,
    sort: 'custom',
    table: {
      width: 150
    },
  },
  {
    label: '备注',
    field: 'remark',
    sort: 'custom',
    isSearch: false,
    isTable: false,
  },
    {
      label: '自动提交',
      field: 'autoCommit',
      dictType: DICT_TYPE.TRUE_FALSE,
      dictClass: 'string',
      isTable: false,
      isForm: false,
      sort: 'custom',
      table: {
        width: 150
      },
      form: {
        component: 'Switch',
        value: requestsettingData.autoCommit,
        componentProps: {
          inactiveValue: 'FALSE',
          activeValue: 'TRUE',
          disabled: true
        }
      }
    },
    {
      label: '自动通过',
      field: 'autoAgree',
      dictType: DICT_TYPE.TRUE_FALSE,
      dictClass: 'string',
      isTable: false,
      isForm: false,
      sort: 'custom',
      table: {
        width: 150
      },
      form: {
        component: 'Switch',
        value: requestsettingData.autoAgree,
        componentProps: {
          inactiveValue: 'FALSE',
          activeValue: 'TRUE',
          disabled: true
        }
      }
    },
    {
      label: '自动执行',
      field: 'autoExecute',
      dictType: DICT_TYPE.TRUE_FALSE,
      dictClass: 'string',
      isTable: false,
      isForm: false,
      sort: 'custom',
      table: {
        width: 150
      },
      form: {
        component: 'Switch',
        value: requestsettingData.autoExecute,
        componentProps: {
          inactiveValue: 'FALSE',
          activeValue: 'TRUE',
          disabled: true
        }
      }
    },
    {
      label: '直接生成记录',
      field: 'directCreateRecord',
      dictType: DICT_TYPE.TRUE_FALSE,
      dictClass: 'string',
      isForm: false,
      isTable: false,
      sort: 'custom',
      table: {
        width: 150
      },
      form: {
        component: 'Switch',
        value: requestsettingData.directCreateRecord,
        componentProps: {
          inactiveValue: 'FALSE',
          activeValue: 'TRUE',
          disabled: true
        }
      }
    },
  {
    label: '到库区类型范围',
    field: 'toAreaTypes',
    dictType: DICT_TYPE.AREA_TYPE,
    dictClass: 'string',
    isTable: false,
    sort: 'custom',
    table: {
      width: 150
    },
    isForm: false,
  },
  {
    label: '到库区代码范围',
    field: 'toAreaCodes',
    sort: 'custom',
    isSearch: false,
    table: {
      width: 150
    },
    isTable: false,
    isForm: false
  },
  {
    label: '入库库存状态范围',
    field: 'inInventoryStatuses',
    isSearch: false,
    table: {
      width: 170
    },
    dictType: DICT_TYPE.INVENTORY_STATUS,
    dictClass: 'string',
    isTable: false,
    sort: 'custom',
    tableForm: {
      disabled: true,
      type: 'Select',
      width: 150
    },
    isForm: false
  },
  {
    label: '出库库存状态范围',
    field: 'outInventoryStatuses',
    sort: 'custom',
    isSearch: false,
    table: {
      width: 170
    },
    dictType: DICT_TYPE.INVENTORY_STATUS,
    dictClass: 'string',
    isTable: false,
    tableForm: {
      disabled: true,
      type: 'Select',
      width: 150
    },
    isForm: false
  },
  {
    label: '操作',
    field: 'action',
    isForm: false,
    table: {
      width: 300,
      fixed: 'right'
    }
  }
]))

// 表单校验
export const StockupDetailRequestRules = reactive({
  packingNumber: [
    { required: true, message: '请选择包装号', trigger: 'change' }
  ],
  batch: [
    { required: true, message: '请选择批次', trigger: 'change' }
  ],
  inventoryStatus: [
    { required: true, message: '请选择库存状态', trigger: 'change' }
  ],
  uom: [
    { required: true, message: '请选择计量单位', trigger: 'change' }
  ],
  itemCode: [
    { required: true, message: '请选择物料代码', trigger: 'change' }
  ],
  toLocationCode : [
    { required: true, message: '请选择到库代码', trigger: 'change' }
  ]
})

// //子表
export const StockupDetailRequest = useCrudSchemas(reactive<CrudSchema[]>([
  {
    label: '单据号',
    field: 'number',
    sort: 'custom',
    isForm: false,
    isTableForm: false,
    table: {
      width: 180
    },
    hiddenInMain: true
  },
  // {
  //   label: '包装号',
  //   field: 'packingNumber',
  //   sort: 'custom',
  //   table: {
  //     width: 150
  //   },
  //   tableForm:{
  //     isInpuFocusShow: true, // 开启查询弹窗
  //     searchListPlaceholder: '请选择包装号',
  //     searchField: 'packingNumber',
  //     searchTitle: '库存余额信息',
  //     searchAllSchemas: Balance.allSchemas,
  //     searchPage: BalanceApi.getBalanceItemPage,
  //     searchCondition: [
  //     //   {
  //     //   key:'customerCode', // 查询列表中字段
  //     //   value:'customerCode', // 指主表某字段
  //     //   message: '请选择客户代码!', // 当前置条件为空时 弹出信息提示
  //     //   isMainValue: true // 表示查询条件是主表的字段的值
  //     // }
  //   ]
  //   },
  //   form: {
  //     // labelMessage: '信息提示说明!!!',
  //     componentProps: {
  //       isSearchList: true, // 开启查询弹窗
  //       searchListPlaceholder: '请选择包装号',
  //       searchField: 'packingNumber',
  //       searchTitle: '库存余额信息',
  //       searchAllSchemas: Balance.allSchemas,
  //       searchPage: BalanceApi.getBalanceItemPage,
  //       searchCondition: [
  //       //   {
  //       //   key:'customerCode', // 查询列表中字段
  //       //   value:'customerCode', // 指主表某字段
  //       //   message: '请选择客户代码!', // 当前置条件为空时 弹出信息提示
  //       //   isMainValue: true // 表示查询条件是主表的字段的值
  //       // }
  //     ]
  //     }
  //   }
  // },
  // {
  //   label: '器具号',
  //   field: 'containerNumber',
  //   sort: 'custom',
  //   table: {
  //     width: 100
  //   },
  //   isSearch: true,
  // },
  // {
  //   label: '批次',
  //   field: 'batch',
  //   sort: 'custom',
  //   isSearch: true,
  //   tableForm: {
  //     disabled: true,
  //   },
  //   form: {
  //     componentProps: {
  //       disabled: true
  //     }
  //   }
  // },
  // {
  //   label: '库存状态',
  //   field: 'inventoryStatus',
  //   dictType: DICT_TYPE.INVENTORY_STATUS,
  //   dictClass: 'string',
  //   isTable: true,
  //   sort: 'custom',
  //   table: {
  //     width: 120
  //   },
  //   tableForm: {
  //     disabled: true,
  //     type: 'Select'
  //   },
  //   form: {
  //     componentProps: {
  //       disabled: true
  //     }
  //   }
  // },
  {
    label: '物料代码',
    field: 'itemCode',
    sort: 'custom',
    table: {
      width: 150
    },
    tableForm:{
      multiple:true,
      isInpuFocusShow: false, // 开启查询弹窗
      searchListPlaceholder: '请选择物料代码',
      searchField: 'code',
      searchTitle: '物料基础信息',
      searchAllSchemas: Itembasic.allSchemas,
      searchPage: ItemBasicApi.selectItembasicPageToFgAndSemibasicPage,
      searchCondition: [{
        key: 'available',
        value: 'TRUE',
        isMainValue: false
      }],
      verificationPage: ItemBasicApi.getItemListByCodes, // 校验数去焦点输入是否正确的方法
      isShowTableFormSearch: true,
      verificationParams: [{
        key: 'code',
        action: '==',
        value: '',
        isMainValue: false,
        isSearch: true,
        isFormModel: true,
      }], // 失去焦点校验参数
    },
    form: {
      componentProps: {
        isSearchList: true,
        searchListPlaceholder: '请选择物料代码',
        searchField: 'itemCode',
        searchTitle: '物料基础信息',
        searchAllSchemas: Itembasic.allSchemas,
        searchPage: ItemBasicApi.selectItembasicPageToFgAndSemibasicPage,
        searchCondition: [{
          key: 'available',
          value: 'TRUE',
          isMainValue: false
        }],
        verificationParams: [{
          key: 'code',
          action: '==',
          value: '',
          isMainValue: false,
          isSearch: true,
          isFormModel: true,
        }], // 失去焦点校验参数
      }
    }
  },
  {
    label: '物品名称',
    field: 'itemName',
    sort: 'custom',
    isTableForm: false,
    table: {
      width: 150
    },
  },
  {
    label: '物品描述1',
    field: 'itemDesc1',
    sort: 'custom',
    isTableForm: false,
    table: {
      width: 120
    },
    hiddenInMain: true
  },
  {
    label: '物品描述2',
    field: 'itemDesc2',
    sort: 'custom',
    isTableForm: false,
    table: {
      width: 120
    },
    hiddenInMain: true
  },
  {
    label: '项目代码',
    field: 'projectCode',
    sort: 'custom',
    isTableForm: false,
    table: {
      width: 120
    },
    hiddenInMain: true
  },
  {
    label: '数量',
    field: 'qty',
    sort: 'custom',
    tableForm: {
      type: 'InputNumber',
      min: 0,
      max:999999,
      // precision: 6
    },
  },
  {
    label: '计量单位',
    field: 'uom',
    dictType: DICT_TYPE.UOM,
    dictClass: 'string',
    isTable: true,
    sort: 'custom',
    table: {
      width: 150
    },
    tableForm: {
      type: 'Select',
      disabled: true
    },
    form: {
      componentProps: {
        disabled: true
      }
    }
  },
  {
    label: '从库位',
    field: 'fromLocationCode',
    sort: 'custom',
    isTableForm: false,
    isTable: false,
    table: {
      width: 150
    },
  },
  {
    label: '到库位',
    field: 'toLocationCode',
    sort: 'custom',
    table: {
      width: 120
    },
    tableForm: {
      enterSearch:true,
      isInpuFocusShow: true, // 开启查询弹窗
      searchListPlaceholder: '请选择库位代码',
      searchField: 'location',
      searchTitle: '库位基础信息',
      searchAllSchemas: Location.allSchemas,
      searchPage: WorkstationApi.selectAreaTypeToLocation,
      searchCondition: [{
        key: 'available',
        value: 'TRUE',
        isMainValue: false
      },
      {
        key: 'areaType',
        value: confgiDataOne.areaType,
        isMainValue: false
        }],
      verificationParams: [{
        key: 'code',
        action: '==',
        value: '',
        isMainValue: false,
        isSearch: true,
        isFormModel: true,
      }], // 失去焦点校验参数
    },
    form: {
      componentProps: {
        enterSearch: true,
        isSearchList: true,
        searchListPlaceholder: '请选择库位代码',
        searchField: 'location',
        searchTitle: '库位基础信息',
        searchAllSchemas: Location.allSchemas,
        searchPage: WorkstationApi.selectAreaTypeToLocation,
        searchCondition: [{
          key: 'available',
          value: 'TRUE',
          isMainValue: false
        },
        {
          key: 'areaType',
          value: confgiDataOne.areaType,
          isMainValue: false
          }],
        verificationParams: [{
          key: 'code',
          action: '==',
          value: '',
          isMainValue: false,
          isSearch: true,
          isFormModel: true,
        }], // 失去焦点校验参数
      }
    }
  },
  {
    label: '备注',
    field: 'remark',
    sort: 'custom',
    hiddenInMain: true
  },
  {
    label: '创建时间',
    field: 'createTime',
    sort: 'custom',
    formatter: dateFormatter,
    table: {
      width: 120
    },
    hiddenInMain: true,
    search: {
      component: 'DatePicker',
      componentProps: {
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        type: 'daterange',
        defaultTime: [new Date('1 00:00:00'), new Date('1 23:59:59')]
      }
    },
    isTableForm: false,
  },
  {
    label: '创建者',
    field: 'creator',
    table: {
      width: 130
    },
    isForm: false,
    isTable: true
  },
  {
    label: '操作',
    field: 'action',
    isDetail: false,
    isForm: false ,
    table: {
      width: 150,
      fixed: 'right'
    },
    hiddenInMain: true,
    isTableForm:false,
  }
]))