<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="aimName" label="点标名称">
                <el-input v-model="formData.aimName" :disabled="'aimName'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="icon" label="图标">
                <single-upload v-model="formData.icon" :disabled="'icon'==subColumn"></single-upload>
            </el-form-item>
            <el-form-item prop="icon" label="图标">
                <el-input v-model="formData.icon" :disabled="'icon'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="iconRank" label="图标顺序">
                <el-input type="number" v-model="formData.iconRank" :disabled="'iconRank'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="aloneFlag" label="是否独占">
                <el-switch v-model="formData.aloneFlag" active-color="#13ce66" inactive-color="#ff4949" :disabled="'aloneFlag'==subColumn">
                </el-switch>
            </el-form-item>
            <el-form-item prop="autoPlay" label="自动触发">
                <el-switch v-model="formData.autoPlay" active-color="#13ce66" inactive-color="#ff4949" :disabled="'autoPlay'==subColumn">
                </el-switch>
            </el-form-item>
            <el-form-item prop="autoDisplay" label="自动显示">
                <el-switch v-model="formData.autoDisplay" active-color="#13ce66" inactive-color="#ff4949" :disabled="'autoDisplay'==subColumn">
                </el-switch>
            </el-form-item>
            <el-form-item prop="showLevel" label="显示层级">
                <el-input type="number" v-model="formData.showLevel" :disabled="'showLevel'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="remark" label="备注">
                <el-input v-model="formData.remark" :disabled="'remark'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="templateId" label="上级模板">
                <el-input v-model="formData.templateId" :disabled="'templateId'==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: '/aimTemplateAim/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;
                    }

                    this.formData.aloneFlag = this.formData.aloneFlag == 'Y' ? true : false;
                    this.formData.autoDisplay = this.formData.autoDisplay == 'Y' ? true : false;
                    this.formData.autoPlay = this.formData.autoPlay == 'Y' ? true : false;

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

    },
    data() {
        return {
            saving: false,
            formData: {
                iconRank: 1,
                showLevel: 1,
            },
            rules: {
                aimName:
                    [
                        { required: true, message: '请输入 点标名称', trigger: 'blur' },
                    ],
            },
            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));

            data.aloneFlag = this.formData.aloneFlag ? 'Y' : 'N';
            data.autoDisplay = this.formData.autoDisplay ? 'Y' : 'N';
            data.autoPlay = this.formData.autoPlay ? 'Y' : 'N';

            this.$http.post({
                url: this.formData.id ? '/aimTemplateAim/update' : '/aimTemplateAim/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: '/aimTemplateAim/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>
