<template>
    <el-dialog v-model="visible" :title="titleMap[mode]" width="680" @closed="$emit('closed', fileIsDel)">
        <el-form ref="formRef" :model="form" :rules="rules" label-width="110px">
            <el-row>
                <el-col v-if="form.id" :span="12">
                    <el-form-item label="政策编号" prop="businessNo">
                        <el-input v-model="form.businessNo" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="填报人">
                        <el-input v-model="form.createName" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="填报部门">
                        <el-input v-model="form.deptName" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="填报单位">
                        <el-input v-model="form.companyName" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col v-if="form.id" :span="12">
                    <el-form-item label="填报日期">
                        <el-date-picker v-model="form.createTime" readonly format="YYYY-MM-DD"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="政策名称" prop="name">
                        <el-input v-model="form.name" placeholder="请输入政策名称"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="政策等级" prop="zcLevel">
                        <el-select v-model="form.zcLevel" placeholder="请选择政策等级">
                            <el-option v-for="item in levelDic" :key="item" :label="item" :value="item"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="政策类别" prop="zcType">
                        <el-select v-model="form.zcType" placeholder="请选择政策类别">
                            <el-option v-for="item in typeDic" :key="item" :label="item" :value="item"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="政策文号" prop="docNo">
                        <el-input v-model="form.docNo" placeholder="请输入政策文号"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="联系方式" prop="contactPhone">
                        <el-input v-model="form.contactPhone" placeholder="请输入联系方式"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="政策概要" prop="abstractContent">
                        <el-input v-model="form.abstractContent" type="textarea" :rows="4" placeholder="请输入政策概要"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="附件">
                        <yhUpload v-model="form.fileList" v-model:isUpload="isUpload" :limit="10" @updateTable="fileIsDel = true">
                            <el-button type="primary" icon="upload" size="small"></el-button>
                        </yhUpload>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <template #footer>
            <el-button :loading="isSaving" type="primary" :disabled="isUpload" @click="submit(mode)">保 存</el-button>
            <el-button :loading="isSaving" type="primary" :disabled="isUpload" @click="submit('saveApprove')">直接上报</el-button>
        </template>
    </el-dialog>
</template>

<script>
import Common from "@/api/common";
import API from "@/api/policy/share";
import { useUserStore } from "@/store/user";
import { levelDic, typeDic } from "./main";
import yhUpload from "@/components/Upload/index.vue";

const { userInfo } = useUserStore(); // store 用户

export default {
    emits: ["success", "closed"],
    
    components: {
        yhUpload
    },

    data() {
        return {
            levelDic, typeDic,
            visible: false,
            isSaving: false,
            isUpload: false,
            fileIsDel: false,
            
            mode: "add",
            titleMap: {
                add: "新增",
                edit: "修改"
            },
            form: {
                id: null,
                businessNo: null,
                status: null,
                createId: userInfo.id,
                createName: userInfo.name,
                deptId: userInfo.deptId,
                deptName: null,
                companyId: null,
                companyName: null,
                createTime: null,
                name: null,
                zcLevel: null,
                zcType: null,
                docNo: null,
                contactPhone: null,
                abstractContent: null,
                fileList: []
            },

            rules: {
                businessNo: [{ required: true }],
                name: [{ required: true, message: "请输入政策名称" }],
                zcLevel: [{ required: true, message: "请选择政策等级" }],
                zcType: [{ required: true, message: "请选择政策类别" }],
                docNo: [{ required: true, message: "请输入政策文号" }],
                contactPhone: [{ required: true, message: "请输入联系方式" }],
                abstractContent: [{ required: true, message: "请输入政策概要" }]
            }
        }
    },

    mounted() {
        this.getUserDept();
    },

    methods: {
        open(mode = "add") {
            this.mode = mode;
            this.visible = true;
            return this;
        },

        setData(id) {
            API.detail(id).then(res => {
                for (const key in this.form) {
                    if (key == "fileList") this.form[key] = res.data[key].map(file => ({ ...file, name: file.originalName, path: file.fileDomain + "/" + file.fileName }));
                    else this.form[key] = res.data[key] || null;
                }
            });
        },

        getUserDept() {
            Common.getUserDept(userInfo.deptId).then(res => {
                this.form.deptId = res.data.deptId;
                this.form.deptName = res.data.deptName;
                this.form.companyId = res.data.companyId;
                this.form.companyName = res.data.companyName;
            });
        },

        // 表单提交方法
        submit(mode) {
            this.$refs.formRef.validate(valid => {
                if (valid) {
                    this.isSaving = true;
                    API[mode](this.form).then(() => {
                        this.isSaving = false;
                        ElMessage.success("操作成功");
                        this.visible = false;
                        this.fileIsDel = false;
                        this.$emit("success", mode);
                    }).catch(() => this.isSaving = false);
                } else {
                    return false;
                }
            });
        }
    }
}
</script>