import { ref, reactive } from 'vue';
import type { BasicTableProps, VxeGridInstance } from '@/components/VxeTable';
import { BaseFeesetAction } from '@/type/BaseFeesetAction';
import { Plus, Delete, Refresh, CopyDocument, Setting, Odometer } from "@element-plus/icons-vue";
import { ActionOptions } from '@/components/DropDownButtons/type';
import { useReturnData } from '@/utils/useReturnData';
import { useErrorConfirm } from '@/utils/useErrorConfirm';
import { api_costFeeSetParentIdListApi } from '@/api/costFeeSet/costFeeSet';

const { report } = useErrorConfirm()
/**
 * 成本费用设置表格配置
 */
function useProviderFeeSetTable() {
    const providerFeeSetGridRef = ref<VxeGridInstance>()
    // vxe-gird-成本费用设置列表配置
    const providerFeeSetListOptions = reactive<BasicTableProps>({
        id: 'providerFeeSet_table',
        border: true,
        keepSource: true,
        showOverflow: true,
        height: 480,
        loading: false,
        stripe: true,
        rowConfig: { isHover: true, keyField: 'id' },
        columnConfig: { resizable: true },
        expandConfig: {
            accordion: true,
            lazy: true,
            visibleMethod({ row }) {//判断是否显示展开图标
                if (row.haveSumRecord) {
                    return true
                } else {
                    return false
                }
            },
            loadMethod(scope) {//异步展开
                return hanleExpandRow(scope)
            }
        },
        columns: [
            { type: "seq", width: 45 },
            { type: "checkbox", width: 45 },
            { field: 'providerName', title: '服务商', minWidth: 120 },
            { field: 'productName', title: '产品名称', minWidth: 120 },
            { field: 'feeSetName', title: '费用名称', minWidth: 120 },
            { field: 'feeItemCategoryName', title: '费项类型', minWidth: 120 },
            { field: 'feeItemName', title: '费用类型', minWidth: 120 },
            { field: 'activeFlag', title: '状态', minWidth: 80, slots: { default: 'activeFlag' } },
            { field: 'beginDate', title: '生效起始时间', minWidth: 110, slots: { default: 'beginDate' } },
            { field: 'endDate', title: '生效结束时间', minWidth: 110, slots: { default: 'endDate' } },
            { field: 'createTime', title: '创建时间', minWidth: 100 },
            { field: 'createUserName', title: '创建人', minWidth: 80 },
            { title: '操作', slots: { default: 'actions' }, width: 135, fixed: 'right' }
        ]
    })

    // 展开的版本
    const providerFeeSetExpandOptions = reactive<BasicTableProps>({
        id: 'customerexpand_table',
        border: false,
        keepSource: true,
        showOverflow: true,
        showHeader: false,
        rowConfig: {
            isHover: true
        },
        columns: [
            { width: 85 },
            { type: "checkbox", width: 45 },
            { field: 'providerName', title: '服务商', minWidth: 120 },
            { field: 'productName', title: '产品名称', minWidth: 120 },
            { field: 'feeSetName', title: '费用名称', minWidth: 120 },
            { field: 'feeItemCategoryName', title: '费项类型', minWidth: 120 },
            { field: 'feeItemName', title: '费用类型', minWidth: 120 },
            { field: 'activeFlag', title: '状态', minWidth: 60, slots: { default: 'activeFlag' } },
            { field: 'beginDate', title: '生效起始时间', minWidth: 110, slots: { default: 'beginDate' } },
            { field: 'endDate', title: '生效结束时间', minWidth: 110, slots: { default: 'endDate' } },
            { field: 'createTime', title: '创建时间', minWidth: 100 },
            { field: 'createUserName', title: '创建人', minWidth: 80 },
            { title: '操作', slots: { default: 'actions' }, width: 135, fixed: 'right' }
        ],
    })

    // 操作类别
    // TODO 权限还未修改正取
    const providerFeeSetActionOptions: ActionOptions[] = [
        {
            value: BaseFeesetAction.DELETE,
            label: "删除",
            type: "danger",
            icon: Delete,
            permissions: ['sys:costfeeitemset:delete']
        },
        {
            value: BaseFeesetAction.VERSION,
            label: "新增版本",
            type: "success",
            icon: Plus,
            permissions: ['sys:customerfeeset:edit']
        },
        {
            value: BaseFeesetAction.EDIT,
            label: "设置费用",
            color: "#0078f3",
            icon: Setting,
            permissions: ['sys:costfeeitemset:edit'],
            isDisabled: true
        },
        {
            value: BaseFeesetAction.VIEW,
            label: "查看",
            color: "#01aee6",
            icon: Odometer,
            permissions: ['sys:costfeeitemset:edit'],
        },
        {
            value: BaseFeesetAction.FEESET_ACTIVE,
            label: "激活",
            islabel: "停用",
            color: "#409eff",
            type: "primary",
            icon: Refresh,
            permissions: ['sys:costfeeitemset:active']
        },
        {
            value: BaseFeesetAction.COPY,
            label: "拷贝",
            color: "#42b983",
            icon: CopyDocument,
            permissions: ['sys:costfeeSet:copy'],
            isDisabled: true
        },
    ]

    const { resData } = useReturnData()
    // 根据折叠id展开
    const hanleExpandRow = async (scope: Recordable) => {
        try {
            scope.row.customerFeeSetList = [];
            const res = await api_costFeeSetParentIdListApi({ id: scope.row.id, parentId: scope.row.parentId })
            scope.row.customerFeeSetList = resData(res);
        } catch (error) {
            report(error)
        }
    }

    return {
        providerFeeSetListOptions,
        providerFeeSetExpandOptions,
        providerFeeSetActionOptions,
        providerFeeSetGridRef
    }
}

export {
    useProviderFeeSetTable
}