<template>
  <div>
    <el-row>
      <el-col>
        <el-button @click="add()" type="success" style="margin-left: 20px;" size="mini"><i
            class="el-icon-plus"></i>
          新增
        </el-button>
        <div style="text-align: right;float:right;">
          <el-select
              v-model="queryTableName"
              placeholder="表名"
              style="width:150px;margin-left:20px"
              size="mini">
            <el-option
                v-for="region in tableNameOptions"
                :key="region.value"
                :label="region.label"
                :value="region.value">
            </el-option>
          </el-select>
          <el-select
              v-model="queryColumnName"
              placeholder="列名"
              style="width:150px;margin-left:20px"
              size="mini">
            <el-option
                v-for="cate in colNameOptions"
                :key="cate.value"
                :label="cate.label"
                :value="cate.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>
        <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-pagination
            style="padding: 15px"
            background
            layout="total, prev, pager, next, jumper"
            :total="total"
            :current-page="pageIndex"
            @current-change="onPageChange"
        ></el-pagination>
      </el-col>
    </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="字典KEY">
            <el-input v-model="form.dictCode" size="small" autocomplete="off"
                      :style="{width:formInputWidth}"></el-input>
          </el-form-item>
          <el-form-item label="字典VALUE">
            <el-input v-model="form.dictValue" size="small" autocomplete="off"
                      :style="{width:formInputWidth}"></el-input>
          </el-form-item>
          <el-form-item label="表名">
            <el-input v-model="form.tableName" size="small" autocomplete="off"
                      :style="{width:formInputWidth}"></el-input>
          </el-form-item>
          <el-form-item label="列名">
            <el-input v-model="form.columnName" size="small" autocomplete="off"
                      :style="{width:formInputWidth}"></el-input>
          </el-form-item>
          <el-form-item label="父字典KEY">
            <el-input v-model="form.parentDictCode" size="small" autocomplete="off"
                      :style="{width:formInputWidth}"></el-input>
          </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, Button, Input, Checkbox, Radio, Select, Option, Pagination, Message, MessageBox} from 'element-ui'
import myTable from "../components/CustomTable";
import metaDictApi from "../../js/api/metaDictApi"

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

export default {
  name: "MetaDictTab",
  components: {
    'myTable': myTable
  },
  data() {
    return {
      modify: false,
      pageIndex: 1,
      queryColumnName: "",
      queryTableName: "",
      colNameOptions: [],
      tableNameOptions: [],
      multipleSelection: [],
      dialogVisible: false,
      dialogTitle: "",
      form: {
        dictCode: "",
        dictValue: "",
        columnName: "",
        tableName: "",
        parentDictCode: ""
      },
      formLabelWidth: "100px",
      formInputWidth: "200px",
      saveLoading: false,
      deleteLoading: false
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    setDataBody() {
      let dataBody = {}
      if (this.queryColumnName.length > 0) {
        dataBody.columnName = this.queryColumnName;
      }
      if (this.queryTableName.length > 0) {
        dataBody.tableName = this.queryTableName;
      }
      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) {
        metaDictApi.add(this.form, (response) => {
          if (response.data.code === 200) {
            this.closeDialog();
            Message.success("添加成功")
          } else {
            this.closeDialog();
            Message.error("添加失败")
          }
        })
      } else {
        metaDictApi.update(this.form, (response) => {
          if (response.data.code === 200) {
            this.closeDialog();
            Message.success("更新成功")
          } else {
            this.closeDialog();
            Message.error("更新失败")
          }
        })
      }
    },
    deleteOne(id) {
      MessageBox.confirm("是否删除？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            metaDictApi.delete({id: id}, (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.queryTableName = ""
      this.queryColumnName = ""
      this.queryPageable({
        index: 0,
        size: 10,
        data: {},
      });
    },
    resetFormData() {
      this.form = {
        columnName: "",
        tableName: ""
      }
    }
    ,
    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: 'metaDict/queryPageable',
        })
  }
  ,
  computed: {
    metadata: function () {
      const temp = [];
      this.$store.state.metadata.metadata.forEach((element) => {
        if (element.tableName === "meta_dict") {
          let tempElement = {...element};
          tempElement.minWidth = tempElement.columnWidth + "%";
          tempElement.columnWidth = 0;
          temp.push(tempElement);
        }
      });
      return temp;
    }
    ,
    ...
        mapState({
          tableData: (state) => state.metaDict.table,
          total: (state) => state.metaDict.count,
          isLoading: (state) => state.metaDict.isLoading
        })
  }
  ,
  mounted() {
    this.refreshPage();
    metaDictApi.getColumnNameList(response => {
      if (response.data.code === 200) {
        const content = response.data.content;
        for (let i = 0; i < content.length; ++i) {
          this.colNameOptions.push({value: content[i], label: content[i]})
        }
      }
    })
    metaDictApi.getTableNameList(response => {
      if (response.data.code === 200) {
        const content = response.data.content;
        for (let i = 0; i < content.length; ++i) {
          this.tableNameOptions.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>