<template>
  <div>
    <el-button size="mini" type="text" @click="addBtn">编 辑</el-button>
    <el-dialog title="新 增" append-to-body :visible.sync="dialogFormVisible" width="500px">
      <div slot="title" style="font-size: 15px">
        编辑标签
      </div>
      <el-row :gutter="20">
        <el-form :model="form" :rules="rules" ref="form" label-width="90px" label-position="left" size="mini">
          <el-col :span="24">
            <el-form-item label="新增标签" prop="tag">
              <el-input :disabled="!!data.issystem" v-model="form.tag" placeholder="请输入标签名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="helptips">
              <el-input type="textarea" v-model="form.helptips" placeholder="请输入标签描述"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="互斥范围" prop="result">
              <el-select v-model="form.result" multiple placeholder="请选择标签">
                <el-option
                  v-for="item in allData"
                  :key="item.sys_systemtagdetailid"
                  :label="item.tag"
                  :value="item.sys_systemtagdetailid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="warning" @click="onSubmit" class="normal-btn-width btn-warning">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "add",
  props:['allData','data'],
  inject:['sys_systemtagid'],
  data(){
    return {
      dialogFormVisible:false,
      form:{
        tag:'',
        result:'',
        helptips:'',
        sys_systemtagid:'',
        sys_systemtagdetailid:''
      },
      result:[],
      rules:{
        tag:[
          { required: true, message: '请输入标签名', trigger: 'blur'},
        ],
      }
    }
  },
  methods:{
    addBtn () {
      this.dialogFormVisible = true
      this.form = Object.assign({},this.form,this.data)
      this.form.result = this.form.mutextags.map(item => {
        return item.sys_systemtagdetailid
      })
      
    },
    onSubmit(){
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        this.form.sys_systemtagid = this.sys_systemtagid()
        const res = await this.$api.requested({
          "id": "20221013104501",
          "version":1,
          "content": this.form
        })
        this.tool.showMessage(res,async()=>{
          let mutextags = this.form.result.map(item => {
            return {
              "sys_systemtagdetailid": item,
            }
          })
          if (mutextags.length > 0) {
            let res2 = await this.$api.requested({
              "id":20221014122501,
              "content": {
                "sys_systemtagid": res.data.sys_systemtagid,
                "sys_systemtagdetailid": res.data.sys_systemtagdetailid,
                "mutextags": mutextags,
              }
            })
          }
          this.dialogFormVisible = false
          this.$refs['form'].resetFields();
          this.$emit('onSuccess')
          
        })
      })
    }
  }
}
</script>

<style scoped>
/deep/.el-select {
  width: 100%;
}
</style>