<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="150px" class="order_form">
      <el-form-item label="预览结果">
        <el-tag :color=ruleForm.labelColor :style="{'color':ruleForm.fontSizeColor}">{{ruleForm.labelName}}</el-tag>
      </el-form-item>
      <el-form-item label="标签名称" prop="labelName">
        <el-input v-model="ruleForm.labelName" placeholder="请输入标签名称"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="labelDescription">
        <el-input v-model="ruleForm.labelDescription" placeholder="请输入备注"></el-input>
      </el-form-item>
      <el-form-item label="标签颜色" prop="labelColor">
        <el-color-picker v-model="ruleForm.labelColor" @change='handleColor'></el-color-picker>
      </el-form-item>
      <el-form-item label="标签字体颜色" prop="fontSizeColor">
        <el-color-picker v-model="ruleForm.fontSizeColor" @change='handleFontSizeColor'></el-color-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getProviceCity, getDictionary } from '@/api/commonAPI'
import { addLable , editLable,getLableDetail} from '@/api/memberManagement/memberList'
import { validateTel } from '@/utils/validate'
import { filterContent } from '@/utils'

export default {
  name: 'AddOrEdit',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    callid: {
      type: Number,
      default: 0
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      props: { // 自定义省市下拉数据的key值
        value: 'code',
        label: 'name',
        children: 'entityJson'
      },
      sourceOptions: [], // 工单来源下拉数据
      ruleForm: {
        unique_id: '', //		否	string	callid
        id: '',
        fontSizeColor: '',//标签字体颜色
        labelName: '',//标签名称
        labelColor: '#fff',//标签颜色
        labelDescription: '',//标签描述
      },
      rules: {
        labelName: [{
          required: true,
          trigger: 'blur',
          message: '请输入标签名称',
        }]
      },
      loading: false
    }
  },
  created() {
    if (this.callid) {
      this.ruleForm.unique_id = this.callid
    }
    Promise.all([])
      .then(() => {
        if (this.rowid) {
          this.ruleForm.id = this.rowid
          this.getDetail(this.rowid)
        }
      })
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.ruleForm.content = filterContent.delHtmlTag(this.ruleForm.content)// 保存内容时去掉 html 标签
          this.loading = true
          return new Promise(response=>{
            const params={
              id:this.rowid ? this.rowid : '',//修改id值
              state:1,
              name:this.ruleForm.labelName,//标签名称
              labelcolor:this.ruleForm.labelColor,//标签颜色
              note:this.ruleForm.labelDescription,//标签描述
              fontcolor:this.ruleForm.fontSizeColor,//标签字体颜色
            }
              // 添加
            if (!this.rowid) {
              addLable(params).then(response => {
                this.loading = false
                if (response.state.toLowerCase() === 'success') {
                  this.$parent.$layer.close(this.layerid)
                  if (this.callid) {
                    this.$parent.getOrderRecord() // 重新加载来电弹屏工单记录
                  } else {
                    this.$parent.getList() // 重新加载父级数据
                  }
                  this.$message.success('恭喜你，添加成功!')
                }
              }).catch(() => {
                this.loading = false
              })
              return
            }
            // 编辑
            editLable(params).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$layer.close(this.layerid)
                this.$parent.getList() // 重新加载父级数据
                this.$message.success('恭喜你，编辑成功!')
              }
            }).catch(() => {
              this.loading = false
            })
          })
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    },
    // 详情
    getDetail(rid) {
      getLableDetail(rid).then(response => {
        if (response.state.toLowerCase() === 'success') {
          this.ruleForm.labelName = response.data.F_Name;
          this.ruleForm.labelColor = response.data.F_LabelColor;
          this.ruleForm.labelDescription = response.data.F_Note;
          this.ruleForm.fontSizeColor = response.data.F_FontColor;
        }
      })
    },
    handleColor(val){
      this.ruleForm.labelColor=val
    },
    handleFontSizeColor(val){
      this.ruleForm.fontSizeColor=val
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.order_form .form_select{
	width: 100%;
}
</style>