<template>
    <div class="edit-view">
        <el-form
            :model="formData"
            :rules="rules"
            ref="form"
            label-width="80px"
            label-position="right"
            size="small"
            style="max-width: 800px;"
        >
            <el-form-item prop="name" label="套餐名称">
                <el-input v-model="formData.name" class="input-title"></el-input>
            </el-form-item>
            <el-form-item prop="title" label="标题">
                <el-input v-model="formData.title" class="input-title"></el-input>
            </el-form-item>
            <el-form-item prop="img" label="图">
                <multi-upload v-model="formData.img"></multi-upload>
            </el-form-item>
            <el-form-item prop="tag" label="套餐标签">
                <el-tag
                    :key="i"
                    v-for="(tag, i) in formData.tag"
                    closable
                    :disable-transitions="false"
                    @close="removeTag(i)"
                    size="medium"
                >
                    {{ tag }}
                </el-tag>
                <el-input
                    class="input-new-tag"
                    v-if="inputVisible"
                    v-model="inputValue"
                    ref="saveTagInput"
                    size="small"
                    @keyup.enter.native="handleInputConfirm"
                    @blur="handleInputConfirm"
                >
                </el-input>
                <el-button v-else class="button-new-tag" size="small" @click="showInput">新建标签</el-button>
            </el-form-item>
            <el-form-item prop="amount" label="金额">
                <el-input-number type="number" v-model="formData.amount"></el-input-number>
            </el-form-item>
            <el-form-item prop="detail" label="详情">
                <rich-text v-model="formData.detail"></rich-text>
            </el-form-item>
            <el-form-item prop="type" label="套餐类型">
                <el-select v-model="formData.type" clearable filterable placeholder="请选择">
                    <el-option v-for="item in typeOptions" :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: 'PackageEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('package/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: {
                img: [],
                tag: []
            },
            rules: {
                name: [
                    {
                        required: true,
                        message: '请输入套餐名称',
                        trigger: 'blur'
                    }
                ],
                amount: [
                    {
                        required: true,
                        message: '请输入金额',
                        trigger: 'blur'
                    }
                ],
                type: [
                    {
                        required: true,
                        message: '请输入套餐类型',
                        trigger: 'blur'
                    }
                ]
            },
            typeOptions: [
                { label: '团队', value: 'TEAM' },
                { label: '个人', value: 'PERSONAL' }
            ],
            inputVisible: false,
            inputValue: ''
        };
    },
    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('/package/save', data, { body: 'json' })
                .then(res => {
                    this.saving = false;
                    this.$message.success('成功');
                    this.$emit('next', 'second');
                    this.$router.replace({
                        name: 'PackageEdit',
                        query: {
                            id: res.id
                        }
                    });
                })
                .catch(e => {
                    console.log(e);
                    this.saving = false;
                    this.$message.error(e.error);
                });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/package/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);
                    }
                });
        },
        removeTag(i) {
            this.formData.tag.splice(i, 1);
        },
        showInput() {
            this.inputVisible = true;
            this.$nextTick(_ => {
                this.$refs.saveTagInput.$refs.input.focus();
            });
        },
        handleInputConfirm() {
            let inputValue = this.inputValue;
            if (inputValue) {
                this.formData.tag.push(inputValue);
            }
            this.inputVisible = false;
            this.inputValue = '';
        }
    }
};
</script>
<style lang="less" scoped>
.el-tag + .el-tag {
    margin-left: 10px;
}
.button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
}
.input-new-tag {
    width: 100px;
    margin-left: 10px;
    vertical-align: bottom;
}
.input-title {
    width: 350px;
}
</style>
