import type { CrudSchema } from '@/hooks/web/useCrudSchemas'
import { dateFormatter,dateFormatter2 } from '@/utils/formatTime'
import { accountantFormart } from '@/utils/formatter'

import * as SupplierApi from '@/api/wms/supplier'
import * as SupplierinvoiceRequestMainApi from '@/api/wms/supplierinvoiceRequestMain'
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]

const procurementCreators = await SupplierinvoiceRequestMainApi.queryUserInfoByRoleCode({ roleCode: 'purchase',pageSize: 1000,pageNo: 1,sort: '',by: 'ASC' })
/**
 * @returns {Array} 采购员列表
 */
export const PurchaseMemberInfo = useCrudSchemas(reactive<CrudSchema[]>([
  {
    label: '用户编号',
    field: 'id',
    table: {
      width: 180,
    },
  },
  {
    label: '用户名称',
    field: 'username',
    table: {
      width: 180,
    },
  },
  {
    label: '用户昵称',
    field: 'nickname',
    table: {
      width: 180,
    },
  }
]))

/**
 * @returns {Array} 采购订单或者采购退货单
 */
export const PurchaseReceiptOrReturnRecordDetail = useCrudSchemas(reactive<CrudSchema[]>([
  {
    label: '单据类型',
    field: 'billType'
  },
  {
    label: '收货日期',
    field: 'deliveryDate',
    sort: 'custom',
    isDetail:false,
    isTable: true,
    isForm:false,
    isTableForm:false,
    isSearch:true,
    formatter: dateFormatter2,
    search: {
      component: 'DatePicker',
      componentProps: {
        valueFormat: 'YYYY-MM-DD',
        type: 'daterange',
        defaultTime: [new Date('1 '), new Date('1 ')]
      }
    },
  },
  {
    label: '待开票单据号',
    field: 'recvBillNum',
    table: {
      width: 180,
    },
  },
  {
    label: '供应商发货单号',
    field: 'asnBillNum',
    table: {
      width: 180,
    },
  },
  {
    label: '供应商代码',
    field: 'supplierCode'
  },
  {
    label: '订单号',
    field: 'poNumber'
  },
  {
    label: '订单行',
    field: 'poLine'
  },
  {
    label: '采购价格',
    field: 'purchasePrice'
  },
  {
    label: '可开票数量',
    field: 'invoicableQuantity'
  },
  {
    label: '货币',
    field: 'currency',
  },
  {
    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
    },
    sortTableDefault:1,
    isSearch: true,
    form: {
      // labelMessage: '信息提示说明!!!',
      componentProps: {    
        enterSearch:true,
        isSearchList: true, // 开启查询弹窗
        searchListPlaceholder: '请选择供应商代码', // 输入框占位文本
        searchField: 'code', // 查询弹窗赋值字段
        searchTitle: '供应商信息', // 查询弹窗标题
        searchAllSchemas: Supplier.allSchemas, // 查询弹窗所需类
        searchPage: SupplierApi.getSupplierPageSCP // 查询弹窗所需分页方法
      }
    }
  },
  {
    label: '订单类型',
    field: 'orderType',
    dictType: DICT_TYPE.PURCHASE_INVOICE_ORDER_TYPE,
    dictClass: 'string',
    isTable: false,
    sort: 'custom',
    table: {
      width: 150
    },
  },
  {
    label: '供应商名称',
    field: 'supplierName',
    sort: 'custom',
    table: {
      width: 180,
    },
    sortTableDefault:2,
    isSearch: true,
    isForm: false
  },
  {
    label: '状态',
    field: 'status',
    dictType: DICT_TYPE.SUPPLIERINVOICE_REQUEST_STATUS,
    dictClass: 'string',
    isTable: true,
    isForm:false,
    sort: 'custom',
    table: {
      width: 150
    },
    sortTableDefault:9,
    isSearch: true,
    search: {
      componentProps: {
        multiple: true,
        collapseTags: true,
        collapseTagsTooltip: true,
      }
    },
    form: {
      value: '1',
      componentProps: {
        disabled: true
      }
    }
  },
  //子表数据,仅是查询条件
  {
    label: '货运单号',
    field: 'asnBillNum',
    sort: 'custom',
    table: {
      width: 180,
      fixed: 'left'
    },
    isTableForm:false,
    isTable:false,
    isDetail:false,
    isSearch: true,
    isForm: false,
  },
  {
    label: '系统未税金额',
    field: 'amount',
    formatter: accountantFormart,
    table: {
      width: 150
    },
    sortTableDefault:8,
    form: {
      component: 'InputNumber',
      componentProps: {
        precision: 2,
        disabled: true,
      }
    },
    tableForm: {
      type: 'InputNumber',
      precision: 2,
    }
  },
  {
    label: '税率(%)',
    field: 'taxRate',
    dictType: DICT_TYPE.TAX_RATE_DICT,
    dictClass: 'string',
    sortTableDefault:3,
    isTable: true,
    sort: 'custom',
    table: {
      width: 150
    },
  },
  
  // {
  //   label: '未税金额',
  //   field: 'beforeTaxAmount',
  //   formatter: accountantFormart,
  //   table: {
  //     width: 150
  //   },
  //   sortTableDefault:3,
  //   isForm:false,
  //   isTableForm:false
  // },
  // {
  //   label: '税额',
  //   field: 'totalTaxAmount',
  //   formatter: accountantFormart,
  //   table: {
  //     width: 150
  //   },
  //   sortTableDefault:3,
  //   isForm:false,
  //   isTableForm:false
  // },
  // {
  //   label: '价税合计金额',
  //   field: 'adTaxAmount',
  //   formatter: accountantFormart,
  //   table: {
  //     width: 150
  //   },
  //   sortTableDefault:3,
  //   isForm:false,
  //   isTableForm:false
  // },
  {
    label: '系统税额',
    field: 'taxAmount',
    formatter: accountantFormart,
    table: {
      width: 150
    },
    sortTableDefault:6,
    form: {
      component: 'InputNumber',
      componentProps: {
        precision: 2,
        disabled: true,
      }
    },
    tableForm: {
      type: 'InputNumber',
      precision: 2,
    }
  },
  {
    label: '系统价税合计',
    field: 'afterTaxAmount',
    formatter: accountantFormart,
    table: {
      width: 150
    },
    sortTableDefault:7,
    form: {
      component: 'InputNumber',
      componentProps: {
        precision: 2,
        disabled: true,
      }
    },
    tableForm: {
      type: 'InputNumber',
      min: 0,
      precision: 2,
      disabled: true,
    }
  },
  {
    label: '索赔金额',
    field: 'claimAmount',
    formatter: accountantFormart,
    table: {
      width: 150
    },
    isTable:false,
    isTableForm:false,
    isForm:false,
    isDetail:false,
    form: {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 2,
      }
    },
    tableForm: {
      type: 'InputNumber',
      min: 0,
      precision: 2,
    }
  },
  // {
  //   label: '调整价差',
  //   field: 'discountAmount',
  //   formatter: accountantFormart,
  //   table: {
  //     width: 150
  //   },
  //   isTable:false,
  //   form: {
  //     component: 'InputNumber',
  //     componentProps: {
  //       precision: 2,
  //     }
  //   },
  //   tableForm: {
  //     type: 'InputNumber',
  //     min: 0,
  //     precision: 2,
  //   }
  // },
  // {
  //   label: '调整税额',
  //   field: 'adjustingTaxAmount',
  //   table: {
  //     width: 150
  //   },
  //   isTable:false,
  //   form: {
  //     component: 'InputNumber',
  //     componentProps: {
  //       precision: 2,
  //     }
  //   },
  //   tableForm: {
  //     type: 'InputNumber',
  //     min: 0,
  //     precision: 2,
  //   }
  // },
  {
    label: '折扣金额',
    field: 'discountAmount1',
    formatter: accountantFormart,
    table: {
      width: 150
    },
    isTable:false,
    form: {
      component: 'InputNumber',
      componentProps: {
        precision: 2,
      }
    },
    tableForm: {
      type: 'InputNumber',
      min: 0,
      precision: 2,
    }
  },
  {
    label: '未税金额',
    field: 'beforeTaxAmount',
    formatter: accountantFormart,
    table: {
      width: 150
    },
    sortTableDefault:3,
    form: {
      component: 'InputNumber',
      componentProps: {
        precision: 2,
        disabled: true,
      }
    },
    isTableForm:false,
  },
  {
    label: '税额',
    field: 'totalTaxAmount',
    formatter: accountantFormart,
    table: {
      width: 150
    },
    sortTableDefault:4,
    form: {
      component: 'InputNumber',
      componentProps: {
        precision: 2,
        disabled: true,
      }
    },
    isTableForm:false,
  },
  {
    label: '价税合计金额',
    field: 'adTaxAmount',
    formatter: accountantFormart,
    table: {
      width: 150
    },
    sortTableDefault:5,
    form: {
      component: 'InputNumber',
      componentProps: {
        precision: 2,
        disabled: true,
      }
    },
    isTableForm:false,
  },
  {
    label: '是否有价差',
    field: 'balanceStatementStatus',
    dictType: DICT_TYPE.INVOICE_BALANCE_STATEMENT_STATUS,
    dictClass: 'string',
    isTable: true,
    isForm:false,
    isSearch:false,
    sort: 'custom',
    table: {
      width: 150
    },
    sortTableDefault:5,
  },
  {
    label: '价差说明',
    field: 'balanceStatement',
    isTable: false,
    isForm:false,
    isSearch:false,
    sort: 'custom',
    table: {
      width: 150
    },
    sortTableDefault:5,
  },
  {
    label: '未税差额',
    field: 'beforeTaxDiffAmount',
    formatter: accountantFormart,
    table: {
      width: 150
    },
    sortTableDefault:5,
    form: {
      component: 'InputNumber',
      componentProps: {
        precision: 2,
        disabled: true,
      }
    },
    isTableForm:false,
  },
  {
    label: '税额差异',
    field: 'taxAmountDiff',
    formatter: accountantFormart,
    table: {
      width: 150
    },
    sortTableDefault:5,
    form: {
      component: 'InputNumber',
      componentProps: {
        precision: 2,
        disabled: true,
      }
    },
    isTableForm:false,
  },
  {
    label: '价税合计差额',
    field: 'totalTaxDiffAmount',
    formatter: accountantFormart,
    table: {
      width: 150
    },
    sortTableDefault:5,
    isTableForm:false,
    form: {
      component: 'InputNumber',
      componentProps: {
        precision: 2,
        disabled: true,
      }
    },
  },
  
  // {
  //   label: '总差额',
  //   field: 'totalDifference',
  //   formatter: accountantFormart,
  //   table: {
  //     width: 150
  //   },
  //   isTable:false,
  //   form: {
  //     component: 'InputNumber',
  //     componentProps: {
  //       precision: 2,
  //     }
  //   },
  //   tableForm: {
  //     type: 'InputNumber',
  //     min: 0,
  //     precision: 2,
  //   }
  // },
  {
    label: '金税票号',
    field: 'goldenTaxInvoiceNumber',
    table: {
      width: 200,
    },
    sortTableDefault:10,
    form:{
      componentProps: {
        maxlength:23,
        showWordLimit:true
      }
    }
  },
  {
    label: '快递单号',
    field: 'expressTrackingNumber',
    isTable:false,
    isForm:false,
    table: {
      width: 150
    },
  },
  {
    label: '开票日期',
    field: 'invoiceTime',
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    sort: 'custom',
    table: {
      width: 180
    },
    sortTableDefault:11,
    form: {
      component: 'DatePicker',
      componentProps: {
        style: {width:'100%'},
        type: 'datetime',
        dateFormat: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      }
    },
  },
  // {
  //   label: '采购审批人',
  //   field: 'procurementCreatorName',
  //   sort: 'custom',
  //   table: {
  //     width: 180
  //   },
  //   isTable:true,
  //   isDetail: true,
  //   isTableForm: false,
  //   isForm:false,
  //   isSearch:false,
  //   sortTableDefault:12
  // },
  {
    label: '过账日期',
    field: 'postingDate',
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    sort: 'custom',
    table: {
      width: 180
    },
    isTable:false,
    isDetail:false,
    form: {
      component: 'DatePicker',
      componentProps: {
        disabled: true,
        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
  //   },
  //   isTable:false,
  //   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
    },
    isTable:false,
    isTableForm:false,
    isForm:false,
    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',
    table: {
      width: 150
    },
    isTable:false,
    sortTableDefault:1001,
    isForm: false,
  },
  {
    label: '创建时间',
    field: 'createTime',
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    sort: 'custom',
    table: {
      width: 180
    },
    isForm: false,
    isDetail: false,
    isTableForm: false,
    isTable: true,
    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')]
      }
    },
  },
  {
    label: '差价范围',
    field: 'cha',
    sort: 'custom',
    table: {
      width: 180,
      fixed: 'left'
    },
    isTableForm:false,
    isTable:false,
    isDetail:false,
    isSearch: true,
    isForm: false,
  },
  {
    label: '供应商创建时间',
    field: 'requestTime',
    isTable:true,
    formatter: dateFormatter,
    sortTableDefault:1000,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    sort: 'custom',
    table: {
      width: 180
    },
    isForm: false,
    isSearch: true,
    form: {
      component: 'DatePicker',
      componentProps: {
        style: {width:'100%'},
        type: 'datetimerange',
        dateFormat: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      }
    },
    search: {
      value:[],
      component: 'DatePicker',
      componentProps: {
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        dateFormat: 'YYYY-MM-DD HH:mm:ss',
        type: 'datetimerange',
        defaultTime: [new Date('1 '), new Date('1 ')]
      }
    },
  },
  {
    label: '最后更新者',
    field: 'updater',
    sort: 'custom',
    table: {
      width: 150
    },
    isTable:false,
    isForm: false,
  },
  {
    label: '最后更新时间',
    field: 'updateTime',
    isTable: false,
    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: 'procurementCreator',
    table: {
      width: 150
    },
    sortTableDefault:14,
    isForm: true,
    isTable:false,
    isDetail:false,
    form: {
      component: 'Select',
      api: () => procurementCreators.list,
      componentProps: {
        optionsAlias: {
          labelField: 'nickname',
          valueField: 'id'
        }
      },
      // labelMessage: '信息提示说明!!!',
      // componentProps: {
      //   isSearchList: true, // 开启查询弹窗
      //   hiddenFilterButton:true,//是否隐藏筛选按钮
      //   dialogWidth:'665px',//搜索出来弹窗的宽度
      //   searchListPlaceholder: '请选择采购员', // 输入框占位文本
      //   searchField: 'id', // 查询弹窗赋值字段
      //   searchTitle: '采购员信息', // 查询弹窗标题
      //   searchAllSchemas: PurchaseMemberInfo.allSchemas, // 查询弹窗所需类
      //   searchPage: SupplierinvoiceRequestMainApi.queryUserInfoByRoleCode, // 查询弹窗所需分页方法
      //   searchCondition: [{
      //     key: 'roleCode', // 查询列表中字段
      //     value: 'purchase', // 指查询具体值
      //     isMainValue: false // 表示查询条件不是主表的字段的值
      //   }]
      // }
    }
  },
  {
    label: '采购价格审批人',
    field: 'procurementCreatorName',
    table: {
      width: 150
    },
    sortTableDefault:14,
    isForm: false,
    isTable:true,
    isDetail:false,
    form: {
      component: 'Select',
      api: () => procurementCreators.list,
      componentProps: {
        optionsAlias: {
          labelField: 'nickname',
          valueField: 'id'
        }
      },
    }
  },
  // {
  //   label: '采购审批人',
  //   field: 'procurementCreatorName',
  //   sort: 'custom',
  //   table: {
  //     width: 180
  //   },
  //   isTable:true,
  //   isDetail: true,
  //   isTableForm: false,
  //   isForm:false,
  //   isSearch:false,
  //   sortTableDefault:12
  // },
 
  {
    label: '采购审批时间',
    field: 'procurementCreateTime',
    isTable: true,
    sortTableDefault:13,
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    form:{
      component: 'DatePicker',
      componentProps: {
        style: {width:'100%'},
        type: 'datetime',
        dateFormat: 'YYYY-MM-DD HH:mm:ss',//YYYY-MM-DD
        valueFormat: 'x',//数据转成时间戳
      }
    },
    sort: 'custom',
    table: {
      width: 180
    },
    isForm: false,
  },
  {
    label: '财务审批人',
    field: 'financialCreatorName',
    sort: 'custom',
    table: {
      width: 150
    },
    sortTableDefault:14,
    isForm: false,
  },
  {
    label: '采购驳回原因',
    field: 'purchaseRejectCause',
    table: {
      width: 180
    },
    sortTableDefault:16,
    isTable: true,
    isSearch: false,
    isForm: false,
    isDetail:false,
  },
  {
    label: '财务驳回原因',
    field: 'financeRejectCause',
    table: {
      width: 180
    },
    sortTableDefault:17,
    isTable: true,
    isSearch: false,
    isForm: false,
    isDetail:false,
  },
  {
    label: '财务审批时间',
    field: 'financialCreateTime',
    isTable: true,
    sortTableDefault:15,
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    form:{
      component: 'DatePicker',
      componentProps: {
        style: {width:'100%'},
        type: 'datetime',
        dateFormat: 'YYYY-MM-DD HH:mm:ss',//YYYY-MM-DD
        valueFormat: 'x',//数据转成时间戳
      }
    },
    sort: 'custom',
    table: {
      width: 180
    },
    isForm: false,
  },
  {
    label: '供应商审批人',
    field: 'supplierCreator',
    sort: 'custom',
    isTable:false,
    isDetail:false,
    sortTableDefault:1006,
    table: {
      width: 180
    },
    isForm: false,
  },
  {
    label: '供应商审批人',
    field: 'supplierCreatorName',
    sort: 'custom',
    isTable:false,
    isDetail:true,
    sortTableDefault:1006,
    table: {
      width: 180
    },
    isForm: false,
  },
  {
    label: '供应商审批时间',
    field: 'supplierCreateTime',
    isTable:false,
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    form:{
      component: 'DatePicker',
      componentProps: {
        style: {width:'100%'},
        type: 'datetime',
        dateFormat: 'YYYY-MM-DD HH:mm:ss',//YYYY-MM-DD
        valueFormat: 'x',//数据转成时间戳
      }
    },
    sort: 'custom',
    table: {
      width: 180
    },
    isForm: false,
  },
  {
    label: '备注',
    field: 'remark',
    sort: 'custom',
    table: {
      width: 150
    },
    isForm:false,
    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,
      }
    }
  }
]))
/**
 * @returns {Array} 供应商发票在详情展示的主表字段
 */
export const SupplierinvoiceRequestDetailMain = useCrudSchemas(reactive<CrudSchema[]>([
  {
    label: '汇总信息',
    field: '',
    sort: 'custom',
    isForm: false,
    isSearch: false,
    isTable: false,
    isDetail: true,
  },
  {
    label: '供应商信息',
    field: '',
    sort: 'custom',
    isForm: false,
    isSearch: false,
    isTable: false,
    isDetail: true,
  },
  {
    label: 'SCP系统数据',
    field: '',
    sort: 'custom',
    isForm: false,
    isSearch: false,
    isTable: false,
    isDetail: true,
  },

  {
    label: '单据号',
    field: 'number',
    sort: 'custom',
    
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '供应商代码',
    field: 'supplierCode',
    sort: 'custom',
    
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '系统价税合计',
    field: 'afterTaxAmount',
    formatter: accountantFormart,
    
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '财务凭证号',
    field: 'voucherNumber',
   
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '供应商名称',
    field: 'supplierName',
    sort: 'custom',
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '系统未税金额',
    field: 'amount',
    formatter: accountantFormart,
    
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '供应商创建时间',
    field: 'requestTime',
    sort: 'custom',
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '订单类型',
    field: 'orderType',
    dictType: DICT_TYPE.PURCHASE_INVOICE_ORDER_TYPE,
    dictClass: 'string',
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '系统税额',
    field: 'taxAmount',
    formatter: accountantFormart,
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '状态',
    field: 'status',
    dictType: DICT_TYPE.SUPPLIERINVOICE_REQUEST_STATUS,
    dictClass: 'string',
    sort: 'custom',
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '发票号码',
    field: 'goldenTaxInvoiceNumber',
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '系统采购价格',
    field: 'purchaseAmountTotalMain',
    formatter: accountantFormart,
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '采购价格审批人',
    field: 'procurementCreatorName',
    sortTableDefault: 1003,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '开票日期',
    field: 'invoiceTime',
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '总价差',
    field: 'differencePriceTotalMain',
    formatter: accountantFormart,
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '采购价格审批时间',
    field: 'procurementCreateTime',
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    sortTableDefault: 1003,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '税率(%)',
    field: 'taxRate',
    dictType: DICT_TYPE.TAX_RATE_DICT,
    dictClass: 'string',
    sortTableDefault: 3,
    isTable: true,
    sort: 'custom',
    table: {
      width: 150
    },
  },
  {
    label: '',
    field: '',
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '价差通过说明',
    field: 'balanceStatement',
    sort: 'custom',
    
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '价税合计',
    field: 'adTaxAmount',
    formatter: accountantFormart,
    
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '',
    field: '',
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },

  {
    label: '财务审批人',
    field: 'financialCreatorName',
    sort: 'custom',
    
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '未税金额',
    field: 'beforeTaxAmount',
    formatter: accountantFormart,
    
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '尾差',
    field: 'totalDifference',
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '财务审批时间',
    field: 'financialCreateTime',
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '税额',
    field: 'totalTaxAmount',
    formatter: accountantFormart,
    
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '价税合计差额(价税合计-系统价税合计)',
    field: 'totalTaxDiffAmount',
    formatter: accountantFormart,
    
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '财务过账日期',
    field: 'postingDate',
    isTable: true,
    sortTableDefault: 15,
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
   
  },
  {
    label: '折扣金额',
    field: 'discountAmount1',
    formatter: accountantFormart,
   
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '未税差额(未税金额-系统未税金额)',
    field: 'beforeTaxDiffAmount',
    formatter: accountantFormart,
    
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '备注',
    field: 'remark',
    sort: 'custom',
   
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
    // detail:{
    //   span:2
    // }
  },
  {
    label: '工厂地点',
    field: 'siteId',
    sort: 'custom',
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  {
    label: '税额差异(税额-系统税额)',
    field: 'taxAmountDiff',
    formatter: accountantFormart,
    
    sortTableDefault: 6,
    isForm: false,
    isTable: false,
    isSearch: false,
    isDetail: true,
  },
  // {
  //   label: '创建时间',
  //   field: 'createTime',
  //   formatter: dateFormatter,
  //   sortTableDefault:1000,
  //   detail: {
  //     dateFormat: 'YYYY-MM-DD HH:mm:ss'
  //   },
  //   isForm: false,
  //   isTable: false,
  //   isSearch: false,
  //   isDetail: true,
  // },

]))
const singlePriceFormart = (row, column, cellValue) => {
  cellValue= Number(cellValue).toFixed(4)
  cellValue = cellValue + '' || ''

  let x = cellValue.split('.')
  let x1 = x[0]
  let x2  = x.length > 1 ? '.' + x[1] : ''
  const reg = /(\d+)(\d{3})/
  while(reg.test(x1)){
    x1 = x1.replace(reg, '$1,$2')
  }
  return x1+x2
}

function validateTaxRate(rule, value, callback) {
  if (value>0) {
    callback()
  }else{
    callback(new Error('税率必须大于0'))
  }
}

//表单校验
export const SupplierinvoiceRequestMainRules = reactive({
  taxRate: [
    required,
    // { validator:validateTaxRate, message: '税率必须大于0', trigger: 'blur'}
  ],
  supplierCode: [
    { required: true, message: '请选择供应商代码', trigger: 'change' }
  ],
  invoiceTime:[
    { required: false, message: '请选择发票日期', trigger: 'blur' }
  ],
  goldenTaxInvoiceNumber:[
    { required: false, message: '请输入金税票号', trigger: 'blur' }
  ],
  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' }
  ],
  procurementCreator: [
    { required: true, message: '请选择采购员', trigger: 'change' }
  ],
  beforeTaxAmount: [
    { required: true, message: '请输入未税金额', trigger: 'change' }
  ],
  totalTaxAmount: [
    { required: true, message: '请输入税额', trigger: 'change' }
  ],
})

export const SupplierinvoiceRequestFinance = useCrudSchemas(reactive<CrudSchema[]>([
  {
    label: '过账日期',
    field: 'postingDate',
    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',
      }
    },
  },
  {
    label: '凭证描述',
    field: 'voucherNumberRemark',
    sort: 'custom',
    isTable:false,
    form: {
      component: 'Input',
      componentProps:{
        type: 'textarea'
      }
    },
  },
]))

export const SupplierinvoiceRequestFinanceRules = reactive({
  postingDate: [
    { required: true, message: '请选择过账日期', trigger: 'change' }
  ],
  // voucherNumberRemark:  [
  //   { max: 50, message: '不得超过40个字符', trigger: 'blur' }
  // ],
})
/**
 * @returns {Array} 供应商发票申请子表
 */
export const SupplierinvoiceRequestDetail = useCrudSchemas(reactive<CrudSchema[]>([
  {
    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: 'itemDesc',
    table: {
      width: 150
    },
    isForm:false,
    isTableForm:false,
    isDetail: false,
    isTable: true,
  },
  
   //仅是主列表页面的筛选搜索条件
   {
    label: '收货日期',
    field: 'deliveryDate',
    sort: 'custom',
    isDetail:false,
    isTable: false,
    isForm:false,
    isTableForm:false,
    isSearch:true,
    formatter: dateFormatter,
    search: {
      value:[],
      component: 'DatePicker',
      componentProps: {
        valueFormat: 'YYYY-MM-DD',
        type: 'daterange',
        defaultTime: [new Date('1 '), new Date('1 ')]
      }
    },
  },
  {
    label: '收货日期',
    field: 'deliveryDate',
    table: {
      width: 150
    },
    formatter: dateFormatter2,
    isForm:false,
    isTableForm:true,
    isDetail: false,
    isTable: true,
    tableForm:{
      disabled:true
    }
  },
  {
    label: '可开票数量',
    field: 'invoicableQuantity',
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '合同价格',
    field: 'singlePrice',
    formatter: singlePriceFormart,
    table: {
      width: 150
    },
    form: {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        precision: 2,
      }
    },
    tableForm: {
      type: 'InputNumber',
      min: 0,
      precision: 2,
    }
  },
  {
    label: '采购价格',
    field: 'purchasePrice',
    formatter: singlePriceFormart,
    table: {
      width: 150
    },
    form: {
      component: 'InputNumber',
      componentProps: {
        disabled: true,
        min: 0,
        precision: 4,
      }
    },
    tableForm:{
      type: 'InputNumber',
      disabled: true,
      min: 0,
      precision: 4,
    }
  },
  {
    label: '合同金额',
    field: 'contractAmountTotal',
    table: {
      width: 150
    },
    isForm:false,
    tableForm:{
      disabled: true
    }
  },
  {
    label: '采购金额',
    field: 'purchaseAmontTotal',
    table: {
      width: 150
    },
    isForm:false,
    tableForm:{
      disabled: true
    }
  },
  {
    label: '总差额',
    field: 'differencePriceTotal',
    table: {
      width: 150
    },
    isForm:false,
    tableForm:{
      disabled: true
    }
  },
  {
    label: '单价差额',
    field: 'differencePrice',
    formatter: singlePriceFormart,
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true,
        min: 0,
        precision: 4,
      }
    },
    tableForm:{
      type: 'slot',
      disabled: true,
      min: 0,
      precision: 4,
    }
  },
  {
    label: '到货数量',
    field: 'arrivalQty',
    table: {
      width: 150
    },
    isForm:false,
    isTableForm:true,
    isDetail: false,
    isTable: true,
    tableForm:{
      disabled:true
    }
  },
  {
    label: '订单号',
    field: 'poNumber',
    form: {
      componentProps: {
        disabled: true
      }
    },
    table: {
      width: 150
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '订单行',
    field: 'poLine',
    table: {
      width: 150
    },
    form: {
      // labelMessage: '信息提示说明!!!',
      componentProps: {
        multiple:true,//多选
        isSearchList: true,
        searchListPlaceholder: '请选择订单行',
        searchField: 'poLine',
        searchTitle: '待开票列表',
        searchAllSchemas: PurchaseReceiptOrReturnRecordDetail.allSchemas,
        searchPage: supplierinvoiceRequestDetailApi.getPoNumberPoLineInfo,
        searchCondition: [
          {
            key: 'supplierCode',
            value: 'supplierCode',
            message: '请填供应商信息!',
            isMainValue: true
          },{
            key: 'orderType',
            value: 'orderType',
            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
        },
        {
          key: 'orderType',
          value: 'orderType',
          message: '请填订单类型!',
          isMainValue: true
        }
      ]
    }
  },
  {
    label: '待开票单据号',
    field: 'recvBillNum',
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      disabled: true
    }
  },
  {
    label: '单据类型',
    field: 'billType',
    dictType: DICT_TYPE.BILL_TYPE,
    dictClass: 'string',
    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',
    hiddenInMain:true,
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      disabled: true
    }
  },
  
  // {
  //   label: '未税差额',//子表数据
  //   field: 'untaxedDifference',
  //   formatter: accountantFormart,
  //   table: {
  //     width: 150
  //   },
  //   form: {
  //     component: 'InputNumber',
  //     componentProps: {
  //       precision: 2,
  //     }
  //   },
  //   tableForm: {
  //     type: 'InputNumber',
  //     min: 0,
  //     precision: 2,
  //   }
  // },
  // {
  //   label: '含税差额',
  //   field: 'taxInclusiveDifference',
  //   formatter: accountantFormart,
  //   table: {
  //     width: 150
  //   },
  //   form: {
  //     component: 'InputNumber',
  //     componentProps: {
  //       precision: 2,
  //     }
  //   },
  //   tableForm: {
  //     type: 'InputNumber',
  //     min: 0,
  //     precision: 2,
  //   }
  // },
  
  {
    label: '货币',
    field: 'currency',
    dictType: DICT_TYPE.CURRENCY,
    dictClass: 'string',
    isTableForm:true,
    table: {
      width: 150
    },
    form: {
      componentProps: {
        disabled: true
      }
    },
    tableForm:{
      type:'Select',
      disabled: true
    }
  },
  {
    label: '备注',
    field: 'remark',
    hiddenInMain:true,
  },
  {
    label: '创建时间',
    field: 'createTime',
    hiddenInMain:true,
    isTable: false,
    table: {
      width: 150
    },
    formatter: dateFormatter,
    detail: {
      dateFormat: 'YYYY-MM-DD HH:mm:ss'
    },
    isTableForm: false,
    isForm: false
  },
  {
    label: '创建者',
    field: 'creator',
    hiddenInMain:true,
    isTableForm: false,
    isTable:false,
    isForm: false,
    table: {
      width: 150
    },
  },
  {
    label: '操作',
    field: 'action',
    isDetail: false,
    isForm: false,
    table: {
      width: 250,
      fixed: 'right'
    },
    hiddenInMain:true,
    isTableForm: false,
  }
]))
// 批次校验
const validateSinglePrice = (rule: any, value: any, callback: any) => {
  if (Number(value)>0) {
      callback();
  } else {
    callback(new Error('请输入合同价格'));
  }
}
//表单校验
export const SupplierinvoiceRequestDetailRules = reactive({
  singlePrice: [
    { required: true, message: '请输入合同价格', trigger: 'change' },
    { validator: validateSinglePrice, trigger: 'change' }
  ],
})
// 采购通过
export const PurchasePass = useCrudSchemas(reactive<CrudSchema[]>([
  {
    label: '价差通过说明',
    field: 'balanceStatement',
    sort: 'custom',
    table: {
      width: 180
    },
    isTable:false,
    form: {
      componentProps:{
        type:'textarea'
      }
    },
  },
]))
//表单校验
export const PurchasePassRules = reactive({
  balanceStatement: [
    { required: true, message: '请输入价差通过说明', trigger: 'blur'},
    { max: 100, message: '最多100字符', trigger: 'blur'}
  ],
})