<template>
    <div class="tag-dialog-content">
        <div class="dialog-header">
            <i class="el-icon-price-tag header-icon"></i>
            <span class="header-title">为客户添加标签</span>
        </div>
        
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="tag-form">
            <el-form-item label="标签库">
                <el-select @change="tagChange" v-model="tag" placeholder="从标签库选择" clearable size="medium" style="width: 100%;">
                    <el-option
                            v-for="item in tagsOptions"
                            :key="item.dictValue"
                            :label="item.dictLabel"
                            :value="item.dictValue"
                        />
                </el-select>
            </el-form-item>
            <el-form-item label="已选标签" prop="tags">
                <div class="tags-container">
                    <el-tag
                        :key="tag"
                        v-for="tag in tags"
                        closable
                        :disable-transitions="false"
                        class="selected-tag"
                        @close="handleClose(tag)">
                        {{tag}}
                    </el-tag>
                    <el-input
                        class="input-new-tag"
                        v-if="inputVisible"
                        v-model="inputValue"
                        ref="saveTagInput"
                        size="small"
                        placeholder="输入新标签"
                        @keyup.enter.native="handleInputConfirm"
                        @blur="handleInputConfirm"
                    >
                    </el-input>
                    <el-button v-else class="button-new-tag" size="small" icon="el-icon-plus" @click="showInput">自定义标签</el-button>
                </div>
                <div class="form-tip" v-if="tags.length === 0">
                    <i class="el-icon-info"></i>
                    <span>请从标签库选择或自定义添加标签</span>
                </div>
            </el-form-item>
        </el-form>
    </div>
</template>
  
<script>
    import { updateCustomer  } from "@/api/crm/customer";
    export default {
        name: "AddTag",
        data() {
            return {
                tag:null,
                tagsOptions:[],
                inputVisible: false,
                inputValue: '',
                tags:[],
                form: {
                    customerId:null,
                    tags:null,
                 
                },
                rules: {
                    tags: [
                        { required: true, message: "请至少选择一个标签", trigger: "change" }
                    ],
             
                }
                 
            };
        },
        created() {
            this.getDicts("crm_customer_tag").then((response) => {
                this.tagsOptions = response.data;
            });
        },
        methods: {
            tagChange(e){
                var v=this.tagsOptions.find(value=>value.dictValue==e);
                if (v && !this.tags.includes(v.dictLabel)) {
                    this.tags.push(v.dictLabel);
                    this.form.tags=this.tags.toString();
                }
                this.tag = null;
            },
            reset(customer) {
                this.tags= [];
                this.form.customerId=customer.customerId;
                this.form.tags=customer.tags;
                if(this.form.tags!=null){
                    this.tags=this.form.tags.split(",")
                }
            },
            handleClose(tag) {
                this.tags.splice(this.tags.indexOf(tag), 1);
                this.form.tags=this.tags.toString();
            },
            showInput() {
                this.inputVisible = true;
                this.$nextTick(_ => {
                    this.$refs.saveTagInput.focus();
                });
            },
            handleInputConfirm() {
                let inputValue = this.inputValue.trim();
                if (inputValue && !this.tags.includes(inputValue)) {
                    this.tags.push(inputValue);
                    this.form.tags=this.tags.toString();
                }
                this.inputVisible = false;
                this.inputValue = '';
            },
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        updateCustomer(this.form).then(response => {
                            if (response.code === 200) {
                                this.$message.success("操作成功");
                                this.$emit('close');
                            }
                        });
                    }
                });
            },
        }
    };
</script>
<style lang="scss" scoped>
.tag-dialog-content {
    padding: 10px 0;
    
    .dialog-header {
        display: flex;
        align-items: center;
        margin-bottom: 24px;
        
        .header-icon {
            font-size: 24px;
            color: #e6a23c;
            margin-right: 12px;
        }
        
        .header-title {
            font-size: 16px;
            font-weight: 600;
            color: #303133;
        }
    }
    
    .tag-form {
        .tags-container {
            display: flex;
            flex-wrap: wrap;
            gap: 8px;
            align-items: center;
            min-height: 40px;
        }
        
        .selected-tag {
            margin: 0;
        }
        
        .button-new-tag {
            background: #f5f7fa;
            border: 1px dashed #d9d9d9;
            color: #606266;
            
            &:hover {
                background: #ecf5ff;
                border-color: #409eff;
                color: #409eff;
            }
        }
        
        .form-tip {
            margin-top: 8px;
            font-size: 12px;
            color: #909399;
            display: flex;
            align-items: center;
            gap: 4px;
        }
    }
}
</style>
