<template>
  <div>
    <el-row>
      <el-col :span="4">
        <el-button @click="add()" type="success" style="margin-left: 20px;" size="mini"><i
            class="el-icon-plus"></i>
          新增
        </el-button>
      </el-col>
      <el-col :span="20">
        <div style="text-align: right">
          <el-input style="width:200px;" size="mini" v-model="queryTargetValue"
                    placeholder="目标value值"></el-input>
          <el-input style="width:200px;margin-left:20px" size="mini" v-model="queryTargetName"
                    placeholder="目标名称"></el-input>
          <el-input style="width:200px;margin-left:20px" size="mini" v-model="queryTargetCnName"
                    placeholder="目标中文名"></el-input>
          <el-select
              v-model="queryTargetType"
              placeholder="类别"
              style="width:100px;margin-left:20px"
              size="mini">
            <el-option
                v-for="option in targetTypeOptions"
                :key="option.value"
                :label="option.label"
                :value="option.value">
            </el-option>
          </el-select>
          <el-select
              v-model="queryTargetCountry"
              placeholder="所在国"
              style="width:100px;margin-left:20px"
              size="mini">
            <el-option
                v-for="option in targetCountryOptions"
                :key="option.value"
                :label="option.label"
                :value="option.value">
            </el-option>
          </el-select>
          <el-button @click="query" type="primary" size="mini" style="margin-left:20px"><i
              class="el-icon-search"></i> 查询
          </el-button>
          <el-button @click="resetSearch" type="info" size="mini" style="margin-left:20px"><i
              class="el-icon-refresh"></i> 重置
          </el-button>
        </div>
      </el-col>
      <el-col :span="24">
        <myTable :metaData="metadata" :tableData="tableData" :isLoading="isLoading"
                 @selection-change="handleSelectionChange">
          <template #default="{ index }">
            <el-button-group>
              <el-button size="small" icon="fa fa-edit" @click="edit(tableData[index])"></el-button>
              <el-button size="small" icon="fa fa-trash" @click="deleteOne(tableData[index].id)"></el-button>
            </el-button-group>
          </template>
        </myTable>
      </el-col>
      <el-pagination
          style="padding: 15px"
          background
          layout="total, prev, pager, next, jumper"
          :total="total"
          :current-page="pageIndex"
          @current-change="onPageChange"
      ></el-pagination>
    </el-row>
    <el-dialog :title="dialogTitle"
               :visible="dialogVisible"
               width="450px"
               center
               :destroy-on-close="true"
               :show-close="false"
    >
      <div class="dialogForm">
        <el-form :model="form" :label-width="formLabelWidth">
          <el-form-item label="Value">
            <el-input v-model="form.targetValue" size="small" autocomplete="off"
                      :style="{width:formInputWidth}"></el-input>
          </el-form-item>
          <el-form-item label="名称">
            <el-input v-model="form.targetName" size="small" autocomplete="off"
                      :style="{width:formInputWidth}"></el-input>
          </el-form-item>
          <el-form-item label="中文名">
            <el-input v-model="form.targetCnName" size="small" autocomplete="off"
                      :style="{width:formInputWidth}"></el-input>
          </el-form-item>
          <el-form-item label="分类">
            <el-select
                v-model="form.targetType" size="small"
                placeholder="请选择"
                collapse-tags
                :style="{width:formInputWidth}"
            >
              <el-option
                  v-for="item in targetTypeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="国家">
            <el-select
                v-model="form.targetCountry" size="small"
                placeholder="请选择"
                collapse-tags
                :style="{width:formInputWidth}"
            >
              <el-option
                  v-for="item in targetCountryOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
          <el-button size="small" type="success" @click="submit" :loading="saveLoading">保存</el-button>
          <el-button style="margin-left: 10px;" size="small" type="primary" @click="btn_close">关闭</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import {mapState, mapActions, mapMutations} from 'vuex'
import Vue from 'vue'
import {
  Card,
  Col,
  Row,
  Button,
  Input,
  Checkbox,
  Radio,
  Select,
  Option,
  Pagination,
  Message,
  MessageBox
} from 'element-ui'
import myTable from "../components/CustomTable";
import sensitiveTargetApi from "../../js/api/targetSensitiveApi"

Vue.use(Card);
Vue.use(Button);
Vue.use(Input);
Vue.use(Checkbox);
Vue.use(Col);
Vue.use(Row);
Vue.use(Radio);
Vue.use(Select);
Vue.use(Option);
Vue.use(Pagination);

export default {
  name: "SensitiveTargetTab",
  components: {
    'myTable': myTable
  },
  data() {
    return {
      modify: false,
      pageIndex: 1,
      queryTargetValue: "",
      queryTargetName: "",
      queryTargetCnName: "",
      queryTargetType: "",
      queryTargetCountry: "",
      targetCountryOptions: [],
      targetTypeOptions: [
        {value: "ship", label: "船舶"},
        {value: "plane", label: "飞机"}
      ],
      multipleSelection: [],
      showSensitiveWord: false,
      dialogVisible: false,
      dialogTitle: "",
      form: {
        targetValue: "",
        targetName: "",
        targetCnName: "",
        targetType: "",
        targetCountry: "",
      },
      formLabelWidth: "60px",
      formInputWidth: "200px",
      saveLoading: false,
      deleteLoading: false
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    setDataBody() {
      let dataBody = {
        delFlag: 0
      }
      if (this.queryTargetValue.length > 0) {
        dataBody.targetValue = this.queryTargetValue;
      }
      if (this.queryTargetName.length > 0) {
        dataBody.targetName = this.queryTargetName;
      }
      if (this.queryTargetCnName.length > 0) {
        dataBody.targetCnName = this.queryTargetCnName;
      }
      if (this.queryTargetType.length > 0) {
        dataBody.targetType = this.queryTargetType;
      }
      if (this.queryTargetCountry.length > 0) {
        dataBody.targetCountry = this.queryTargetCountry;
      }
      return dataBody;
    },
    onPageChange(index) {
      this.pageIndex = index;
      let dataBody = this.setDataBody();
      this.queryPageable({
        index: index - 1,
        size: 10,
        data: dataBody,
      });
    },
    add() {
      this.dialogTitle = "添加目标";
      this.saveLoading = false;
      this.dialogVisible = true;
      this.resetFormData();
    },
    edit(data) {
      this.dialogTitle = "修改目标";
      this.form = data;
      this.saveLoading = false;
      this.dialogVisible = true;
    },
    submit() {
      this.saveLoading = true;
      if (this.form.id === undefined) {
        sensitiveTargetApi.add(this.form, (response) => {
          if (response.data.code === 200) {
            this.closeDialog();
            Message.success("添加成功")
          } else {
            this.closeDialog();
            Message.error("添加失败")
          }
        })
      } else {
        sensitiveTargetApi.update(this.form, (response) => {
          if (response.data.code === 200) {
            this.closeDialog();
            Message.success("更新成功")
          } else {
            this.closeDialog();
            Message.error("更新失败")
          }
        })
      }
    },
    deleteOne(id) {
      sensitiveTargetApi.deleteByIds([id], (response) => {
        if (response.data.code === 200) {
          Message.success("删除成功")
          this.refreshPage()
        } else {
          this.refreshPage()
        }
      })
    },
    deleteMulti() {
      if (this.multipleSelection.length > 0) {
        MessageBox.confirm("是否删除？", "提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning",
        })
            .then(() => {
              let ids = []
              for (let i = 0; i < this.multipleSelection.length; ++i) {
                ids.push(this.tableData[i].id);
              }
              sensitiveTargetApi.deleteByIds(ids, (response) => {
                if (response.data.code === 200) {
                  Message.success("删除成功")
                  this.refreshPage()
                } else {
                  this.refreshPage()
                }
              })
            })
            .catch(() => {
            });
      }
    },
    closeDialog() {
      this.saveLoading = false;
      this.dialogVisible = false;
      this.refreshPage();
    },
    query() {
      this.pageIndex = 1;
      let dataBody = this.setDataBody();
      this.queryPageable({
        index: 0,
        size: 10,
        data: dataBody,
      });
    },
    resetSearch() {
      this.pageIndex = 1;
      this.queryTargetValue = ""
      this.queryTargetName = ""
      this.queryTargetCnName = ""
      this.queryTargetType = ""
      this.queryTargetCountry = ""
      this.queryPageable({
        index: 0,
        size: 10,
        data: this.setDataBody(),
      });
    },
    resetFormData() {
      this.form = {
        targetValue: "",
        targetName: "",
        targetCnName: "",
        targetType: "",
        targetCountry: "",
      }
    }
    ,
    btn_close() {
      this.dialogVisible = false;
      this.resetFormData();
    },
    refreshPage() {
      this.saveLoading = false;
      let dataBody = this.setDataBody();
      this.queryPageable({
        index: this.pageIndex - 1,
        size: 10,
        data: dataBody,
      });
    }
    ,
    ...
        mapMutations({}),
    ...
        mapActions({
          queryPageable: 'sensitiveTarget/queryPageable'
        })
  }
  ,
  computed: {
    metadata: function () {
      const temp = [];
      this.$store.state.metadata.metadata.forEach((element) => {
        if (element.tableName === "target_sensitive") {
          let tempElement = {...element};
          tempElement.minWidth = tempElement.columnWidth + "%";
          tempElement.columnWidth = 0;
          temp.push(tempElement);
        }
      });
      return temp;
    }
    ,
    ...
        mapState({
          tableData: (state) => state.sensitiveTarget.words,
          total: (state) => state.sensitiveTarget.count,
          isLoading: (state) => state.sensitiveTarget.isLoading
        })
  }
  ,
  mounted() {
    this.refreshPage();
    sensitiveTargetApi.getCountryList((response) => {
      if (response.data.code === 200) {
        const content = response.data.content;
        for (let i = 0; i < content.length; ++i) {
          this.targetCountryOptions.push({
            value: content[i],
            label: content[i]
          })
        }
      }
    })
  }
}
</script>

<style scoped>
.height {
  margin-top: 20px;
}

.table {
  margin-top: 20px;
}

.dialogForm {
  display: flex;
  justify-content: center;
}

.demo-form {
  width: 400px;
}

.box-card .text_label {
  text-align: right;
  vertical-align: middle;
  float: left;
  font-size: 14px;
  color: #606266;
  padding: 0 12px 0 0;
  box-sizing: border-box;
  width: 100%;
  line-height: 28px;
}

.box-card {
  background-color: #f8f8f8;
  margin-bottom: 10px;
  padding-bottom: 15px;
}

.box-card .title {
  text-align: center;
  font-size: 18px;
  padding: 10px;
  color: #666;
}

.box-card .dot {
  display: inline-block;
  vertical-align: middle;
  width: 6px;
  height: 6px;
  margin: 0 15px;
  background-color: #0080e5;
}

.el-input__inner {
  height: 28px !important;
  line-height: 28px !important;
}

.card-item {
  height: 41px;
  margin-top: 10px;
}

.el-cascader--mini {
  width: 100%;
}

.logintype_content {
  margin: 5px;
  border: 1px solid rgb(221, 221, 221);
  background-color: rgb(242, 242, 242);
  padding: 10px 10px 0 10px;
}

.el-page-header, .el-page-header__content {
  color: #666 !important;
}

.parse-button-icon {
  text-align: center;
  color: #409EFF;
}

.el-radio {
  margin-top: 5px;
}

.el-button:focus, .el-button:hover {
  margin-right: 0px;
}

.el-pagination {
  display: flex;
  justify-content: center;
}

</style>