<template>
    <div>
        <el-form :model="formData" :rules="rules" ref="form" label-width="80px" label-position="right" size="small" style="max-width: 500px;">
            <el-form-item prop="newsType" label="类型">
                <el-select placeholder="类型" size="small" v-model="formData.newsType" class="filter-item">
                    <el-option label="攻略指南" value="0">
                    </el-option>
                    <el-option label="新鲜资讯" value="1">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="title" label="标题">
                <el-input v-model="formData.title"></el-input>
            </el-form-item>
            <el-form-item prop="subtitle" label="副标题">
                <el-input v-model="formData.subtitle"></el-input>
            </el-form-item>
            <el-form-item prop="image" label="图片">
                <single-upload v-model="formData.image"></single-upload>
            </el-form-item>
            <el-form-item prop="newsUrl" label="地址">
                <el-input v-model="formData.newsUrl"></el-input>
            </el-form-item>
            <el-form-item prop="rank" label="排序">
                <el-input v-model="formData.rank"></el-input>
            </el-form-item>
            <el-form-item prop="hot" label="热度">
                <el-input v-model="formData.hot"></el-input>
            </el-form-item>
            <el-form-item prop="recommend" label="推荐">
                <el-select placeholder="是否推荐" size="small" v-model="formData.recommend" class="filter-item">
                    <el-option label="推荐" value="Y">
                    </el-option>
                    <el-option label="不推荐" value="N">
                    </el-option>

                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button @click="onSave" :loading="$store.state.fetchingData" type="primary">保存</el-button>
                <el-button @click="onDelete" v-if="formData.id" type="danger">删除</el-button>
                <el-button @click="$router.go(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import formValidator from '../formValidator'

export default {
    created() {
        if (this.$route.query.id) {
            this.$http.get({
                url: '/newsInfo/getOne',
                data: {
                    id: this.$route.query.id
                }
            }).then(res => {
                if (res.success) {
                    res.data.newsType = String(res.data.newsType);
                    this.formData = res.data;
                }
            })
        }
    },
    data() {
        return {
            saving: false,
            formData: {
                newsType: '0',
                recommend: 'N'
            },
            rules: {
                title:
                    [
                        { required: true, message: '请输入标题', trigger: 'blur' },
                    ],
                image:
                    [
                        { required: true, message: '请上传图片', trigger: 'blur' },
                    ],
                newsUrl:
                    [
                        { required: true, message: '请输入地址', trigger: 'blur' },
                    ],
            },
        }
    },
    methods: {
        onSave() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            var data = JSON.parse(JSON.stringify(this.formData));
            delete data.createTime;
            delete data.updateTime;
            this.$http.post({
                url: this.formData.id ? '/newsInfo/update' : '/newsInfo/save',
                data: data
            }).then(res => {
                if (res.success) {
                    this.$message.success('成功');
                    this.$router.go(-1);
                } else {
                    this.$message.warning('失败')
                }
            });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/newsInfo/del',
                    data: { id: this.formData.id }
                })
            }).then(() => {
                this.$message.success('删除成功');
                this.$router.go(-1);
            }).catch(action => {
                if (action === 'cancel') {
                    this.$message.info('删除取消');
                } else {
                    this.$message.error('删除失败');
                }
            })
        },
    }
}
</script>
<style lang="less" scoped>
</style>
