<template>
    <div shadow="never" class="nav_detail ">


        <Table ref="allBusinessTable"
               :tableParams="tableParams"
               :btnRole="leftBtn"
               @handleAdd="handleAdd"
               @selectionChange="handleSelectionChange" >
            <template #left>
<!--                <el-button v-hasPermi="[`${ props.perPrefix }:detail:tab_sjjl_sc`]" @click="removeBus">删除</el-button>-->

            </template>

        </Table>
    </div>

</template>

<script setup name="AllBusiness" lang="ts">

import {
    listAllBusiness,
    delAllBusiness,
    addAllBusiness,
    updateAllBusiness
} from '@/api/business/allBusiness';

import {AllBusinessVO, AllBusinessQuery, AllBusinessForm} from '@/api/business/allBusiness/types';
import { ElButton, ElMessage, ElMessageBox } from 'element-plus';
import auth from '@/plugins/auth'
import Table from '@/components/Table/index.vue'
import {tableTypes} from '@/components/Table/types'

const {proxy} = getCurrentInstance() as ComponentInternalInstance;


const removeBus = ()=>{
    ElMessageBox.confirm(
        '是否确认删除当前数据？',
        '确认提示',
        { type: 'warning', confirmButtonText: '提交' }
    ).then(() => {
        delAllBusiness( ids.value).then((res) => {
            if(res.success) {
                ElMessage.success('删除成功')
                getList()
            }else{
                ElMessage.error('删除失败')
            }
        })
    }).catch(() => { })

}
const allBusinessTable = ref()
const getList = () => {
    allBusinessTable.value.getData()
}

const props = defineProps({
    resourceType: {
        type: String,
        default: true
    },
    resourceTypeId: {
        type: String,
        default: true
    },
    customerId: {
        type: String,
        default: true
    },
    perPrefix: {
        type: String,
    }
})

const leftBtn = ref([
    // { type: 'add', hasPermi: `${ props.perPrefix }:detail:tab_sjjl_tj` },
    // { type: 'edit', hasPermi: `${ props.perPrefix }:detail:edit` },
    { type: 'remove', hasPermi: `${ props.perPrefix }:detail:tab_sjjl_sc` },
    // { type: 'import', hasPermi: `${ props.perPrefix }:detail:import` },
    { type: 'export', hasPermi: `${ props.perPrefix }:detail:tab_sjjl_dc` }
])

const tableParams = reactive<tableTypes<AllBusinessQuery, AllBusinessVO>>({
    tableColumn: [
        { show: true, label: '商机标题', prop: 'title', sortable: true},
        {show: true, label: '商机阶段', prop: 'stageName', sortable: false},
        {show: true, label: '报价金额(￥)', prop: 'offerAmount', sortable: false},
        {show: true, label: '最后跟进', prop: 'expectedSigningDate', sortable: false},
        {show: true, label: '未跟进天数', prop: 'notFollowedDays', sortable: false},
        {show: false, label: '成交机率', prop: 'stransactionProbabilityName', sortable: true},
        {show: false, label: '商机来源', prop: 'sourcesOfBusinessName', sortable: false},
        {
            show: true,
            label: '操作',
            prop: 'operation',
            disabledMove: true,
            render: ({column}: { column: AllBusinessVO }) => {
                return [
                    auth.hasPermi(`${ props.perPrefix }:detail:tab_sjjl_xq`) ? h(ElButton, {
                        link: true, type: 'primary', onClick: () => {
                            goDetail(column.id)
                        }
                    }, '详情') : '',
                ]
            }
        },
    ],
    keyId: 'id',
    queryParams: {
        resourceType: props.resourceType,
        resourceTypeId: props.resourceTypeId
    },
    listFn: listAllBusiness,
    delFn: delAllBusiness,
    importCode: 'IMPORT_BUSINESS_EXCEL',
    exportCode: 'EXPORT_BUSINESS_EXCEL',
    commonQueryOrderBy: 'getedDate'
})
const router = useRouter();
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);

/** 多选框选中数据 */
const handleSelectionChange = (selection: AllBusinessVO[]) => {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
    if(props.resourceType == '10'){
        router.push(`/business_opportunity/addbus?customerId=${props.customerId}`)
        return
    }
    if(props.resourceType == '20'){
        router.push(`/business_opportunity/addbus?customerId=${props.customerId}&liaisonId=${props.resourceTypeId}`)
        return
    }
    router.push(`/business_opportunity/addbus?customerId=${props.customerId}`)
}
/** 新增按钮操作 */
const goDetail = (id) => {
    router.pushTarget(`/business_opportunity/detail?id=${id}`)
}
const { queryParams } = toRefs(tableParams);

onMounted(() => {
    // getCustomerData()
});
</script>
