<template>
    <div>
        <el-table :data="list" border v-loading="loadding" style="width: 100%;">
            <el-table-column prop="userInfo.phone" label="所属行业及企业类型">
                <template #default="{ row }">
                    <div class="d-flex a-c">
                        <div>{{ selectDictLabel(industry_type, row.industryType) }}</div>
                        <div style="padding: 0 10px;">
                            <el-icon>
                                <arrow-right />
                            </el-icon>
                        </div>
                        <div>{{ selectDictLabel(enterprise_type, row.cpyType) }}</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="res" label="状态" align="center">
                <template #default="{ row }">
                    <template v-if="row.res === '0'">
                        <div>待审核</div>
                    </template>
                    <template v-if="row.res === '1'">
                        <div>已登记</div>
                    </template>
                    <template v-if="row.res === '2'">
                        <el-tooltip class="box-item" effect="dark" :content="row.msg" placement="top-start">
                            <div class="d-flex a-c j-c c-s-p">
                                <el-icon color="#E33F36">
                                    <warning-filled />
                                </el-icon>
                                未通过
                            </div>
                        </el-tooltip>
                    </template>
                </template>
            </el-table-column>
            <el-table-column prop="postName" label="操作">
                <template #default="{ row }">
                    <el-button @click="lookDetail(row)" type="primary" style="color: #3E7B65;" text>详情</el-button>
                    <template v-if="['0'].includes(row.res)">
                        <el-button @click="handleApproval(row, '1')" type="success" text>审核通过</el-button>
                        <el-button @click="handleApproval(row, '2')" type="warning" text>审核不通过</el-button>
                    </template>
                    <!-- <el-button v-if="['1'].includes(row.res)" @click="eidtAuth(row)" type="primary" text>编辑</el-button> -->
                    <!-- <el-button v-if="['2'].includes(row.res)" @click="deleteAuth(row)" type="danger" text>删除</el-button> -->
                    <el-button v-if="['1'].includes(row.res)" @click="cancelAuth(row)" type="danger" text>取消登记</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
    <CpyAuthDetail
        v-if="show4"
        v-model:show="show4"
        :info="formItem"
        :business_license="business_license"
        :drug_production_license="drug_production_license"
        :business_scope1="business_scope1"
        :business_scope2="business_scope2"
        :enterprise_type="enterprise_type"
        :industry_type="industry_type"
    ></CpyAuthDetail>
    <!-- <CpyAuthReg
        v-if="show5"
        v-model:show="show5"
        @change="changeJc"
        :business_license="business_license"
        :drug_production_license="drug_production_license"
        :info="formItem"
    ></CpyAuthReg> -->
</template>
<script lang="ts" setup name="baseAuthInfo">
import { approvalCert, cancelCert, companyCertificatelist } from '@/api/settled';
import { propTypes } from '@/utils/propTypes';
import { CpyAuthDetail } from '@/views/settled'
import { CircleCheck, CircleClose } from '@element-plus/icons-vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const {  enterprise_type, industry_type,business_license, drug_production_license, business_scope1, business_scope2 } = toRefs<any>(
    proxy?.useDict('enterprise_type', 'industry_type', 'business_license', 'drug_production_license', 'business_scope1', 'business_scope2')
);
const formItem = ref<any>(null)
const show4 = ref(false)
const show5 = ref(false)
const props = defineProps({
    cpyid: propTypes.string.def('')
});
const list = ref<any[]>([])
const loadding = ref(false)
const lookDetail = async (row: any) => {
    formItem.value = row
    show4.value = true
}
const eidtAuth = async (row: any) => {
    formItem.value = row
    show5.value = true
}
const changeJc = () => {
    getList()
}
const cancelAuth = async (row: any) => {
    ElMessageBox.confirm('您确定取消当前登记么?', '提示', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'success',
            icon: markRaw(CircleCheck)
        })
            .then(() => {
                submitCancelCert(row.id);
            })
            .catch(() => {
                proxy?.$modal.msgWarning('取消审核');
            });
}
const submitCancelCert = async (id: string) => {
    proxy?.$modal.loading('取消中...')
    const res = await cancelCert(id)
    proxy?.$modal.closeLoading()
    if (!res) return
    getList()
}
// 审批办理
const handleApproval = async (row: any, res: string) => {
    if (res === '1') {
        ElMessageBox.confirm('该认证审核通过?', '提示', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'success',
            icon: markRaw(CircleCheck)
        })
            .then(() => {
                const params = {
                    targetType: 'cert',
                    targetId: row.id,
                    res
                };
                approvalAjax(params);
            })
            .catch(() => {
                proxy?.$modal.msgWarning('取消审核');
            });
    } else {
        ElMessageBox.prompt('该认证审核不通过', '提示', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            inputPlaceholder: '不通过理由',
            inputErrorMessage: '请填写不通过理由',
            type: 'error',
            icon: markRaw(CircleClose),
            inputValidator: (value) => {
                return value ? true : false
            }
        })
            .then(({ value }) => {
                const params = {
                    targetType: 'cert',
                    targetId: row.id,
                    res,
                    msg: value
                };
                approvalAjax(params);
            })
            .catch(() => {
                proxy?.$modal.msgWarning('取消审核');
            });
    }
};
const approvalAjax = async (params: any) => {
    proxy?.$modal.loading('审核中...')
    const res = await approvalCert(params).catch(() => {});
    proxy?.$modal.closeLoading()
    if (!res) return;
    proxy?.$modal.msgSuccess('操作完成')
    getList()
};
const getList = async () => {
    loadding.value = true;
    const params = {
        certType: '1',
        cpyid: props.cpyid
    }
    const res = await companyCertificatelist(params);
    if (!res) return;
    loadding.value = false;
    list.value = [...res.rows];
};
onMounted(() => {
    getList()
})
</script>
