import { httpList } from "/@/api/purchase/porder";
import { ContentConfig } from "/@/components/PageContent";
import { renderIconLabelLeft, renderProp } from "/@/utils/columnRenderHelper";

import {
  cgdStatusOptions,
  cg_order_source_options,
  cg_order_type_options,
  hasAccountOptions,
  sendStatusOptions,
  send_status_list,
  orderTypeCgOptions
} from "/@/utils/status";

import { GET_PAYMENT_DATE, getIsBeforeUpdateDate, getDateName } from "/@/utils/constants" 
import { ElMessage } from "element-plus"

const columns = [
  {
    width: 40,
    type: "selection",
    align: "center",
    fixed: "left"
  },
  {
    label: "采购单编码",
    prop: "sequenceNo",
    width: "155px"
  },
  {
    label: "采购主单编码",
    prop: "cxCode",
    width: "150px"
  },
  {
    label: "对账编号",
    prop: "payNo",
    width: "150px"
  },
  {
    label: "原采购单号",
    prop: "mainCode",
    width: "150px"
  },
  {
    label: "销售订单编码",
    prop: "qrdCode",
    width: "155px"
  },
  {
    label: "回票标签金额",
    prop: "inv_tag_fee",
    width: 100
  },
  {
    label: "付款标签金额",
    prop: "pay_tag_fee",
    width: 100
  },
  {
    label: "供应商端",
    prop: "has_account",
    width: 80,
    ...renderProp(hasAccountOptions, "has_account")
  },
  {
    label: "状态",
    prop: "status",
    width: 95,
    ...renderProp(cgdStatusOptions, "status")
  },
  {
    label: "销售单发货状态",
    prop: "qrdSend",
    width: 105,
    ...renderProp(send_status_list, "qrdSend")
  },
  {
    label: "采购单状态",
    prop: "sendStatus",
    width: 120,
    ...renderProp(sendStatusOptions, "cgd_status")
  },
  {
    label: '业务类型',
    width: 95,
    ...renderProp(orderTypeCgOptions, 'is_comon')
  },
  {
    label: "订单来源",
    prop: "cgdSource",
    width: 95,
    ...renderProp(cg_order_source_options, "cgdSource")
  },
  {
    label: "商品来源",
    prop: "cgdType",
    width: 95,
    ...renderProp(cg_order_type_options, "cgdType")
  },
  {
    label: "支付方式",
    prop: "pay_type",
    width: 95
  },
  {
    label: "商品编码",
    prop: "goodNo",
    width: "150px"
  },
  {
    label: "商品名称",
    prop: "goodName",
    width: "180px"
  },
  {
    label: "税率",
    prop: "tax",
    width: "45px"
  },
  {
    label: "商品数量",
    prop: "goodNum",
    width: "80px"
  },
  {
    label: "商品单价",
    prop: "goodPrice",
    width: "80px"
  },

  {
    label: "商品总价",
    prop: "totalPrice",
    width: "80px"
  },
  {
    label: "卖出方公司编码",
    prop: "supplierNo",
    width: "150px",
    ...renderIconLabelLeft("supplierNo", "supplierName", "卖出公司编码:")
  },
  {
    label: "备库单编码",
    prop: "bkCode",
    width: "150px"
  },
  {
    label: "采购员",
    prop: "ownerName",
    width: 75
  },

  {
    label: "买入方公司",
    prop: "companyName",
    width: "160px",
    ...renderIconLabelLeft("companyNo", "companyName", "买入公司编码:")
  },
  // {
  //   label: "买入方公司编码",
  //   prop: "companyNo",
  //   width: "150px"
  // },
  {
    label: "创建时间",
    prop: "addtime",
    width: 155
  },
  {
    label: "操作",
    fixed: "right",
    slot: "operation",
    width: 70
  }
];


function callback(row: any){
  // 1.通用订单(不能添加)
  if(row.is_comon == '1'){
    ElMessage.warning('通用订单不允许添加标签')
    return false
  }

  // 2.其他订单(区分有无对账单)
  if(row.payNo){
    // 2-1.有对账单(判断对账单创建日期在全局日期的前后)
    // 2-1-1. 对账单日期在全局日期前的(不能添加)
    const isBeforeUpdateDate = getIsBeforeUpdateDate(GET_PAYMENT_DATE(row.payNo))
    if(isBeforeUpdateDate){
      ElMessage.warning('对账单创建时间在' + getDateName() + '前的不允许添加标签')
      return false
    }
  }
  return true
}


const contentConfig: ContentConfig = {
  title: "采购单管理",
  columns,
  companyProp: "supplierNo",
  listNoRelation: true,
  notReuqiredInit: true,
  payProps: {
    prop: "sequenceNo",
    type: "5",
    callback
  },
  returnProps: {
    prop: "sequenceNo",
    type: "6",
    callback
  },
  // 付款标签
  showPayTagFn: ({ status, is_comon }, permissions) => permissions.includes("013"),
  // 回票标签
  showReturnTagFn: ({ status, is_comon }, permissions) => permissions.includes("014"),
  // 解除付款标签
  showUnPayTagFn: ({ status, is_comon }, permissions) => permissions.includes("055"),
  // 解除回票标签
  showUnReturnTagFn: ({ status, is_comon }, permissions) => permissions.includes("056"),
  apis: {
    httpList,
    httpAdd: true
  }
};

export default contentConfig;
