<template>
    <div class="p-2">
        <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
            <div class="search" v-show="showSearch">
                <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
                    <el-form-item label="企业名称" prop="cpyName">
                        <el-input v-model="queryParams.cpyName"></el-input>
                    </el-form-item>
                    <el-form-item label="审核状态" prop="res">
                        <el-select v-model="queryParams.res" placeholder="审核状态" clearable>
                            <el-option v-for="dict in cpy_res_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </transition>

        <el-tabs v-model="activeTab" style="padding: 5px;">
            <el-tab-pane label="基本资质认证" name="tab1">
                <el-card shadow="never">
                    <el-table v-loading="loading" :data="companyCertificateList">
                        <el-table-column type="index" label="序号" width="55" align="center" />
                        <el-table-column prop="cpyName" label="企业名称" width="150" align="center" />
                        <el-table-column label="企业类型" align="center" prop="cpyType">
                            <template #default="{ row }">
                                <dict-tag :options="enterprise_type" :value="row.cpyType" />
                            </template>
                        </el-table-column>
                        <el-table-column label="行业类别" align="center" prop="industryType">
                            <template #default="{ row }">
                                <dict-tag :options="industry_type" :value="row.industryType" />
                            </template>
                        </el-table-column>
                        <el-table-column label="认证类型" align="center" prop="certType">
                            <template #default="{ row }">
                                <dict-tag :options="cert_type" :value="row.certType" />
                            </template>
                        </el-table-column>
                        <el-table-column label="认证材料" align="center" prop="certMaterials" width="300">
                            <template #default="{ row }">
                                <div style="display: flex;flex-direction: column;">
                                    <div v-for="item in row.certMaterials" class="item">
                                        <dict-tag :options="cert_base_input_type" :value="item.fieldType" />
                                    </div>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="审核结果" align="center" prop="res">
                            <template #default="{ row }">
                                <dict-tag :options="cpy_res_status" :value="row.res" />
                            </template>
                        </el-table-column>
                        <el-table-column label="提交时间" align="center" prop="createTime" width="180">
                            <template #default="scope">
                                <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="审核时间" align="center" prop="auditorTime" width="180">
                            <template #default="scope">
                                <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d}') }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="说明" align="center" prop="msg" />
                        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                            <template #default="{ row }">
                                <el-button type="text" @click="handleApproval(row)" v-if="row.res == '0'">审核</el-button>
                                <el-button type="text" @click="handleView(row)" v-if="row.res != '0'">查看</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination
                        v-show="total > 0"
                        :total="total"
                        v-model:page="queryParams.pageNum"
                        v-model:limit="queryParams.pageSize"
                        @pagination="getList"
                    />
                </el-card>
            </el-tab-pane>
            <el-tab-pane label="特殊资质认证" name="tab2">
                <el-card shadow="never">
                    <el-table v-loading="loading" :data="companyCertificateList2">
                        <el-table-column type="index" label="序号" width="55" align="center" />
                        <el-table-column prop="cpyName" label="企业名称" width="150" align="center" />
                        <el-table-column label="认证类型" align="center" prop="certType">
                            <template #default="{ row }">
                                <dict-tag :options="cert_type" :value="row.certType" />
                            </template>
                        </el-table-column>
                        <el-table-column label="认证类型" align="center" prop="specialType">
                            <template #default="{ row }">
                                <dict-tag :options="special_type" :value="row.specialType" />
                            </template>
                        </el-table-column>
                        <el-table-column label="审核结果" align="center" prop="res">
                            <template #default="{ row }">
                                <dict-tag :options="cpy_res_status" :value="row.res" />
                            </template>
                        </el-table-column>
                        <el-table-column label="提交时间" align="center" prop="createTime" width="180">
                            <template #default="scope">
                                <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="审核时间" align="center" prop="auditorTime" width="180">
                            <template #default="scope">
                                <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d}') }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="说明" align="center" prop="msg" />
                        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                            <template #default="{ row }">
                                <el-button type="text" @click="handleApproval(row)" v-if="row.res == '0'">审核</el-button>
                                <el-button type="text" @click="handleView(row)" v-if="row.res != '0'">查看</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination
                        v-show="total2 > 0"
                        :total="total2"
                        v-model:page="queryParams2.pageNum"
                        v-model:limit="queryParams2.pageSize"
                        @pagination="getList2"
                    />
                </el-card>
            </el-tab-pane>
        </el-tabs>

        <el-dialog title="资质审核" v-model="visible" width="600px" append-to-body>
            <el-form :model="form">
                <el-form-item label="认证类型">
                    <dict-tag :options="cert_type" :value="form.certType" />
                </el-form-item>
                <el-row v-show="form.certType == '1'">
                    <el-col :span="12">
                        <el-form-item label="企业类型">
                            <dict-tag :options="enterprise_type" :value="form.cpyType" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="行业类别">
                            <dict-tag :options="industry_type" :value="form.industryType" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row v-show="form.certType == '2'">
                    <el-col :span="24">
                        <el-form-item label="认证类型">
                            <dict-tag :options="special_type" :value="form.specialType" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-divider>证明材料</el-divider>
                <component :is="component1" :data="form.certType == '1' ? form.certMaterials : form.specialCertMaterials"> </component>
            </el-form>
            <template #footer>
                <el-form ref="approvalFormRef" :model="approvalForm" :rules="approvalRules" label-width="100px" :disabled="viewmode">
                    <el-form-item label="审核结果" prop="res">
                        <el-select v-model="approvalForm.res" placeholder="审核结果" clearable>
                            <el-option
                                v-for="dict in cpy_res_status.filter((x: any) => x.value != '0')"
                                :label="dict.label"
                                :value="dict.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="原因" prop="msg">
                        <el-input v-model="approvalForm.msg" />
                    </el-form-item>
                </el-form>
                <div class="dialog-footer" v-if="!viewmode">
                    <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="CompanyCertificate" lang="ts">
import { listCompanyCertificate, approvalCert } from '@/api/smarttrace/companyCertificate';
import { CompanyCertificateVO, CompanyCertificateQuery, CompanyCertificateForm } from '@/api/smarttrace/companyCertificate/types';
import { ApprovalForm } from '@/api/smarttrace/companyFieldApply/types';
import CertMaterials from './certMaterials.vue';
import OtherCertMaterials from './otherCertMaterials.vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const companyCertificateList = ref<CompanyCertificateVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const { cpy_res_status, enterprise_type, industry_type, cert_base_input_type } = toRefs<any>(
    proxy?.useDict('cpy_res_status', 'cpy_type', 'en', 'enterprise_type', 'industry_type', 'cert_base_input_type')
);
const special_type = [
    { label: 'GAP基地认证登记', value: '1' },
    { label: '地理标志保护产品专用标志可用认证', value: '2' },
    { label: '中国有机认证', value: '3' },
    { label: '绿色食品认证', value: '4' },
    { label: '农产品地理标志可用认证', value: '5' },
    { label: '中国良好农业规范认证(GAP)', value: '6' },
    { label: '其他认证', value: '7' }
];
const queryFormRef = ref<ElFormInstance>();
const initFormData = (): CompanyCertificateForm => ({
    id: undefined,
    cpyid: undefined,
    cpyType: undefined,
    certMaterials: [],
    res: undefined,
    industryType: undefined,
    auditor: undefined,
    applyer: undefined,
    auditorTime: undefined,
    msg: undefined,
    certType: undefined,
    specialCertMaterials: {},
    specialType: undefined
});
const data = reactive<PageData<CompanyCertificateForm, CompanyCertificateQuery>>({
    form: initFormData(),
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        cpyType: undefined,
        res: '0',
        industryType: undefined,
        cpyName: undefined,
        certType: '1',
        params: {}
    },
    rules: {}
});

const { queryParams, form } = toRefs(data);

/** 查询企业认证列表 */
const getList = async () => {
    loading.value = true;
    const res = await listCompanyCertificate(queryParams.value);
    companyCertificateList.value = res.rows;
    total.value = res.total;
    loading.value = false;
};

const cert_type = [
    { value: '1', label: '基本资质认证' },
    { value: '2', label: '特殊资质认证' }
];
/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    queryParams2.value.pageNum = 1;
    getList();
    getList2();
};

/** 重置按钮操作 */
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
};
// 是否显示弹框
const visible = ref(false);
const viewmode = ref(false);
const component1 = ref();
const handleView = (row: CompanyCertificateVO) => {
    reset();
    Object.assign(form.value, row);
    if (row.certType == '1') {
        component1.value = CertMaterials;
    } else {
        component1.value = OtherCertMaterials;
    }
    viewmode.value = true;
    visible.value = true;
};
const initApprovalFrom = (): ApprovalForm => ({
    targetType: 'field',
    targetId: undefined,
    res: '1',
    msg: undefined
});
const approvalForm = ref(initApprovalFrom());
const reset = () => {
    form.value = initFormData();
    approvalForm.value = initApprovalFrom();
};
const approvalFormRef = ref<ElFormInstance>();
const approvalRules = {
    res: [{ required: true, message: '审核结果不能为空', trigger: 'blur' }]
};
const cancel = () => {
    visible.value = false;
};
const buttonLoading = ref(false);
const submitForm = async () => {
    const valid = await approvalFormRef?.value?.validate();
    if (valid) {
        buttonLoading.value = true;
        await approvalCert(approvalForm.value).finally(() => (buttonLoading.value = false));
        proxy?.$modal.alert('操作成功');
        visible.value = false;
        handleQuery();
    }
};
const handleApproval = (row: any) => {
    reset();
    Object.assign(form.value, row);
    approvalForm.value.targetId = row.id;
    if (row.certType == '1') {
        component1.value = CertMaterials;
    } else {
        component1.value = OtherCertMaterials;
    }
    viewmode.value = false;
    visible.value = true;
};
const activeTab = ref('tab1');
const total2 = ref(0);
const companyCertificateList2 = ref<CompanyCertificateVO[]>([]);
const queryParams2 = ref({
    pageNum: 1,
    pageSize: 10,
    certType: '2'
});
const getList2 = async () => {
    loading.value = true;
    const res = await listCompanyCertificate({
        ...queryParams2.value,
        cpyName: queryParams.value.cpyName,
        res: queryParams.value.res
    });
    companyCertificateList2.value = res.rows;
    total2.value = res.total;
    loading.value = false;
};

onMounted(() => {
    getList();
    getList2();
});
</script>
<style scoped>
.item {
    border-bottom: 1px solid green;
}
</style>
