import { shallowRef } from 'vue'
import { useTableColumnStore } from '@/stores'

type TableColumnKey = typeof tableColumnKeys[number]
const tableColumnKeys = ['system_menu', 'system_role', 'warehousing_diamond', 'warehousing_warehouse', 'mine_capital', 'listing_sellorder', 'listing_buyorder', 'listing_saleorder', 'listing_purchaseorder', 'listing_delisting', 'listing_bargain_buy', 'listing_bargain_sell', 'mine_address', 'mine_invoice', 'favorite', 'order', 'order_step', 'bargain_buy', 'bargain_sell'] as const

/**
 * PC端表格列
 */
const pcTableColumnMap = new Map<TableColumnKey, Ermcp.TableColumn[]>([
    // 系统管理-菜单管理
    ['system_menu', [
        { prop: 'resourcename', label: '菜单' },
        { prop: 'resourcecode', label: '代码' },
        { prop: 'component', label: '组件' },
        { prop: 'iconame', label: '图标' },
        { prop: 'operate', label: '操作' }
    ]],
    // 系统管理-角色管理
    ['system_role', [
        { prop: 'id', label: '序号', width: 100 },
        { prop: 'roleName', label: '角色名称' },
        { prop: 'createdAt', label: '创建时间' },
        { prop: 'updatedAt', label: '更新时间' },
        { prop: 'operate', label: '操作', width: 300 }
    ]],
    // 我的仓储-我的库存
    ['warehousing_diamond', [
        { prop: 'warehousenamedisplay', label: '仓库' },
        { prop: 'zsshapetypedisplay', label: '形状', width: 200 },
        { prop: 'zscolortype1display', label: '颜色' },
        { prop: 'zsclaritytype1display', label: '净度' },
        { prop: 'zscuttype1display', label: '切工' },
        { prop: 'zspolishtype1display', label: '抛光' },
        { prop: 'zssymmetrytype1display', label: '对称' },
        { prop: 'zsfluorescencetype1display', label: '荧光' },
        { prop: 'sizedisplay', label: '尺寸' },
        { prop: 'price', label: '价格' },
        { prop: 'weight', label: '克拉重量' },
        { prop: 'priceper', label: '克拉单价' },
        { prop: 'marketflag', label: '是否上架' },
        { prop: 'operate', label: '操作', width: 240 }
    ]],
    // 我的仓储-我的仓库
    ['warehousing_warehouse', [
        { prop: 'warehousecode', label: '仓库代码' },
        { prop: 'warehousename', label: '仓库名称' },
        { prop: 'address', label: '仓库地址' },
        { prop: 'contactname', label: '联系人' },
        { prop: 'contactnum', label: '联系电话' },
        { prop: 'createtime', label: '创建时间' },
        { prop: 'operate', label: '操作' }
    ]],
    // 挂牌大厅-出售大厅
    ['listing_sellorder', [
        { prop: 'sellusername', label: '卖方' },
        { prop: 'warehousenamedisplay', label: '仓库', width: 180 },
        { prop: 'zsshapetypedisplay', label: '形状', width: 200 },
        { prop: 'zscolortype1display', label: '颜色' },
        { prop: 'zsclaritytype1display', label: '净度' },
        { prop: 'zscuttype1display', label: '切工' },
        { prop: 'zspolishtype1display', label: '抛光' },
        { prop: 'zssymmetrytype1display', label: '对称' },
        { prop: 'zsfluorescencetype1display', label: '荧光' },
        { prop: 'size1', label: '尺寸' },
        { prop: 'price', label: '价格' },
        { prop: 'totalqty', label: '克拉重量' },
        { prop: 'fixedprice', label: '克拉单价' },
        { prop: 'operate', label: '操作', width: 180 }
    ]],
    // 挂牌大厅-求购大厅
    ['listing_buyorder', [
        { prop: 'buyusername', label: '买方' },
        { prop: 'zsshapetypedisplay', label: '形状', width: 200 },
        { prop: 'zscolortypedisplay', label: '颜色' },
        { prop: 'zsclaritytypedisplay', label: '净度' },
        { prop: 'zscuttypedisplay', label: '切工' },
        { prop: 'zspolishtypedisplay', label: '抛光' },
        { prop: 'zssymmetrytypedisplay', label: '对称' },
        { prop: 'zsfluorescencetypedisplay', label: '荧光' },
        { prop: 'zssize', label: '尺寸' },
        { prop: 'operate', label: '操作' }
    ]],
    // 挂牌大厅-我的出售
    ['listing_saleorder', [
        { prop: 'warehousenamedisplay', label: '仓库', width: 180 },
        { prop: 'zsshapetypedisplay', label: '形状', width: 200 },
        { prop: 'zscolortype1display', label: '颜色' },
        { prop: 'zsclaritytype1display', label: '净度' },
        { prop: 'zscuttype1display', label: '切工' },
        { prop: 'zspolishtype1display', label: '抛光' },
        { prop: 'zssymmetrytype1display', label: '对称' },
        { prop: 'zsfluorescencetype1display', label: '荧光' },
        { prop: 'size1', label: '尺寸' },
        { prop: 'price', label: '价格' },
        { prop: 'totalqty', label: '克拉重量' },
        { prop: 'fixedprice', label: '克拉单价' },
        { prop: 'operate', label: '操作', width: 180 }
    ]],
    // 挂牌大厅-我的求购
    ['listing_purchaseorder', [
        { prop: 'zsshapetypedisplay', label: '形状', width: 200 },
        { prop: 'zscolortypedisplay', label: '颜色' },
        { prop: 'zsclaritytypedisplay', label: '净度' },
        { prop: 'zscuttypedisplay', label: '切工' },
        { prop: 'zspolishtypedisplay', label: '抛光' },
        { prop: 'zssymmetrytypedisplay', label: '对称' },
        { prop: 'zsfluorescencetypedisplay', label: '荧光' },
        { prop: 'zssize', label: '尺寸' },
        { prop: 'operate', label: '操作', width: 180 }
    ]],
    // 挂牌大厅-我的摘牌
    ['listing_delisting', [
        { prop: 'buyorsell', label: '类型' },
        { prop: 'matchusername', label: '对手方' },
        { prop: 'zsshapetypedisplay', label: '形状' },
        { prop: 'zscolortype1display', label: '颜色' },
        { prop: 'zsclaritytype1display', label: '净度' },
        { prop: 'zscuttype1display', label: '切工' },
        { prop: 'zspolishtype1display', label: '抛光' },
        { prop: 'zssymmetrytype1display', label: '对称' },
        { prop: 'zsfluorescencetype1display', label: '荧光' },
        { prop: 'sizedisplay', label: '尺寸' },
        { prop: 'qty', label: '重量' },
        { prop: 'price', label: '价格' },
        { prop: 'exchangerate', label: '汇率' },
        { prop: 'tradeprice', label: '成交价格' },
        { prop: 'tradetime', label: '成交时间', width: 200 },
        { prop: 'operate', label: '操作' }
    ]],
    // 挂牌大厅-我的询价-求购
    ['listing_bargain_buy', [
        { prop: 'sellusername', label: '卖方' },
        { prop: 'warehousenamedisplay', label: '仓库' },
        { prop: 'zsshapetypedisplay', label: '形状', width: 200 },
        { prop: 'zscolortype1display', label: '颜色' },
        { prop: 'zsclaritytype1display', label: '净度' },
        { prop: 'zscuttype1display', label: '切工' },
        { prop: 'zspolishtype1display', label: '抛光' },
        { prop: 'zssymmetrytype1display', label: '对称' },
        { prop: 'zsfluorescencetype1display', label: '荧光' },
        { prop: 'sizedisplay', label: '尺寸' },
        { prop: 'price', label: '价格' },
        { prop: 'qty', label: '克拉重量' },
        { prop: 'priceper', label: '克拉单价' },
        { prop: 'applyprice', label: '我的出价' },
        { prop: 'applystatus', label: '申请状态' },
        { prop: 'applytime', label: '申请时间', width: 200 },
        { prop: 'operate', label: '操作', width: 180 }
    ]],
    // 挂牌大厅-我的询价-出售
    ['listing_bargain_sell', [
        { prop: 'buyusername', label: '买方' },
        { prop: 'zsshapetypedisplay', label: '形状', width: 200 },
        { prop: 'zscolortypedisplay', label: '颜色' },
        { prop: 'zsclaritytypedisplay', label: '净度' },
        { prop: 'zscuttypedisplay', label: '切工' },
        { prop: 'zspolishtypedisplay', label: '抛光' },
        { prop: 'zssymmetrytypedisplay', label: '对称' },
        { prop: 'zsfluorescencetypedisplay', label: '荧光' },
        { prop: 'zssizedisplay', label: '尺寸' },
        { prop: 'goodsno', label: '我的商品' },
        { prop: 'applyqty', label: '克拉重量' },
        { prop: 'applyprice', label: '我的出价' },
        { prop: 'applystatus', label: '申请状态' },
        { prop: 'applytime', label: '申请时间', width: 200 },
        { prop: 'operate', label: '操作', width: 180 }
    ]],
    // 会员中心-我的出入金
    ['mine_capital', [
        { prop: 'accountcode', label: '资金账户' },
        { prop: 'cusbankname', label: '托管银行' },
        { prop: 'executetype', label: '申请类型' },
        { prop: 'amount', label: '金额' },
        { prop: 'charge', label: '服务费' },
        { prop: 'updatetime', label: '申请时间' },
        { prop: 'applystatus', label: '状态' },
        { prop: 'operate', label: '操作' }
    ]],
    // 会员中心-我的地址
    ['mine_address', [
        { prop: 'receivername', label: '收货人' },
        { prop: 'cardtypeid', label: '证件类型' },
        { prop: 'cardnum', label: '证件号码' },
        { prop: 'phonenum', label: '联系电话' },
        { prop: 'address', label: '收货地址', width: 300 },
        { prop: 'isdefault', label: '是否默认' },
        { prop: 'operate', label: '操作', width: 250 }
    ]],
    // 会员中心-我的发票
    ['mine_invoice', [
        { prop: 'receipttype', label: '发票类型' },
        { prop: 'username', label: '发票抬头' },
        { prop: 'taxpayerid', label: '税号' },
        { prop: 'contactinfo', label: '企业电话' },
        { prop: 'information', label: '企业信息' },
        { prop: 'operate', label: '操作' }
    ]],
    // 我的收藏
    ['favorite', [
        { prop: 'warehousenamedisplay', label: '仓库' },
        { prop: 'zsshapetypedisplay', label: '形状' },
        { prop: 'zscolortype1display', label: '颜色' },
        { prop: 'zsclaritytype1display', label: '净度' },
        { prop: 'zscuttype1display', label: '切工' },
        { prop: 'zspolishtype1display', label: '抛光' },
        { prop: 'zssymmetrytype1display', label: '对称' },
        { prop: 'zsfluorescencetype1display', label: '荧光' },
        { prop: 'sizedisplay', label: '尺寸' },
        { prop: 'price', label: '价格' },
        { prop: 'weight', label: '克拉重量' },
        { prop: 'priceper', label: '克拉单价' },
        { prop: 'operate', label: '操作', width: 180 }
    ]],
    // 订单管理
    ['order', [
        { prop: 'zscategorydisplay', label: '商品分类' },
        { prop: 'goodsno', label: '商品编号' },
        { prop: 'zsshapetypedisplay', label: '形状' },
        { prop: 'zscolortype1display', label: '颜色' },
        { prop: 'sizedisplay', label: '尺寸' },
        { prop: 'zspolishtype1display', label: '重量' },
        { prop: 'price', label: '价格' },
        { prop: 'accountname', label: '对手方' },
        { prop: 'paidamount', label: '已付/已付金额' },
        { prop: 'freezeamountremain', label: '履约剩余冻结' },
        { prop: 'performancestatus', label: '履约状态' },
        { prop: 'steptypeid', label: '当前步骤' },
        { prop: 'remaindays', label: '剩余天数' },
        { prop: 'operate', label: '操作', width: 380 }
    ]],
    // 订单履约步骤
    ['order_step', [
        { prop: 'steptypename', label: '名称' },
        { prop: 'stepdays', label: '天数' },
        { prop: 'remaindays', label: '剩余天数' },
        { prop: 'stepvalue', label: '步骤值(%)' },
        { prop: 'stepamount', label: '金额' },
        { prop: 'realamount', label: '完成金额' },
        { prop: 'isauto', label: '是否自动' },
        { prop: 'steplanchtype', label: '启动类型' },
        { prop: 'starttime', label: '开始日期' },
        { prop: 'endtime', label: '结束日期' },
        { prop: 'stepstatus', label: '步骤状态' },
        { prop: 'remark', label: '步骤备注' },
    ]],
    // 询价消息-求购
    ['bargain_buy', [
        { prop: 'warehousenamedisplay', label: '仓库' },
        { prop: 'zsshapetypedisplay', label: '形状', width: 200 },
        { prop: 'zscolortype1display', label: '颜色' },
        { prop: 'zsclaritytype1display', label: '净度' },
        { prop: 'zscuttype1display', label: '切工' },
        { prop: 'zspolishtype1display', label: '抛光' },
        { prop: 'zssymmetrytype1display', label: '对称' },
        { prop: 'zsfluorescencetype1display', label: '荧光' },
        { prop: 'sizedisplay', label: '尺寸' },
        { prop: 'price', label: '价格' },
        { prop: 'qty', label: '克拉重量' },
        { prop: 'priceper', label: '克拉单价' },
        { prop: 'buyusername', label: '买方' },
        { prop: 'applyprice', label: '买方出价' },
        { prop: 'applystatus', label: '申请状态' },
        { prop: 'applytime', label: '申请时间', width: 200 },
        { prop: 'operate', label: '操作', width: 180 }
    ]],
    // 询价消息-出售
    ['bargain_sell', [
        { prop: 'zsshapetypedisplay', label: '形状', width: 200 },
        { prop: 'zscolortypedisplay', label: '颜色' },
        { prop: 'zsclaritytypedisplay', label: '净度' },
        { prop: 'zscuttypedisplay', label: '切工' },
        { prop: 'zspolishtypedisplay', label: '抛光' },
        { prop: 'zssymmetrytypedisplay', label: '对称' },
        { prop: 'zsfluorescencetypedisplay', label: '荧光' },
        { prop: 'zssizedisplay', label: '尺寸' },
        { prop: 'sellusername', label: '卖方' },
        { prop: 'goodsno', label: '卖方商品' },
        { prop: 'applyqty', label: '克拉重量' },
        { prop: 'applyprice', label: '卖方出价' },
        { prop: 'applystatus', label: '申请状态' },
        { prop: 'applytime', label: '申请时间', width: 200 },
        { prop: 'operate', label: '操作', width: 180 }
    ]],
])

/**
 * 获取表格列数据
 * @param tableKey 
 * @returns 
 */
export function getTableColumns(tableKey: TableColumnKey) {
    const tableColumns = pcTableColumnMap.get(tableKey) ?? [] // *这里应该判断终端类型，有空再处理*

    return tableColumns.map((e) => {
        e.show = true
        if (e.prop === 'operate') {
            e.fixed = 'right'
        }
        return e
    })
}

export function useTableColumns(tableKey: string, operateColumn = true) {
    const { tableColumns } = useTableColumnStore()

    const columns = shallowRef(tableColumns.value.reduce((res, cur) => {
        if (cur.tablekey === tableKey) {
            cur.columns.forEach((e) => {
                res.push({
                    prop: e.columnfield,
                    label: e.columntitle,
                    width: Number(e.columnwidth),
                    show: e.isshow ? true : false,
                    fixed: e.fixed,
                })
            })
            if (operateColumn) {
                res.push({
                    prop: 'operate',
                    label: '操作',
                    show: true,
                })
            }
        }
        return res
    }, [] as Ermcp.TableColumn[]))

    const updateColumns = () => {
        console.log('更新表头列')
    }

    return {
        columns,
        updateColumns,
    }
}