<template>
    <el-dialog v-model="visible" title="详情" width="680" @closed="$emit('closed')">
        <el-form ref="formRef" :model="form" :rules="rules" :disabled="mode == 'agree' || mode == 'refuse'" label-width="110px">
            <el-row>
                <el-col :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 :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-input v-model="form.zcLevel" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="政策类别" prop="zcType">
                        <el-input v-model="form.zcType" readonly></el-input>
                    </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 class="no-upload-btn" label="附件">
                        <yhUpload v-model="form.fileList" :limit="10">
                            <el-button type="primary" icon="upload" size="small"></el-button>
                        </yhUpload>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <el-form ref="approveRef" :model="form" :rules="rules" label-width="110px">
            <el-row>
                <template v-if="mode == 'agree' && isLastApprover">
                    <el-col :span="12">
                        <el-form-item label="是否入库" prop="isInWh">
                            <el-radio-group v-model="form.isInWh" @change="form.inWhType = null">
                                <el-radio v-for="(label, key) in whetherDic" :key="key" :label="label" :value="parseInt(key)"></el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="分值（0-10）" prop="score">
                            <el-input-number v-model="form.score" :min="0" :max="10" :step="0.1" :precision="2" :controls="false" placeholder="请输入分值"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col v-if="form.isInWh == 1" :span="12">
                        <el-form-item label="入库类型" prop="inWhType">
                            <el-select v-model="form.inWhType" placeholder="请选择入库类型">
                                <el-option v-for="item in storageTypeDic" :key="item" :label="item" :value="item"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </template>

                <el-col v-if="mode == 'refuse'" :span="24">
                    <el-form-item label="意见" prop="comment">
                        <el-input v-model="form.comment" type="textarea" :rows="4" placeholder="请输入意见"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <template #footer>
            <template v-if="form.processDefinitionId">
                <el-button v-if="mode == 'resubmit'" :loading="isSaving" type="primary" @click="submit">重新上报</el-button>
                <el-button v-else :loading="isSaving" type="primary" @click="submit">确 定</el-button>
            </template>
        </template>
    </el-dialog>
</template>

<script>
import API from "@/api/policy/share";
import Workflow from "@/api/workflow";
import { storageTypeDic, whetherDic } from "@/views/policyShare/main";

import yhUpload from "@/components/Upload/index.vue";

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

    data() {
        return {
            storageTypeDic, whetherDic,

            visible: false,
            isSaving: false,
            mode: "agree",
            form: {
                id: null,
                processDefinitionId: null,
                businessNo: null,
                status: null,
                createId: null,
                createName: null,
                createTime: null,
                deptId: null,
                deptName: null,
                companyId: null,
                companyName: null,
                name: null,
                zcLevel: null,
                zcType: null,
                docNo: null,
                contactPhone: null,
                abstractContent: null,
                score: null,
                isInWh: 0,
                inWhType: 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: "请输入政策概要" }],
                score: [{ required: true, message: "请输入分值" }],
                isInWh: [{ required: true }],
                inWhType: [{ required: true, message: "请选择入库类型" }],
                comment: [{ required: true, message: "请输入退回意见" }]
            },

            isLastApprover: false
        }
    },

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

        setData(id) {
            API.detail(id).then(res => {
                if (res.code === 200) {
                    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 if (key == "isInWh") this.form.isInWh = !!res.data[key] && res.data[key] || 0;
                        else this.form[key] = res.data[key] || null;
                    }
                    if (this.mode == "refuse") this.form["comment"] = "不同意";

                    Workflow.detail(res.data.processTaskId, res.data.processInstanceId).then(workflowRes => {
                        const { process, flows } = workflowRes;
                        this.isLastApprover = flows.findIndex(f => f.historyActivityType == "candidate") == -1;
                        this.form.processDefinitionId = process.processDefinitionId || null;
                    });
                } else ElMessage.error(res.msg);
            });
        },
  
        // 表单提交方法
        submit() {
            const promiseArray = [this.$refs.approveRef.validate()];
            this.mode == "resubmit" && promiseArray.push(this.$refs.formRef.validate());

            Promise.all(promiseArray).then(() => {
                this.isSaving = true;
                API[this.mode](this.form).then(res => {
                    this.isSaving = false;
                    if (res.code === 200) {
                        ElMessage.success("操作成功");
                        this.visible = false;
                        this.$emit("success");
                    } else ElMessage.error(res.msg);
                }).catch(() => this.isSaving = false);
            }).catch(() => false);
        }
    }
}
</script>

<style lang="scss" scoped>
.no-upload-btn :deep(.yh-upload-file) {
  .el-upload {
    display: none;
  }

  .el-upload-list.is-disabled {
    margin-top: 0;
  }
}

.el-input-number {
  width: 100%;

  :deep(.el-input) .el-input__wrapper {
    padding: 1px 11px;

    .el-input__inner {
      text-align: unset;
    }
  }
}
</style>