<template>
    <div>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="标签库" >
                    <el-select @change="tagChange" v-model="tag" placeholder="请选择" clearable size="small">
                        <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">
                    <el-tag
                        :key="tag"
                        v-for="tag in tags"
                        closable
                        :disable-transitions="false"
                        @close="handleClose(tag)">
                        {{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>
            <div  class="footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </div>
    </div>
</template>
  
<script>
    import { updateCustomer  } from "@/api/crm/customer";
    export default {
        name: "visit",
        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){
                console.log(e)
                var v=this.tagsOptions.find(value=>value.dictValue==e);
                console.log(v)
                this.tags.push(v.dictLabel);
                this.form.tags=this.tags.toString();

            },
            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;
                if (inputValue) {
                    this.tags.push(inputValue);
                }
                this.inputVisible = false;
                this.inputValue = '';
                this.form.tags=this.tags.toString();
            },
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        updateCustomer(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("操作成功");
                                
                                this.$emit('close');
                            }
                        });
                    }
                });
            },
        }
    };
</script>
<style lang="scss" scoped>
.contents{
    height: 100%;
    background-color: #fff;
    padding: 20px;
        
}
.footer{
    display: flex;
    align-items: center;
    justify-content: flex-end;
}
</style>



 