<template>
    <div class="edit-view">
        <page-title>
            <el-button @click="$router.go(-1)" :disabled="saving">取消</el-button>
            <el-button @click="onDelete" :disabled="saving" type="danger" v-if="formData.id"> 删除 </el-button>
            <el-button @click="onSave" :loading="saving" type="primary">保存</el-button>
        </page-title>
        <div class="edit-view__content-wrapper">
            <div class="edit-view__content-section">
                <el-form
                    :model="formData"
                    :rules="rules"
                    ref="form"
                    label-width="152px"
                    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="remark" label="备注">
                        <el-input v-model="formData.remark"></el-input>
                    </el-form-item>
                    <el-form-item prop="addForExist" label="为现有藏品添加标签" v-if="!formData.id">
                        <el-radio v-model="formData.addForExist" :label="true">是</el-radio>
                        <el-radio v-model="formData.addForExist" :label="false">否</el-radio>
                    </el-form-item>
                    <el-form-item prop="addMode" label="添加模式" v-if="formData.addForExist === true">
                        <el-radio v-model="formData.addMode" label="easy">名称包含</el-radio>
                        <el-radio v-model="formData.addMode" label="regex">正则匹配</el-radio>
                    </el-form-item>
                    <el-form-item prop="pattern" label="匹配内容" v-if="formData.addForExist === true">
                        <el-input v-model="formData.pattern"></el-input>
                    </el-form-item>
                    <el-form-item class="form-submit">
                        <el-button @click="onSave" :loading="saving" type="primary"> 保存 </el-button>
                        <el-button @click="onDelete" :disabled="saving" type="danger" v-if="formData.id">
                            删除
                        </el-button>
                        <el-button @click="$router.go(-1)" :disabled="saving">取消</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
        <el-dialog width="300px" :visible.sync="showDialog">
            <div v-loading="checkAddLoading">
                <div v-if="checkResult">
                    即将为{{ checkResult.collection }}个collection，{{ checkResult.asset }}个asset添加标签，是否继续?
                </div>
            </div>
            <div slot="footer">
                <el-button @click="showDialog = false" size="mini">取消</el-button>
                <el-button @click="submit(true)" type="primary" size="mini">继续</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    name: 'TagEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('tag/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: {
                addForExist: false,
                addMode: 'easy',
                pattern: ''
            },
            rules: {
                name: [
                    {
                        required: true,
                        message: '请输入名称',
                        trigger: 'blur'
                    }
                ],
                pattern: [
                    {
                        required: true,
                        message: '请输入匹配内容',
                        trigger: 'blur'
                    }
                ]
            },
            showDialog: false,
            checkAddLoading: false
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit(skipDialog) {
            if (!this.formData.id && this.formData.addForExist === true) {
                if (skipDialog) {
                    this.showDialog = false;
                } else {
                    this.showDialog = true;
                    this.checkAddLoading = 'loading';
                    this.checkResult = null;
                    this.$http.get('/tag/checkAdd', this.formData).then(res => {
                        this.checkAddLoading = false;
                        this.checkResult = res;
                    });
                    return;
                }
            }
            let data = { ...this.formData };

            this.saving = true;
            this.$http
                .post(this.formData.id ? '/tag/save' : '/tag/create', data, { body: this.formData.id ? '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.$confirm('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/tag/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>
