import { httpList, httpDel } from "/@/api/purchase/orderRecord";
import { ContentConfig } from "/@/components/PageContent";

import { statusOptions, payStatusOptions, invStatusOptions } from "./_options";
import { renderIconLabelLeft, renderProp } from "/@/utils/columnRenderHelper";
import { hasAccountOptions, orderTypeCgOptions, orderTypeOptions } from "/@/utils/status";

import { ElMessage } from "element-plus"



import { GET_PAYMENT_DATE, getIsBeforeUpdateDate, getDateName } from "/@/utils/constants" 

const columns = [
  {
    type: "selection",
    width: 40,
    align: "center",
    fixed: 'left'
  },
  {
    label: "对账编码",
    prop: "payNo",
    width: 150,
    align: "left"
  },

  {
    label: "对账总额",
    prop: "total_fee",
    width: 110,
    align: "total_fee"
  },

  {
    label: '业务类型',
    width: 90,
    ...renderProp(orderTypeCgOptions, 'is_comon')
  },
  {
    label: "对账单状态",
    prop: "status",
    width: 140,
    transform: status =>
      statusOptions.find(s => status === s.value)?.label || "--",
    ...renderProp(statusOptions)
  },
  {
    label: '付款状态',
    prop: 'pay_status',
    width: 80,
    transform: pay_status =>
      payStatusOptions.find(s => pay_status === s.value)?.label || "--",
    ...renderProp(payStatusOptions, 'pay_status')
  },
  {
    label: '回票状态',
    prop: 'inv_status',
    width: 80,
    transform: inv_status =>
      payStatusOptions.find(s => inv_status === s.value)?.label || "--",
    ...renderProp(invStatusOptions, 'inv_status')
  },
  {
    label: '采购单标签金额合计',
    width: '160px',
    children: [
      {
        label: '付款',
        prop: 'cgd_pay_tag_total'
      },
      {
        label: '回票',
        prop: 'cgd_inv_tag_total'
      }
    ]
  },
  {
    label: '对账单标签金额',
    width: '160px',
    children: [
      {
        label: '付款',
        prop: 'pay_tag_fee'
      },
      {
        label: '回票',
        prop: 'inv_tag_fee'
      }
    ]
  },
  // {
  //   label: '付款标签金额',
  //   prop: 'pay_tag_fee',
  //   width: '100px'
  // },
  // {
  //   label: '采购单付款标签金额合计',
  //   prop: 'cgd_pay_tag_total',
  //   width: '160px'
  // },
  // {
  //   label: '汇票标签金额',
  //   prop: 'inv_tag_fee',
  //   width: '100px'
  // },
  // {
  //   label: '采购单回票标签金额合计',
  //   prop: 'cgd_inv_tag_total',
  //   width: '160px'
  // },
  // {
  //   label: "供应商端",
  //   prop: "has_account",
  //   width: 80,
  //   ...renderProp(hasAccountOptions, "has_account")
  // },
  {
    label: "卖出方公司",
    prop: "supplierName",
    minWidth: 160,
    align: "left",
    ...renderIconLabelLeft('supplierNo','supplierName','卖出方公司:')
  },
  {
    label: "买入方公司",
    prop: "companyName",
    minWidth: 180,
    align: "left",
    ...renderIconLabelLeft('companyNo', 'companyName', '买入方公司:')
  },

  {
    label: "申请人",
    prop: "apply_name",
    width: 70,
    align: "left"
  },
  {
    label: "申请时间",
    width: 140,
    prop: "addtime"
  },
  {
    label: "操作",
    fixed: "right",
    width: 205,
    slot: "operation"
  }
];

function callback(row: any){
  // 1. 非通用订单(判断日期前后)
  if(row.is_comon != '1'){
    const isBeforeUpdateDate = getIsBeforeUpdateDate(GET_PAYMENT_DATE(row.payNo))
    if(!isBeforeUpdateDate){
      ElMessage.warning('采购订单或源头订单创建时间在' + getDateName() + '后的不允许添加标签')
      return false
    }
  }

  return true
}


const contentConfig: ContentConfig = {
  title: "订单对账",
  columns,
  apis: {
    httpList,
    httpAdd: true,
    httpDelete: httpDel
  },
  superUserNoAction: true,
  showDelete: () => false,
  payProps: {
    prop: "payNo",
    type: "1",
    callback
  },
  returnProps: {
    prop: "payNo",
    type: "2",
    callback
  },
  showPayTagFn: ({ status, pay_status, is_comon }, permissions) => String(status) === "2" && String(pay_status) !== '3' && permissions.includes("013"),
  showReturnTagFn: ({ status,inv_status, is_comon }, permissions) => String(status) === "2" && String(inv_status) !== '3' && permissions.includes("014"),
  showUnPayTagFn: ({ status, is_comon }, permissions) => String(status) === "2" && permissions.includes("055") ,
  showUnReturnTagFn: ({ status, is_comon }, permissions) => String(status) === "2" && permissions.includes("056")
};

export default contentConfig;
