<template>
    <div class="edit-view">
        <el-form
            :model="formData"
            :rules="rules"
            ref="form"
            label-width="80px"
            label-position="right"
            size="small"
            style="max-width: 500px"
        >
            <el-form-item prop="name" label="项目名称">
                <el-input v-model="formData.name"></el-input>
            </el-form-item>
            <el-form-item prop="pics" label="图片">
                <multi-upload v-model="formData.pics"></multi-upload>
            </el-form-item>
            <el-form-item prop="intro" label="项目简介">
                <rich-text v-model="formData.intro"></rich-text>
            </el-form-item>
            <el-form-item prop="attachment" label="附件">
                <file-upload v-model="formData.attachment" :limit="10" format="json"></file-upload>
            </el-form-item>
            <!-- <el-form-item prop="review" label="评审">
                <el-input type="textarea" autosize v-model="formData.review" :rows="5"></el-input>
            </el-form-item>
            <el-form-item prop="reviewStatus" label="评审状态">
                <el-select v-model="formData.reviewStatus" clearable filterable placeholder="请选择">
                    <el-option
                        v-for="item in reviewStatusOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="passStatus" label="通过状态">
                <el-select v-model="formData.passStatus" clearable filterable placeholder="请选择">
                    <el-option
                        v-for="item in passStatusOptions"
                        :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: 'ProjectManagementEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('projectManagement/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
    },
    data() {
        return {
            saving: false,
            formData: {
                reviewStatus: '待评审',
                passStatus: '待审核'
            },
            rules: {
                name: [
                    {
                        required: true,
                        message: '请输入项目名称',
                        trigger: 'blur'
                    }
                ],
                status: [
                    {
                        required: true,
                        message: '请输入status',
                        trigger: 'blur'
                    }
                ]
            },
            reviewStatusOptions: [
                { label: '待评审', value: '待评审' },
                { label: '已评审', value: '已评审' }
            ],
            passStatusOptions: [
                { label: '待审核', value: '待审核' },
                { label: '已通过', value: '已通过' },
                { label: '未通过', value: '未通过' }
            ]
        };
    },
    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('/projectManagement/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(`/projectManagement/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>
