<template>
  <div class="patent-keywords-highlight">
    <el-container>
      <el-header class="patent-keywords-highlight-header">
        <el-switch v-model="form.enable" active-color="#13ce66" @change="onChange2"></el-switch>
        <div>
          <el-button type="primary" size="small" @click="handleAdd" :loading="btnLoading">新增</el-button>
          <el-button type="success" size="small" @click="handleSave">保存</el-button>
        </div>
      </el-header>
      <el-main class="patent-keywords-highlight-main">
        <div class="margin-top_10">
          <el-select v-model="selected" size="small" placeholder="请选择" class="width_100" @change="onChange">
            <el-option v-for="(item, index) in dataList" :label="item.name" :value="index"></el-option>
          </el-select>
          <div v-for="item in form.configs" class="margin-top_10 item">
            <el-color-picker v-model="item.color" size="small" @change="onChange2"></el-color-picker>
            <el-input v-model="item.keywords" placeholder="多个关键词请使用';(英文分号)'隔开" size="small" @input="onChange2(true)"></el-input>
          </div>
          <div class="delete-button">
            <el-button type="danger" size="small" @click="handleDelete" v-if="!form._default" :loading="btnLoading2">删除</el-button>
          </div>
        </div>
      </el-main>
    </el-container>

    <el-dialog :title="title" :visible.sync="visible" width="500px" append-to-body destroy-on-close :before-close="close" top="10vh">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="80px">
        <el-form-item label="模板名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入模板名称"></el-input>
        </el-form-item>
        <el-form-item v-for="item in form.configs">
          <template slot="label">
            <el-color-picker v-model="item.color" size="small"></el-color-picker>
          </template>
          <el-input v-model="item.keywords" placeholder="请输入专利高亮关键词"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ['projectId'],
  data() {
    return {
      dataList: [],
      selected: 0,
      visible: false,
      title: '管理高亮模板',
      form: {},
      btnLoading: false,
      btnLoading2: false,
      rules: {
        name: [{ required: true, message: '请输入专题库名称', trigger: 'blur' },],
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    close() {
      this.visible = false
    },
    onChange() {
      this.form = this.dataList[this.selected]
      this.onChange2()
    },
    onChange2(input) {
      if (!this.form.enable && input) {
        this.form.enable = true
      }
      this.$store.commit('SET_PATENT_HIGHLIGHT', this.form)
    },
    getList() {
      this.$api.getPatentKeywordsHighlight({ projectId: this.projectId }).then(response => {
        this.dataList = response.data
        this.selected = 0
        this.onChange()
      })
    },
    handleAdd() {
      this.btnLoading = true
      this.$api.addDefaultPatentKeywordsHighlight({ projectId: this.projectId }).then(response => {
        this.dataList.push(response.data)
        this.$message.success('操作成功')
        this.btnLoading = false
        this.selected = this.dataList.length - 1
        this.onChange()
      }).catch(error => {
        this.btnLoading = false
      })
    },
    handleSave() {
      this.visible = true
    },
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true
          this.$api.updatePatentKeywordsHighlight(this.form).then(response => {
            this.$message.success('操作成功')
            this.btnLoading = false
            this.visible = false
            this.onChange2()
          }).catch(error => {
            this.btnLoading = false
          })
        }
      })
    },
    handleDelete() {
      this.btnLoading2 = true
      this.$api.deletePatentKeywordsHigh({ id: this.form.id }).then(response => {
        this.$message.success('操作成功')
        this.btnLoading2 = false
        this.dataList.splice(this.selected, 1)
        this.selected = this.selected - 1
        this.onChange()
      }).catch(error => {
        this.btnLoading2 = false
      })
    }
  }
}
</script>

<style lang="scss">
.patent-keywords-highlight {
  .patent-keywords-highlight-header {
    padding: 25px 0 !important;
    height: 40px;
  }

  .patent-keywords-highlight-main {
    padding: 0;
    display: contents;

    .item {
      .el-input {
        width: 88%;
        float: right;
      }
    }

    .delete-button {
      margin-top: 10px;
      text-align: right;
    }
  }
}
</style>