<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="res">
                        <el-select v-model="queryParams.res" placeholder="审核状态" clearable>
                            <el-option v-for="dict in cpy_res_status" :label="dict.label" :value="dict.value"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="提交日期">
                        <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="[new Date(), new Date()]"></el-date-picker>
                    </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-card shadow="never">
            <template #header v-if="false">
                <el-row :gutter="10" class="mb8">
                    <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>
            </template>

            <el-table v-loading="loading" :data="companyFieldApplyList">
                <el-table-column type="index" label="序号" width="50" align="center" />
                <el-table-column label="企业名称" align="center" prop="cpyName" width="200">
                    <template #default="{ row }">
                        <div :class="{ emphasize: row.hisField.cpyName != row.cpyName }">
                            {{ row.hisField.cpyName }}==&gt;
                            <span>
                                {{ row.cpyName }}
                            </span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="企业类型" align="center" prop="cpyType" width="200">
                    <template #default="{ row }">
                        <div style="display: flex;justify-content: center;" :class="{ emphasize: row.hisField.cpyType != row.cpyType }">
                            <div>
                                <dict-tag :value="row.hisField.cpyType" :options="cpy_type"></dict-tag>
                            </div>
                            ==&gt;
                            <div>
                                <dict-tag :value="row.cpyType" :options="cpy_type"></dict-tag>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="社会统一信用代码" align="center" prop="creditCode" width="200">
                    <template #default="{ row }">
                        <div :class="{ emphasize: row.hisField.creditCode != row.creditCode }">
                            <div>
                                {{ row.hisField.creditCode }}
                            </div>
                            ==&gt;
                            <div>
                                {{ row.creditCode }}
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="证件" width="300">
                    <template #default="{ row }">
                        <div style="display: flex;justify-content: center;align-items: center;" :class="{ emphasize: row?.hisField?.license?.url != row.license.url }">
                            <div>
                                <image-preview :src="row.hisField.license.url" :width="100" :height="100"></image-preview>
                            </div>
                            <div>==&gt;</div>
                            <div>
                                <image-preview :src="row.license.url" :width="100" :height="100"></image-preview>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="审核结果" align="center" prop="res">
                    <template #default="scope">
                        <dict-tag :value="scope.row.res" :options="cpy_res_status"></dict-tag>
                    </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" prop="updateTime" width="180">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
                    <template #default="scope">
                        <el-tooltip content="审核" placement="top">
                            <el-button link type="primary" icon="Edit" v-if="scope.row.res == '0'" @click="handleApproval(scope.row)" v-hasPermi="['smarttrace:companyFieldApply:edit']"></el-button>
                        </el-tooltip>
                    </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-dialog title="信息审核" v-model="visible" width="500px" append-to-body>
            <el-form ref="approvalFormRef" :model="approvalForm" :rules="approvalRules" label-width="100px">
                <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>
            <template #footer>
                <div class="dialog-footer">
                    <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="CompanyFieldApply" lang="ts">
import { listByPage, approvalCpyField } from '@/api/smarttrace/companyFieldApply';
import { CompanyFieldApplyVO, CompanyFieldApplyQuery, CompanyFieldApplyForm, ApprovalForm } from '@/api/smarttrace/companyFieldApply/types';
import { parseTime } from '@/utils/ruoyi';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const companyFieldApplyList = ref<CompanyFieldApplyVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);

const total = ref(0);
const { cpy_res_status, cpy_type } = toRefs<any>(proxy?.useDict('cpy_res_status', 'cpy_type'));
const queryFormRef = ref<ElFormInstance>();
const approvalFormRef = ref<ElFormInstance>();
const today = parseTime(new Date(), '{y}-{m}-{d}');
const dateRange = ref<[DateModelType, DateModelType]>([today as string, today as string]);

const initFormData: CompanyFieldApplyForm = {
    id: undefined,
    cpyid: undefined,
    field: undefined,
    fieldValue: undefined,
    auditor: undefined,
    applyer: undefined,
    auditorTime: undefined,
    res: undefined,
    msg: undefined,
    partnerId: undefined
};
const data = reactive<PageData<CompanyFieldApplyForm, CompanyFieldApplyQuery>>({
    form: { ...initFormData },
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        res: '0',
        params: {}
    },
    rules: {}
});

const { queryParams } = toRefs(data);

/** 查询企业字段修改申请列表 */
const getList = async () => {
    loading.value = true;
    const res = await listByPage(proxy?.addDateRange(queryParams.value, dateRange.value));
    companyFieldApplyList.value = res.rows;
    total.value = res.total;
    loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    dateRange.value = [today as string, today as string];
    handleQuery();
};
/** 审核信息 */
const visible = ref(false);
const buttonLoading = ref(false);
const initApprovalFrom = (): ApprovalForm => ({
    targetType: 'field',
    targetId: undefined,
    res: '1',
    msg: undefined
});
const approvalForm = ref(initApprovalFrom());
const reset = () => {
    approvalForm.value = initApprovalFrom();
};
const approvalRules = {
    res: [{ required: true, message: '审核结果不能为空', trigger: 'blur' }]
};
const cancel = () => {
    visible.value = false;
};
const submitForm = async () => {
    const valid = await approvalFormRef?.value?.validate();
    if (valid) {
        buttonLoading.value = true;
        await approvalCpyField(approvalForm.value).finally(() => (buttonLoading.value = false));
        proxy?.$modal.alert('操作成功');
        visible.value = false;
        handleQuery();
    }
};
const handleApproval = (row: any) => {
    reset();
    approvalForm.value.targetId = row.id;
    visible.value = true;
};
onMounted(() => {
    getList();
});
</script>
<style scoped>
.emphasize {
    background-color: rgba(248, 125, 125, 0.671);
    color: rgb(85, 85, 38);
}
</style>
