import { ContentConfig } from "/@/components/PageContent";
import { httpList } from "/@/api/invoiceInOut/inOutManager";
import { h } from "vue";
import { ElTag } from "element-plus";
import { typeOptions, sourceOptions, statusOptions, managerStatusOptions } from "./shared";
import { xs_inv_type_list } from "/@/utils/status";

const columns = [
  { 
    label: "业务编号", 
    prop: "invoiceCode", 
    width: 150 
  },

  { 
    label: "类型", 
    width: 80,
    cellRenderer({ row }){
      return h(ElTag, { type: 'primary', size: 'small' }, {
        default: () => typeOptions.find(item => item.value == row.type)?.label
      })
    } 
  },

  { 
    label: '数据来源', 
    width: 90,
    cellRenderer({ row }){
      return h(ElTag, { type: 'primary', size: 'small' }, {
        default: () => sourceOptions.find(item => item.value == row.source)?.label
      })
    } 
  },


  { 
    label: "状态", 
    width: 90,
    cellRenderer({ row }) {
      return h(
        ElTag, 
          { type: statusOptions.find(item => item.value == row.status)?.type, size: 'small' }, 
          { default: () => statusOptions.find(item => item.value == row.status)?.label }
      )
    } 
  },
  { 
    label: '异常原因', 
    width: 120,
    prop: 'error_remark'
  },
  { 
    label: '销售方公司纳税识别号', 
    prop: 'seller_code', 
    width: 150 
  },

  { 
    label: '销售方公司', 
    prop: 'seller_name', 
    width: 150 
  },

  { 
    label: '订单编号', 
    prop: 'orderCode', 
    width: 160 
  },
  
  { 
    label: "订单主编号", 
    prop: "cxCode", 
    width: 160 
  },
  
  { 
    label: "商品编号", 
    prop: "goodNo", 
    width: 120 
  },

  { 
    label: "商品名称", 
    prop: "goodName", 
    width: 120 
  },

  { 
    label: "单位", 
    prop: "unit", 
    width: 60 
  },

  { 
    label: '商品数量', 
    prop: 'num', 
    width: 100 
  },
  
  { 
    label: '商品单价', 
    prop: 'goodPrice', 
    width: 100 
  },
  
  { 
    label: '订单总金额', 
    prop: 'totalPrice', 
    width: 120 
  },

  { 
    label: '税目', 
    prop: 'cat_code', 
    width: 120 
  },
  
  { 
    label: '税率', 
    width: 60,
    cellRenderer({ row }){
      return (row.tax * 100) + '%'
    } 
  },

  { 
    label: '关联金额', 
    prop: 'inv_fee', 
    width: 120 
  },

  { 
    label: '购买方公司名称', 
    prop: 'buyer_name',
    width: 120 
  },
  
  { 
    label: '购买方公司纳税号', 
    prop: 'buyer_code', 
    width: 120 
  },
  
  { 
    label: '发票号码', 
    prop: 'inv_number', 
    width: 100 
  },
  
  { 
    label: '发票类型', 
    width: 180,
    cellRenderer({ row }){
      return xs_inv_type_list.find((item) => item.value === row.inv_type)?.label
    }
  },

  
  { 
    label: '销售方公司', 
    prop: 'inv_seller_code', 
    width: 180 
  },
  
  { 
    label: '销售方公司纳税号', 
    prop: 'inv_seller_name', 
    width: 180 
  },

  { 
    label: '发票明细ID', 
    prop: 'inv_item_id', 
    width: 100 
  },

  { 
    label: '货物或应税劳务、服务名称', 
    prop: 'inv_good_name', 
    width: 170 
  },

  { 
    label: '类目编号', 
    prop: 'cat_code', 
    width: 100 
  },
  
  { 
    label: '规格型号', 
    prop: 'inv_spec', 
    width: 80 
  },
  
  { 
    label: '发票商品单位', 
    prop: 'inv_unit', 
    width: 100 
  },
  
  { 
    label: '发票商品数量', 
    prop: 'inv_num', 
    width: 100 
  },

  { 
    label: '税前单价', 
    prop: 'inv_subprice', 
    width: 100 
  },
  
  { 
    label: '税前总价', 
    prop: 'inv_subtotal', 
    width: 100 
  },
  
  { 
    label: '发票税率', 
    prop: 'inv_tax', 
    width: 80 
  },
  
  { 
    label: '发票税额', 
    prop: 'inv_tax_total', 
    width: 80 
  },
  
  { 
    label: '税后单价', 
    prop: 'inv_price', 
    width: 100 
  },
  
  { 
    label: '税后总额', 
    prop: 'inv_total', 
    width: 100 
  },
  
  { 
    label: '类目编号状态', 
    width: 100,
    cellRenderer({ row }){
      return h(ElTag, { size: 'small', type: row.cat_diff == '0' ? 'danger' : 'success' }, {
        default: () => row.cat_diff == '0' ? '不一致' : '一致'
      })
    }
  },

  { 
    label: '税率状态', 
    prop: 'TODO', 
    width: 100,
    cellRenderer({ row }) {
      return h(
        ElTag, 
        { size: 'small', type: row.tax_diff == '0' ? 'danger' : 'success' }, 
        { default: () => row.tax_diff == '0' ? '不一致' : '一致' }
      )
    }
  },
  
  { 
    label: '备注', 
    prop: 'remark', 
    width: 100 
  },

  { 
    label: '业务类型', 
    width: 80,
    cellRenderer({ row }){
      return managerStatusOptions.find((item) => item.value == row.manager_status)?.label
    }
  },
  { 
    label: '关联商品ID', 
    prop: 'goodNo', 
    width: 80,
    cellRenderer({ row }){
      if (row.ProductRela.length === 0){
        return null
      }
      return row.ProductRela[0].id
    }
  },
  { 
    label: '关联商品名称', 
    prop: 'goodName', 
    width: 100,
    cellRenderer({ row }) {
      if (row.ProductRela.length === 0) {
        return null
      }
      return row.ProductRela[0].goodName
    }
  },
  { 
    label: "操作", 
    fixed: "right", 
    width: 50, 
    slot: "operation" 
  }
];

const contentConfig: ContentConfig = {
  columns,
  title: "商品管理",
  permissions: ['007'],
  apis: { 
    httpList: (parameter = {}) => httpList({ ...parameter, channel: [1, 2], manager_status: '3' }),
    httpAdd: true
  }
};

export default contentConfig;
