<template>
  <el-dialog
    title="团队组合成员"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @close="closeDialog">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="团队组合名称" prop="seriesName" label-width="100">
        <span>{{dataForm.seriesName}}</span>
      </el-form-item>
      <el-form-item label="成员名称" prop="seriesUsage">
        <el-tag
          :key="tag"
          v-for="tag in dynamicTags"
          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>
    <span slot="footer" class="dialog-footer">
      <el-button class="default-btn" @click="visible = false">{{$t("crud.filter.cancelBtn")}}</el-button>
      <el-button class="default-btn primary-btn" type="primary" @click="dataFormSubmit()">{{$t("crud.filter.submitBtn")}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        seriesId: null,
        seriesName: null,
        shopId: 1
      },
      personList:[],
      dynamicTags:[],
      dataRule: {
      },
      inputVisible: false,
      inputValue: null,
    }
  },
  methods: {
    init (series) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.dataForm.seriesId = series.seriesId || 0
        this.dataForm.seriesName = series.seriesName || 0
        if (this.dataForm.seriesName) {
          this.$http({
            url: this.$http.adornUrl('/cardLib/cardPerson/list'),
            method: 'get',
            params: {seriesName: series.seriesName}
          }).then(({data}) => {
            this.dynamicTags = data.map(item => item.personName)
          })
        }
      })
    },
    handleClose(person) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(person), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    closeDialog(){
      this.dynamicTags = []
      this.personList =[]
      this.inputValue = null
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.dynamicTags.forEach(item =>{
            this.personList.push({seriesId: this.dataForm.seriesId, seriesName: this.dataForm.seriesName, personName: item})
          });
          this.$http({
            url: this.$http.adornUrl('/cardLib/cardPerson/savePerson'),
            method: 'post',
            data: this.$http.adornData({seriesId: this.dataForm.seriesId, seriesName: this.dataForm.seriesName, personList: this.personList})
          }).then(({data}) => {
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
              duration: 200,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    }
  }
}
</script>
<style scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
