<template>
    <div class="edit-view">
        <el-form
            :model="record"
            ref="record"
            label-width="120px"
            label-position="right"
            size="small"
            style="max-width: 580px;"
        >
            <el-form-item label="承办单位">
                <el-input v-model="formData.record.organizer" readonly></el-input>
            </el-form-item>
            <el-form-item label="所属考级机构">
                <el-input v-model="formData.record.examinationAgency" readonly></el-input>
            </el-form-item>
            <el-form-item label="联系人">
                <el-input v-model="formData.record.examOwner" readonly></el-input>
            </el-form-item>
            <el-form-item label="联系方式">
                <el-input v-model="formData.record.examOwnerPhone" readonly></el-input>
            </el-form-item>
            <el-form-item label="考试时间">
                <el-input
                    v-model="formData.record.examinationStartTime"
                    style="width: 200px;margin-right: 10px"
                    readonly
                ></el-input>
                至
                <el-input
                    v-model="formData.record.examinationEndTime"
                    style="width: 200px;margin-left: 10px "
                    readonly
                ></el-input>
            </el-form-item>
            <el-form-item label="监管人员">
                <el-input v-model="formData.supervisorUser.nickname" readonly></el-input>
            </el-form-item>
        </el-form>
        <br />
        <el-divider direction="horizontal" content-position="left">检查信息</el-divider>
        <el-form
            :model="formData"
            :rules="rules"
            ref="form"
            label-width="290px"
            label-position="right"
            size="small"
            style="max-width: 600px;"
        >
            <el-form-item prop="time" label="时间">
                <el-date-picker v-model="formData.time" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item prop="isRecord" label="是否考前备案">
                <el-switch v-model="formData.isRecord"></el-switch>
            </el-form-item>
            <el-form-item prop="isPostExamGuide" label="是否明显位置张贴《考试简章》">
                <el-switch v-model="formData.isPostExamGuide"></el-switch>
            </el-form-item>
            <el-form-item prop="isPerfectDeviceServices" label="考场服务设备是否完善">
                <el-switch v-model="formData.isPerfectDeviceServices"></el-switch>
            </el-form-item>
            <el-form-item prop="isHaveTheSameTime" label="考试时间与备案考试时间是否一致">
                <el-switch v-model="formData.isHaveTheSameTime"></el-switch>
            </el-form-item>
            <el-form-item prop="isSameAddress" label="考试地点与备案考试地点是否一致">
                <el-switch v-model="formData.isSameAddress"></el-switch>
            </el-form-item>
            <el-form-item prop="isExaminer" label="是否有无相关专业考官且佩戴考官证">
                <el-switch v-model="formData.isExaminer"></el-switch>
            </el-form-item>
            <el-form-item prop="isRate" label="是否现场对艺术水平做出评定">
                <el-switch v-model="formData.isRate"></el-switch>
            </el-form-item>
            <el-form-item prop="isSureContent" label="是否是所属考级机构教材确定的考级内容">
                <el-switch v-model="formData.isSureContent"></el-switch>
            </el-form-item>
            <el-form-item prop="isPostPoster" label="是否在明显位置张贴《疫情防控指南》海报">
                <el-switch v-model="formData.isPostPoster"></el-switch>
            </el-form-item>
            <el-form-item prop="isHaveThermometer" label="考点是否配备测量体温设备，且专人值守">
                <el-switch v-model="formData.isHaveThermometer"></el-switch>
            </el-form-item>
            <el-form-item prop="isSchedule" label="考场是否实施预约限流措施">
                <el-switch v-model="formData.isSchedule"></el-switch>
            </el-form-item>
            <el-form-item prop="other" label="其他">
                <el-input type="textarea" v-model="formData.other"></el-input>
            </el-form-item>
            <el-form-item><file-upload></file-upload></el-form-item>
            <el-form-item>
                <el-button @click="onSave" :loading="saving" type="primary">保存</el-button>
                <el-button @click="onDelete" :loading="saving" type="danger" v-if="formData.id">删除 </el-button>
                <el-button @click="$router.go(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    name: 'RegulatoryEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('regulatory/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                    this.record = res.record;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {},
            record: {}
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            let data = { ...this.formData };

            this.saving = true;
            this.$http
                .post('/regulatory/save', data, { body: 'json' })
                .then(res => {
                    this.saving = false;
                    this.$message.success('成功');
                    this.$router.go(-1);
                })
                .catch(e => {
                    console.log(e);
                    this.saving = false;
                    this.$message.error(e.error);
                });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/regulatory/del/${this.formData.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.$router.go(-1);
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        console.log(e);
                        this.$message.error((e || {}).error || '删除失败');
                    }
                });
        }
    }
};
</script>
<style lang="less" scoped></style>
