import type { CrudSchema } from '@/hooks/web/useCrudSchemas'
import { dateFormatter } from '@/utils/formatTime'

import * as ItembasicApi from '@/api/wms/itembasic'
import { Itembasic } from './../itembasic/itembasic.data'

import * as ProcessApi from '@/api/wms/process'
import { Process } from '../../factoryModeling/process/process.data'

const { t } = useI18n() // 国际化

/** 
 * @returns {Array} 基础物料清单
*/
export const Bom = useCrudSchemas(reactive<CrudSchema[]>([
  {
    label: '父物料代码',
    field: 'productItemCode',
    isSearch: true,
    sort: 'custom',
    table: {
      width: 150,
      fixed: 'left'
    },
    form: {
      // labelMessage: '信息提示说明!!!',
      componentProps: {
        isSearchList: true, // 开启查询弹窗
        searchListPlaceholder: '请选择物品代码', // 输入框占位文本
        searchField: 'code', // 查询弹窗赋值字段
        searchTitle: '物品基础信息', // 查询弹窗标题
        searchAllSchemas: Itembasic.allSchemas, // 查询弹窗所需类
        searchPage: ItembasicApi.getItembasicPage, // 查询弹窗所需分页方法
        searchCondition: [{
          key: 'available',
          value: 'TRUE',
          action: '==',
          isSearch: true,
          isMainValue: false
        }]
      }
    }
  },
  {
    label: '子物料代码',
    field: 'componentItemCode',
    isSearch: true,
    sort: 'custom',
    table: {
      width: 150
    },
    form: {
      // labelMessage: '信息提示说明!!!',
      componentProps: {
        isSearchList: true, // 开启查询弹窗
        searchListPlaceholder: '请选择物品代码', // 输入框占位文本
        searchField: 'code', // 查询弹窗赋值字段
        searchTitle: '物品基础信息', // 查询弹窗标题
        searchAllSchemas: Itembasic.allSchemas, // 查询弹窗所需类
        searchPage: ItembasicApi.getItembasicPage, // 查询弹窗所需分页方法
        searchCondition: [{
          key: 'available',
          value: 'TRUE',
          action: '==',
          isSearch: true,
          isMainValue: false
        }]
      }
    }
  },
  {
    label: '子物料计量单位',
    field: 'componentUom',
    dictType: DICT_TYPE.UOM,
    dictClass: 'string',
    sort: 'custom',
    isSearch: true,
    isTable: true,
    table: {
      width: 150
    }
  },
  {
    label: '子物料数量',
    field: 'componentQty',
    isSearch: true,
    sort: 'custom',
    form: {
      component: 'InputNumber',
      componentProps: {
        min: 1,
        precision: 6
      }
    },
    table: {
      width: 120
    }
  },
  {
    label: '工序代码',
    field: 'processCode',
    sort: 'custom',
    table: {
      width: 120
    },
    form: {
      // labelMessage: '信息提示说明!!!',
      componentProps: {
        isSearchList: true, // 开启查询弹窗
        searchListPlaceholder: '请选择工序代码', // 输入框占位文本
        searchField: 'code', // 查询弹窗赋值字段
        searchTitle: '工序信息', // 查询弹窗标题
        searchAllSchemas: Process.allSchemas, // 查询弹窗所需类
        searchPage: ProcessApi.getProcessPage, // 查询弹窗所需分页方法
        searchCondition: [{
          key: 'available',
          value: 'TRUE',
          isMainValue: false
        }]
      }
    }
  },
  {
    label: '版本',
    field: 'version',
    sort: 'custom'
  },
  {
    label: '层级',
    field: 'layer',
    sort: 'custom',
    // form: {
    //   component: 'Input',
    //   componentProps: {
    //     // focus: ()=>{console.log('111111111111111111111')},
    //     modelValue: '11111'
    //   }
    // }
  },
  {
    label: '生效时间',
    field: 'activeTime',
    isTable: true,
    sort: 'custom',
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    table: {
      width: 180
    },
    form: {
      component: 'DatePicker',
      componentProps: {
        style: {width:'100%'},
        type: 'datetime',
        dateFormat: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      }
    }
  },
  {
    label: '失效时间',
    field: 'expireTime',
    isTable: true,
    sort: 'custom',
    formatter : dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    table: {
      width: 180
    },
    form: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetime',
        dateFormat: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      }
    }
  },
  {
    label: '是否可用',
    field: 'available',
    sort: 'custom',
    dictType: DICT_TYPE.TRUE_FALSE,
    dictClass: 'string',
    table: {
      width: 120
    },
    form: {
      component: 'Switch',
      value: 'TRUE',
      componentProps: {
        inactiveValue: 'FALSE',
        activeValue: 'TRUE'
      }
    }
  },
  {
    label: '创建时间',
    field: 'createTime',
    isTable: true,
    sort: 'custom',
    formatter: dateFormatter,
    isForm: false,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    table: {
      width: 180
    },
    form: {
      component: 'DatePicker',
      componentProps: {
        style: {width:'100%'},
        type: 'datetime',
        dateFormat: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      }
    }
  },
  {
    label: '创建者',
    field: 'creator',
    isForm: false,
    isTable: false
  },
  { label: '备注', field: 'remark', isTable: false },
  {
    label: '操作',
    field: 'action',
    isDetail: false,
    isForm: false ,
    table: {
      width: 150,
      fixed: 'right'
    }
  }
]))

//表单校验
export const BomRules = reactive({
  productItemCode: [
    { required: true, message: '请输入父物料代码', trigger: 'change' }
  ],
  componentItemCode: [
    { required: true, message: '请输入子物料代码', trigger: 'change' }
  ],
  componentUom: [
    { required: true, message: '请选择子物料计量单位', trigger: 'change' }
  ],
  componentQty: [
    { required: true, message: '请输入子物料数量', trigger: 'blur' }
  ],
  processCode: [
    { required: true, message: '请输入工序代码', trigger: 'blur' },
    { max: 50, message: '不得超过50个字符', trigger: 'blur' },
  ],
  version: [
    { max: 50, message: '不得超过50个字符', trigger: 'blur' }
  ],
  layer: [
    { max: 50, message: '不得超过50个字符', trigger: 'blur' }
  ],
  remark: [
    { max: 50, message: '不得超过50个字符', trigger: 'blur' }
  ],
  available:  [
    { required: true, message: '请选择是否可用', trigger: 'change' }
  ],
})