import type { CrudSchema } from '@/hooks/web/useCrudSchemas'
import { dateFormatter } from '@/utils/formatTime'
import * as SupplierApi from '@/api/wms/supplier'
import { Supplier } from '@/views/wms/basicDataManage/supplierManage/supplier/supplier.data'
const { t } = useI18n() // 国际化

import * as getRequestsettingApi from '@/api/wms/requestsetting/index'
import * as supplierinvoiceRequestDetailApi from '@/api/wms/supplierinvoiceRequestDetail'

// 获取自动提交自动通过自动执行,跳过任务直接删生成记录的默认值
const queryParams = {
  pageSize: 10,
  pageNo: 1,
  code: 'PurchaseInvoiceRequest'
}
const data = await getRequestsettingApi.getRequestsettingPage(queryParams)
const requestsettingData = data?.list[0] || {}

 // 获取当前操作人的部门
 import { useUserStore } from '@/store/modules/user'
 import { TableColumn } from '@/types/table'
 const userStore = useUserStore()
 const userDept = userStore.userSelfInfo.dept
 // id 转str 否则form回显匹配不到
 userDept.id = userDept.id.toString()
 const userDeptArray:any = [userDept]
 

/**
 * @returns {Array} 采购订单或者采购退货单
 */
export const PurchaseReceiptOrReturnRecordDetail = useCrudSchemas(reactive<CrudSchema[]>([
  {
    label: '项目编码',
    field: 'projectCode'
  },
  {
    label: '单据类型',
    field: 'billType'
  },
  {
    label: '单据号',
    field: 'recvBillNum'
  },
  {
    label: '发货单号',
    field: 'asnBillNum'
  },
  {
    label: '供应商代码',
    field: 'supplierCode'
  },
  {
    label: '订单号',
    field: 'poNumber'
  },
  {
    label: '订单行',
    field: 'poLine'
  },
  {
    label: '采购价格',
    field: 'purchasePrice'
  },
  {
    label: '可开票数量',
    field: 'invoicableQuantity'
  },
  {
    label: '物料代码',
    field: 'itemCode'
  },
  {
    label: '创建时间',
    field: 'createTime',
    isTable: true,
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    isTableForm: false,
    isForm: false
  }
]))


/**
 * @returns {Array} 供应商发票申请主表
 */
export const SupplierinvoiceRequestMain = useCrudSchemas(reactive<CrudSchema[]>([
  {
    label: '单据号',
    field: 'number',
    sort: 'custom',
    table: {
      width: 180,
      fixed: 'left'
    },
    isSearch: true,
    isForm: false
  },
  {
    label: '供应商代码',
    field: 'supplierCode',
    sort: 'custom',
    table: {
      width: 150
    },
    isSearch: true,
    form: {
      // labelMessage: '信息提示说明!!!',
      componentProps: {
        isSearchList: true, // 开启查询弹窗
        searchListPlaceholder: '请选择供应商代码', // 输入框占位文本
        searchField: 'code', // 查询弹窗赋值字段
        searchTitle: '供应商信息', // 查询弹窗标题
        searchAllSchemas: Supplier.allSchemas, // 查询弹窗所需类
        searchPage: SupplierApi.getSupplierPage // 查询弹窗所需分页方法
      }
    }
  },
  {
    label: '状态',
    field: 'status',
    dictType: DICT_TYPE.SUPPLIERINVOICE_REQUEST_STATUS,
    dictClass: 'string',
    isTable: true,
    sort: 'custom',
    table: {
      width: 150
    },
    isSearch: true,
    form: {
      value: '1',
      componentProps: {
        disabled: true
      }
    }
  },
  {
    label: '金额',
    field: 'amount',
    table: {
      width: 150
    },
    form: {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 6,
        disabled: true,
      }
    },
    tableForm: {
      type: 'InputNumber',
      min: 0,
      precision: 6,
    }
  },
  {
    label: '税率',
    field: 'taxRate',
    table: {
      width: 150
    },
    form: {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 6,
      }
    },
    tableForm: {
      type: 'InputNumber',
      min: 0,
      precision: 6,
    }
  },
  {
    label: '税额',
    field: 'taxAmount',
    table: {
      width: 150
    },
    form: {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 6,
      }
    },
    tableForm: {
      type: 'InputNumber',
      min: 0,
      precision: 6,
    }
  },
  {
    label: '税后金额',
    field: 'afterTaxAmount',
    table: {
      width: 150
    },
    form: {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 6,
      }
    },
    tableForm: {
      type: 'InputNumber',
      min: 0,
      precision: 6,
    }
  },
  {
    label: '索赔金额',
    field: 'claimAmount',
    table: {
      width: 150
    },
    isTable:false,
    isTableForm:false,
    isForm:false,
    isDetail:false,
    form: {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 6,
      }
    },
    tableForm: {
      type: 'InputNumber',
      min: 0,
      precision: 6,
    }
  },
  {
    label: '折扣金额',
    field: 'discountAmount',
    table: {
      width: 150
    },
    form: {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 6,
      }
    },
    tableForm: {
      type: 'InputNumber',
      min: 0,
      precision: 6,
    }
  },
  {
    label: '调整税额',
    field: 'adjustingTaxAmount',
    table: {
      width: 150
    },
    form: {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 6,
      }
    },
    tableForm: {
      type: 'InputNumber',
      min: 0,
      precision: 6,
    }
  },
  {
    label: '金税票号',
    field: 'goldenTaxInvoiceNumber',
    table: {
      width: 150
    },
  },
  {
    label: '快递单号',
    field: 'expressTrackingNumber',
    table: {
      width: 150
    },
  },
  {
    label: '发票时间',
    field: 'invoiceTime',
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    sort: 'custom',
    table: {
      width: 180
    },
    form: {
      component: 'DatePicker',
      componentProps: {
        style: {width:'100%'},
        type: 'datetime',
        dateFormat: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      }
    },
  },
  
  {
    label: '申请时间',
    field: 'requestTime',
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    sort: 'custom',
    table: {
      width: 180
    },
    isForm:false,
    form: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetime',
        dateFormat: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      }
    },
  },
  {
    label: '部门',
    field: 'departmentCode',
    sort: 'custom',
    table: {
      width: 150
    },
    formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
      return userDeptArray.find((account) => account.id == cellValue)?.name
    },
    form: {
      value: userDept.id,
      component: 'Select',
      api: () => userDeptArray,
      componentProps: {
        disabled: true,
        optionsAlias: {
          labelField: 'name',
          valueField: 'id'
        }
      }
    }
  },
  {
    label: '创建者',
    field: 'creator',
    sort: 'custom',
    table: {
      width: 150
    },
    isForm: false,
  },
  {
    label: '创建时间',
    field: 'createTime',
    isTable: true,
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    sort: 'custom',
    table: {
      width: 180
    },
    isForm: false,
  },
  {
    label: '最后更新者',
    field: 'updater',
    sort: 'custom',
    table: {
      width: 150
    },
    isForm: false,
  },
  {
    label: '最后更新时间',
    field: 'updateTime',
    isTable: true,
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    sort: 'custom',
    table: {
      width: 180
    },
    isForm: false,
    form: {
      component: 'DatePicker',
      componentProps: {
        type: 'datetime',
        dateFormat: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      }
    },
  },
  {
    label: '备注',
    field: 'remark',
    sort: 'custom',
    table: {
      width: 150
    },
    isTable: false,
  },
  {
    label: '自动提交',
    field: 'autoCommit',
    dictType: DICT_TYPE.TRUE_FALSE,
    dictClass: 'string',
    isTable: false,
    isForm:false,
    isTableForm:false,
    isDetail: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,
    isTableForm:false,
    isDetail: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,
    isTableForm:false,
    isDetail: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',
    isTable: false,
    isForm:false,
    isTableForm:false,
    isDetail:false,
    sort: 'custom',
    table: {
      width: 150
    },
    form: {
      component: 'Switch',
      value: requestsettingData.directCreateRecord,
      componentProps: {
        inactiveValue: 'FALSE',
        activeValue: 'TRUE',
        disabled: true,
      }
    }
  },
  {
    label: '操作',
    field: 'action',
    isDetail: false,
    isForm: false,
    table: {
      width: 300,
      fixed: 'right'
    },
  }
]))

//表单校验
export const SupplierinvoiceRequestMainRules = reactive({
  supplierCode: [
    { required: true, message: '请选择供应商代码', trigger: 'change' }
  ],
  departmentCode: [
    { required: true, message: '请输入部门', trigger: 'blur' }
  ],
  autoCommit: [
    { required: true, message: '请选择是否自动提交', trigger: 'change' }
  ],
  autoAgree: [
    { required: true, message: '请选择是否自动通过', trigger: 'change' }
  ],
  autoExecute: [
    { required: true, message: '请选择是否自动执行', trigger: 'change' }
  ],
  directCreateRecord: [
    { required: true, message: '请选择是否跳过任务直接生成记录', trigger: 'change' }
  ],
  businessType: [
    { required: true, message: '请输入业务类型', trigger: 'blur' }
  ],
  remark:  [
    { max: 50, message: '不得超过50个字符', trigger: 'blur' }
  ],
})


/**
 * @returns {Array} 供应商发票申请子表
 */
export const SupplierinvoiceRequestDetail = useCrudSchemas(reactive<CrudSchema[]>([
  {
    label: '订单号',
    field: 'poNumber',
    form: {
      componentProps: {
        disabled: true
      }
    },
    table: {
      width: 150
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '订单行',
    field: 'poLine',
    table: {
      width: 150
    },
    form: {
      // labelMessage: '信息提示说明!!!',
      componentProps: {
        isSearchList: true,
        searchListPlaceholder: '请选择订单行',
        searchField: 'poLine',
        searchTitle: '待开票列表',
        searchAllSchemas: PurchaseReceiptOrReturnRecordDetail.allSchemas,
        searchPage: supplierinvoiceRequestDetailApi.getPoNumberPoLineInfo,
        searchCondition: [
          {
            key: 'supplierCode',
            value: 'supplierCode',
            message: '请填供应商信息!',
            isMainValue: true
          }
        ]
      }
    },
    tableForm: {
      multiple:true,//多选
      isInpuFocusShow: true,
      searchListPlaceholder: '请选择订单行',
      searchField: 'poLine',
      searchTitle: '待开票列表',
      searchAllSchemas: PurchaseReceiptOrReturnRecordDetail.allSchemas,
      searchPage: supplierinvoiceRequestDetailApi.getPoNumberPoLineInfo,
      searchCondition: [
        {
          key: 'supplierCode',
          value: 'supplierCode',
          message: '请填供应商信息!',
          isMainValue: true
        }
      ]
    }
  },
  {
    label: '供应商税率',
    field: 'tax',
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '项目编码',
    field: 'projectCode',
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '单据类型',
    field: 'billType',
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '发货单号',
    field: 'asnBillNum',
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '供应商代码',
    field: 'supplierCode',
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '单价',
    field: 'singlePrice',
    table: {
      width: 150
    },
    form: {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 6,
      }
    },
    tableForm: {
      type: 'InputNumber',
      min: 0,
      precision: 6,
    }
  },
  {
    label: '采购价格',
    field: 'purchasePrice',
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '差额',
    field: 'differencePrice',
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '可开票数量',
    field: 'invoicableQuantity',
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '物料代码',
    field: 'itemCode',
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '物料名称',
    field: 'itemName',
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '货币',
    field: 'currency',
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '备注',
    field: 'remark',
  },
  {
    label: '创建时间',
    field: 'createTime',
    isTable: true,
    table: {
      width: 150
    },
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    isTableForm: false,
    isForm: false
  },
  {
    label: '创建者',
    field: 'creator',
    isTableForm: false,
    isForm: false,
    table: {
      width: 150
    },
  },
  {
    label: '操作',
    field: 'action',
    isDetail: false,
    isForm: false,
    table: {
      width: 150,
      fixed: 'right'
    },
    isTableForm: false,
  }
]))

//表单校验
export const SupplierinvoiceRequestDetailRules = reactive({
  
})