<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="location" label="位置">
                <template>
                    <el-select v-model="formData.location" clearable placeholder="请选择" :disabled="'location'==subColumn">
                        <el-option v-for="item in locationOptions" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
            <el-form-item prop="title" label="标题">
                <el-input v-model="formData.title" :disabled="'title'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="subtitle" label="副标题">
                <el-input v-model="formData.subtitle" :disabled="'subtitle'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="url" label="跳转链接">
                <el-input v-model="formData.url" :disabled="'url'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="imageUrl" label="图片">
                <single-upload v-model="formData.imageUrl" :disabled="'imageUrl'==subColumn"></single-upload>
            </el-form-item>
            <el-form-item prop="rank" label="排序">
                <el-input v-model="formData.rank" :disabled="'rank'==subColumn"></el-input>
            </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.column) {
            this.subColumn = this.$route.query.column.split(',')[1];
            this.subValue = this.$route.query.column.split(',')[0];
        }

        if (this.$route.query.id) {
            this.$http.get({
                url: '/bannerInfo/getOne',
                data: {
                    id: this.$route.query.id
                }
            }).then(res => {
                if (res.success) {


                    this.formData = res.data;

                    if (this.$route.query.column) {
                        this.formData[this.subColumn] = this.subValue;
                    }
                }
            })
        } else {
            if (this.$route.query.column) {
                this.formData[this.subColumn] = this.subValue;
            }
        }

    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {
                location:
                    [
                        { required: true, message: '请输入 位置', trigger: 'blur' },
                    ],
            },
            locationOptions: [
                { label: '首页banner', value: '1' }, 
                { label: '首页修图消息', value: '2' }, 
                { label: '首页影楼消息', value: '4' },
                { label: '首页样片消息', value: '5' },
                { label: '首页营销消息', value: '6' },
                { label: '首页周边消息', value: '7' },
                { label: '修图banner', value: '8' },
                { label: '影楼banner', value: '9' },
                { label: '样片banner', value: '10' },
                { label: '营销banner', value: '11' },
                { label: '周边banner', value: '12' },
                { label: '关于我们banner', value: '13' },
                { label: '加入我们banner', value: '14' },
                { label: '联系我们banner', value: '15' },
                ],
            subColumn: '',
            subValue: '',
        }
    },
    methods: {
        onSave() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            var data = JSON.parse(JSON.stringify(this.formData));
            this.$http.post({
                url: this.formData.id ? '/bannerInfo/update' : '/bannerInfo/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: '/bannerInfo/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>
