<template>
    <div class="edit-view">
        <page-title>
            <el-button @click="$router.go(-1)" :disabled="saving">取消</el-button>
            <el-button @click="onDelete" :disabled="saving" type="danger" v-if="formData.id">
                删除
            </el-button>
            <el-button @click="onSave" :loading="saving" type="primary">保存</el-button>
        </page-title>
        <div class="edit-view__content-wrapper">
            <div class="edit-view__content-section">
                <el-form
                    :model="formData"
                    :rules="rules"
                    ref="form"
                    label-width="81px"
                    label-position="right"
                    size="small"
                    style="max-width: 700px;"
                >
                    <el-form-item prop="name" label="名称">
                        <el-input v-model="formData.name"></el-input>
                    </el-form-item>
                    <el-form-item
                        prop="typeSettingId"
                        :label="
                            formData.category == 'NEWS'
                                ? '类型'
                                : formData.category == 'POLICY'
                                ? '政策层级'
                                : '法律类型'
                        "
                    >
                        <el-select
                            v-model="formData.typeSettingId"
                            clearable
                            filterable
                            placeholder="请选择"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in typeSettingIdOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="issuedAt" label="发文时间">
                        <el-date-picker
                            v-model="formData.issuedAt"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                            style="width: 100%"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="issuedBy" label="发文单位">
                        <el-input v-model="formData.issuedBy"></el-input>
                    </el-form-item>
                    <el-form-item prop="issuedCode" label="发文号" v-if="formData.category == 'POLICY'">
                        <el-input v-model="formData.issuedCode"></el-input>
                    </el-form-item>
                    <el-form-item prop="implementationAt" label="实施年份" v-if="formData.category == 'LAW'">
                        <el-date-picker
                            v-model="formData.implementationAt"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                            style="width: 100%"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="img" label="封面图片" v-if="formData.category != 'LAW'">
                        <single-upload v-model="formData.img"></single-upload>
                    </el-form-item>
                    <el-form-item prop="summary" label="摘要">
                        <el-input :rows="5" type="textarea" v-model="formData.summary"></el-input>
                    </el-form-item>
                    <el-form-item prop="content" label="内容">
                        <rich-text v-model="formData.content"></rich-text>
                    </el-form-item>
                    <el-form-item label="附件" prop="annex" v-if="formData.category == 'NEWS'">
                        <file-upload v-model="formData.annex" format="json"></file-upload>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>
<script>
import FileUpload from '../../components/FileUpload.vue';
export default {
    components: { FileUpload },
    name: 'PolicyLawEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('policyLaw/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                    if (res.category == 'NEWS') {
                        this.getSetting(8);
                    } else if (res.category == 'LAW') {
                        this.getSetting(9);
                    } else {
                        this.getSetting(7);
                    }
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        if (this.$route.query.category) {
            let category = this.$route.query.category;
            this.formData.category = this.$route.query.category;
            if (category == 'NEWS') {
                this.getSetting(8);
            } else if (category == 'LAW') {
                this.getSetting(9);
            } else {
                this.getSetting(7);
            }
        }
    },
    data() {
        return {
            saving: false,
            formData: {
                category: ''
            },
            rules: {
                name: [{ required: true, message: '请输入名称' }],
                typeSettingId: [{ required: true, message: '请选择类型' }],
                issuedAt: [{ required: true, message: '请选择发文时间' }],
                issuedBy: [{ required: true, message: '请输入发文单位', trigger: 'blur' }],
                issuedCode: [{ required: true, message: '请输入发文号' }],
                implementationAt: [{ required: true, message: '请选择实施年份' }],
                img: [{ required: true, message: '请添加封面图片', trigger: 'blur' }],
                summary: [{ required: true, message: '请填写摘要', trigger: 'blur' }],
                content: [{ required: true, message: '请填写内容' }]
            },
            typeSettingIdOptions: [],
            categoryOptions: [
                { label: '政策', value: 'POLICY' },
                { label: '法律', value: 'LAW' },
                { label: '新闻', value: 'NEWS' }
            ]
        };
    },
    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('/policyLaw/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(`/policyLaw/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 || '删除失败');
                    }
                });
        },
        getSetting(flag) {
            this.$http
                .post('/setting/byFlag', { flag: flag })
                .then(res => {
                    if (res.length > 0) {
                        res.forEach(item => {
                            this.typeSettingIdOptions.push({
                                label: item.name,
                                value: item.id
                            });
                        });
                    }
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
    }
};
</script>
<style lang="less" scoped></style>
