<template>
    <el-dialog v-model="deteleBusinessDialogVisible" width="1000" :before-close="beForeCancel" :show-close="false"
        top="10vh">
        <template #header="{ close, titleId, titleClass }">
            <div class="flex justify-between items-center border-b pb-3 dialog-header">
                <h4 :id="titleId">客户回收站</h4>
                <div>
                    <el-button type="primary" v-loading="allLoading.batchRecoveryLoading"
                        :disabled="batchTableData.length == 0" @click="batchOperation('恢复')">批量恢复</el-button>
                    <el-button type="primary" v-loading="allLoading.batchDeteleLoading"
                        :disabled="batchTableData.length == 0" @click="batchOperation('删除')">批量删除</el-button>
                    <el-button @click="cancel()">取消</el-button>
                </div>
            </div>
        </template>
        <div class="h-[60vh] flex flex-col">
            <div class="flex-1 w-full overflow-hidden">
                <el-table ref="deteleTableRef" :data="deteleBusinessTable" border v-loading="allLoading.tableLoading"
                    @selection-change="changeBatch" style="width: 100%;height: 100%;">
                    <el-table-column type="selection" width="55" />
                    <el-table-column prop="customName" label="客户名称" width="180"></el-table-column>
                    <el-table-column prop="customSourceValue" label="客户来源" width="180"></el-table-column>
                    <el-table-column prop="companyPhone" label="公司电话" width="180"></el-table-column>
                    <el-table-column prop="email" label="邮箱" width="200"></el-table-column>
                    <el-table-column prop="customerIndustryValue" label="客户行业" width="180"></el-table-column>
                    <el-table-column prop="customerLevelValue" label="客户级别" width="180"></el-table-column>
                    <el-table-column prop="inchargerName" label="负责人" width="190"></el-table-column>
                    <el-table-column prop="creatorName" label="创建人" width="180"></el-table-column>
                    <el-table-column prop="newCreateTime" label="创建时间" width="180"></el-table-column>
                    <el-table-column label="操作" fixed="right" width="120">
                        <template #default="scope">
                            <el-button link type="primary" size="large"
                                @click="operationItem(scope.row.id, scope.row.customName, '恢复')">恢复</el-button>
                            <el-button link type="danger" size="large"
                                @click="operationItem(scope.row.id, scope.row.customName, '删除')">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex justify-end pt-3">
                <el-pagination layout="total, prev, pager, next, sizes" :page-size="tableForm.pageFrom"
                    @size-change="handleSizeChange" @current-change="handleCurrentChange" :total="businessTotalTable"
                    :hide-on-single-page="true" />
            </div>
        </div>
    </el-dialog>
</template>
<script lang="ts" setup>
import { post } from '@/utils/request';
import { ref, reactive, onMounted, watchEffect, watch, inject } from 'vue'
import { URL_RECYCLELIST, URL_ROWBACK, URL_THOROUGHLYDETELE } from '../api';
import { ElTable } from 'element-plus';
import { confirmAction } from '@/utils/tools';
import { formatDate } from '@/utils/times';

type operationType = '恢复' | '删除'

const emits = defineEmits(['closeVisible']);
const globalPopup = inject<GlobalPopup>('globalPopup')
const deteleBusinessTable = ref([])
const deteleBusinessDialogVisible = ref(false)
const businessTotalTable = ref(0)
const batchTableData = ref([])
const allLoading = reactive({
    batchRecoveryLoading: false,
    batchDeteleLoading: false,
    tableLoading: false
})

const tableForm = reactive({
    pageIndex: 1,
    pageFrom: 10
})

const deteleTableRef = ref<InstanceType<typeof ElTable>>() // 线索table dom

const props = defineProps<{
    visibles: boolean
}>()

watch(() => props.visibles, (newVal) => {
    deteleBusinessDialogVisible.value = newVal
    if (newVal) {
        getTableList()
    }
})

function batchOperation(type: operationType) {
    const value = batchTableData.value.map((item: any) => item.id).join(',')
    const label = batchTableData.value.map((item: any) => item.customName).join(',')
    operationItem(value, label, type, true)
}

function operationItem(value: string | number, label: string, type: operationType, batch: boolean = false) {
    confirmAction(`确定${batch ? '批量' : ''}${type}【${label}】客户吗？`).then(() => {
        let url = type == '恢复' ? URL_ROWBACK : URL_THOROUGHLYDETELE
        post(url, { ids: value }).then(res => {
            if (res.code != 'ok') {
                globalPopup?.showError(res.msg)
                return
            }
            globalPopup?.showSuccess(`${type}成功`)
            getTableList()
            changeBatch(false)
        }).catch((err) => {
            globalPopup?.showError(err.msg)
        })
    })
}

function changeBatch(flag: boolean = true) {
    if (flag) {
        batchTableData.value = deteleTableRef.value && deteleTableRef.value.getSelectionRows()
    } else {
        batchTableData.value = []
        deteleTableRef.value && deteleTableRef.value.clearSelection()
    }
}

function getTableList() {
    allLoading.tableLoading = true
    post(URL_RECYCLELIST, { ...tableForm }).then((res) => {
        if (res.code == 'ok') {
            const { data, total } = res.data
            deteleBusinessTable.value = data.map((item: any) => {
                return {
                    ...item,
                    expectedTransactionDate: formatDate(new Date(item.expectedTransactionDate))
                }
            })
            businessTotalTable.value = total
        }
    }).finally(() => {
        allLoading.tableLoading = false
    })
}

function handleSizeChange(val: number) {
    tableForm.pageIndex = 1
    tableForm.pageFrom = val
    getTableList()
}

function handleCurrentChange(val: number) {
    tableForm.pageIndex = val
    getTableList()
}

function cancel() {
    emits('closeVisible', 'deteleCustomerVisible')
}

function beForeCancel(done: () => void) {
    emits('closeVisible', 'deteleCustomerVisible')
    done()
}

onMounted(() => {

})

</script>
<style lang="scss" scoped></style>