import { ContentConfig } from "/@/components/PageContent";
import { httpList, httpDelete } from "/@/api/purchase/ticketReturn";
import { ElTag } from "element-plus";
import dayjs from "dayjs";
import { h } from "vue";

import {
  invoiceTypeList,
  seller_check_type,
  buyer_check_type,
  ticketReturnStatusOptions,
  checkTypeOptions,
  hasAccountOptions
} from "/@/utils/status";
import { renderIconLabelLeft, renderProp } from "/@/utils/columnRenderHelper";

const columns = [
  {
    type: "selection",
    width: 40,
    align: "center",
    hide: ({ checkList }) => !checkList.includes("勾选列")
  },
  {
    label: "序号",
    type: "index",
    width: 70,
    hide: ({ checkList }) => !checkList.includes("序号列")
  },
  {
    label: "对账回票申请",
    prop: "hpNo",
    width: 140
  },
  {
    label: "对账编码",
    prop: "payNo",
    width: 150
  },
  {
    label: "申请人",
    prop: "apply_name"
  },
  // {
  //   label: "卖方公司编码",
  //   prop: "supplierNo",
  //   width: 150
  // },
  {
    label: "卖出方公司",
    prop: "supplierName",
    width: 145,
    ...renderIconLabelLeft("supplierNo","supplierName","卖出方公司编码:")
  },
  {
    label: "发票税前金额",
    width: "110px",
    prop: "inv_subtotal_amount"
  },
  {
    label: "发票税后金额",
    width: "110px",
    prop: "total"
  },
  {
    label: "供应商端",
    prop: "has_account",
    width: 80,
    ...renderProp(hasAccountOptions, "has_account")
  },
  {
    label: "状态",
    prop: "status",
    width: 120,
    cellRenderer: ({ row, props }) =>
      h(
        ElTag,
        {
          size: props.size
        },
        {
          default: () =>
            ticketReturnStatusOptions.find(
              s => String(row.status) === String(s.value)
            )?.label
        }
      )
  },
  {
    label: "发票类型",
    width: 185,
    cellRenderer({ row }) {
      return h(ElTag, null, {
        default: () =>
          invoiceTypeList.find(({ value }) => value === row.invoiceType)
            ?.label || "--"
      });
    }
  },
  {
    label: "发票状态",
    width: 70,
    prop: "invStatus_cn"
  },
  // {
  //   label: "发票上传方式",
  //   prop: "invType",
  //   width: 120,
  //   cellRenderer: ({ row, props }) =>
  //     h(
  //       ElTag,
  //       {
  //         size: props.size
  //       },
  //       {
  //         default: () =>
  //           invTypeOptions.find(s => String(row.invType) === String(s.value))
  //             ?.label
  //       }
  //     )
  // },
  {
    label: "发票查验方式",
    width: 100,
    cellRenderer: ({ row, props }) =>
      h(
        ElTag,
        {
          size: props.size
        },
        {
          default: () =>
            checkTypeOptions.find(s => String(row.checkApi) === s.value)?.label
        }
      )
  },
  {
    label: "发票号码",
    width: 110,
    prop: "invoiceNumber"
  },
  {
    label: "发票代码",
    width: 110,
    prop: "invoiceCode"
  },
  {
    label: "开票日期",
    prop: "open_time",
    width: 90,
    formatter: ({ open_time }) =>
      open_time ? dayjs(open_time).format("YYYY-MM-DD") : ""
  },
  {
    label: "购买方抬头状态",
    prop: "seller_check",
    width: 130,
    cellRenderer: ({ row }) => {
      return h(ElTag, null, {
        default: () =>
          seller_check_type.find(item => item.value == row.seller_check + "")
            ?.label || "--"
      });
    }
  },
  {
    label: "销售方抬头状态",
    prop: "buyer_check",
    width: 130,
    cellRenderer: ({ row }) => {
      return h(ElTag, null, {
        default: () =>
          buyer_check_type.find(item => item.value == row.buyer_check + "")
            ?.label || "--"
      });
    }
  },
  // {
  //   label: "买方公司编码",
  //   prop: "companyNo",
  //   width: 150
  // },
  {
    label: "买入方公司",
    prop: "companyName",
    width: 145,
    ...renderIconLabelLeft('companyNo','companyName','买入方公司编码:')
  },
  {
    label: "创建时间",
    prop: "addtime",
    width: 150,
    formatter: ({ addtime }) => dayjs(addtime).format("YYYY-MM-DD HH:mm:ss")
  },
  {
    label: "操作",
    fixed: "right",
    slot: "operation",
    width: 110
  }
];

const contentConfig: ContentConfig = {
  title: "采购回票",
  statusProp: "companyNo",
  columns,
  apis: {
    httpDelete,
    httpList,
    httpAdd: true
  },
  deleteProp: "hpNo",
  delTooltip: "是否撤回采购回票申请",
  superUserNoAction: true,
  showDelete: ({ status }) => Number(status) <= 3
};

export default contentConfig;
