<template>
    <div class="edit-view">
        <el-form
            :model="formData"
            :rules="rules"
            ref="form"
            label-width="136px"
            label-position="right"
            size="small"
            style="max-width: 600px;"
        >
            <el-form-item prop="examinationName" label="考级活动名称">
                <el-input v-model="formData.examinationName"></el-input>
            </el-form-item>
            <el-form-item prop="examinationStartTime" label="考级活动时间">
                <el-date-picker
                    v-model="formData.examinationStartTime"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期"
                    style="width: 200px; margin-right: 20px"
                >
                </el-date-picker>
                至
                <el-date-picker
                    v-model="formData.examinationEndTime"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期"
                    style="width: 200px; margin-left: 20px"
                >
                </el-date-picker>
            </el-form-item>
            <el-form-item prop="examinationAgency" label="考级机构名称">
                <el-input v-model="formData.examinationAgency"></el-input>
            </el-form-item>
            <el-form-item prop="category" label="单位类别">
                <el-select v-model="formData.category" clearable filterable placeholder="请选择">
                    <el-option
                        v-for="item in categoryOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="organizer" label="承办单位名称">
                <el-input v-model="formData.organizer"></el-input>
            </el-form-item>
            <el-form-item prop="creditCode" label="统一社会信用代码">
                <el-input v-model="formData.creditCode"></el-input>
            </el-form-item>
            <el-form-item prop="examinationDistrict" label="考级地点">
                <el-select v-model="formData.district">
                    <el-option
                        v-for="item in district"
                        :key="item.id"
                        :value="item.name"
                        :label="item.name"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="examinationAddress" label="考级地点">
                <el-input v-model="formData.examinationAddress"></el-input>
            </el-form-item>
            <el-form-item prop="examCenterQuantity" label="考场数量">
                <el-input-number v-model="formData.examCenterQuantity" class="date-width"></el-input-number>
            </el-form-item>
            <el-form-item prop="examQuantity" label="报考人数">
                <el-input-number v-model="formData.examQuantity" class="date-width"></el-input-number>
            </el-form-item>
            <el-form-item prop="examinerQuantity" label="考官人数">
                <el-input-number v-model="formData.examinerQuantity" class="date-width"></el-input-number>
            </el-form-item>
            <el-form-item prop="examOwner" label="考点负责人名称">
                <el-input v-model="formData.examOwner"></el-input>
            </el-form-item>
            <el-form-item prop="examOwnerPhone" label="考点负责人手机号">
                <el-input v-model="formData.examOwnerPhone"></el-input>
            </el-form-item>
            <el-form-item prop="securityOwner" label="安全负责人名称">
                <el-input v-model="formData.securityOwner"></el-input>
            </el-form-item>
            <el-form-item prop="securityOwnerPhone" label="安全负责人手机号">
                <el-input v-model="formData.securityOwnerPhone"></el-input>
            </el-form-item>
            <el-form-item prop="recordTime" label="备案时间">
                <el-date-picker
                    v-model="formData.recordTime"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期"
                >
                </el-date-picker>
            </el-form-item>
            <el-form-item prop="status" label="状态">
                <el-select v-model="formData.status" clearable filterable placeholder="请选择">
                    <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </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: 'RecordEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('record/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        this.$http
            .get('/district/NJ')
            .then(res => {
                this.district = res;
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {},
            categoryOptions: [{ label: '承办单位', value: 'ORGANIZER' }],
            statusOptions: [{ label: '正常', value: 'NORMAL' }],
            district: []
        };
    },
    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('/record/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(`/record/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>
.date-width {
    width: 200px;
}
</style>
