<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-input v-model="queryName" size="mini" autocomplete="off" placeholder="规则名称"
                    style="width:150px;margin-left:20px"
          ></el-input>
          <el-button @click="query" type="primary" size="mini" style="margin-left:20px"><i
              class="el-icon-search"></i> 查询
          </el-button>
          <el-button @click="refreshPage" 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>
    <filterDialog :dialogTitle="dialogTitle" :dialogVisible="dialogVisible" :form="form" :closeDialog="closeDialog"
                  :submit="submit" :wordBankCategoryList="wordBankCategoryList"
    ></filterDialog>
  </div>
</template>

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

Vue.use(Card);
Vue.use(Button);
Vue.use(Input);
Vue.use(InputNumber);
Vue.use(Checkbox);
Vue.use(Col);
Vue.use(Radio);
Vue.use(Select);
Vue.use(Option);
Vue.use(Pagination);
export default {
  name: "filterTab",
  components: {
    'myTable': myTable,
    'filterDialog': filterDialog
  },
  data() {
    return {
      modify: false,
      pageIndex: 1,
      queryName: "",
      multipleSelection: [],
      dialogVisible: false,
      dialogTitle: "",
      form: {
        filterName: "",
        startUrl: "",
        titleIn: "",
        titleEx: "",
        contentIn: "",
        contentEx: "",
        urlIn: "",
        urlEx: "",
        globalIn: [],
        globalEx: ["0", "2", "3", "4"]
      },
      formLabelWidth: "100px",
      formInputWidth: "200px",
      deleteLoading: false,
      wordBankCategoryList: [],

    }
  },
  methods: {
    handleSelectionChange(val) {
      console.log(val);

    },
    setDataBody() {
      let dataBody = {}
      if (this.queryName.length > 0) {
        dataBody.filterName = this.queryName;
      }
      return dataBody;
    },
    refreshPage() {
      this.saveLoading = false;
      this.pageIndex = 1;
      this.queryName = "";
      let dataBody = this.setDataBody();
      this.queryPageable({
        index: this.pageIndex - 1,
        size: 10,
        data: dataBody,
      });
    },
    onPageChange(index) {
      this.pageIndex = index;
      let dataBody = this.setDataBody()
      this.queryPageable({
        index: index - 1,
        size: 10,
        data: dataBody
      });
    },
    add() {
      this.resetFormData();
      this.dialogTitle = "添加记录";
      this.dialogVisible = true;
    },
    edit(data) {
      this.dialogTitle = "修改记录";
      this.form = JSON.parse(JSON.stringify(data))
      const globalIn = JSON.parse(this.form.globalIn);
      const globalEx = JSON.parse(this.form.globalEx);
      this.form.globalIn = globalIn;
      this.form.globalEx = globalEx;
      console.log(this.form)
      this.dialogVisible = true;
    },
    deleteOne(id) {
      console.log(id);
      MessageBox.confirm("是否删除？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            console.log(id);
            Message.success("删除成功")

          })
          .catch(() => {
          });
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    submit() {
      this.saveLoading = true;
      let dataBody = {}
      for (let formField in this.form) {
        if (formField.startsWith('global')) {
          dataBody[formField] = JSON.stringify(this.form[formField]);
        } else {
          dataBody[formField] = this.form[formField];
        }
      }
      if (this.form.id === undefined) {
        rulesFilterApi.add(dataBody, (response) => {
          if (response.data.code === 200) {
            this.closeDialog();
            Message.success("添加成功")
          } else {
            this.closeDialog();
            Message.error("添加失败")
          }
        })
      } else {
        rulesFilterApi.update(dataBody, (response) => {
          if (response.data.code === 200) {
            this.closeDialog();
            Message.success("更新成功")
          } else {
            this.closeDialog();
            Message.error("更新失败")
          }
        })
      }
      this.saveLoading = false;
    },
    query() {
      this.pageIndex = 1;
      let dataBody = this.setDataBody();
      this.queryPageable({
        index: 0,
        size: 10,
        data: dataBody,
      });
    },
    resetFormData() {
      this.form = {
        filterName: "",
        startUrl: "",
        titleIn: "",
        titleEx: "",
        contentIn: "",
        contentEx: "",
        urlIn: "",
        urlEx: "",
        globalIn: [],
        globalEx: ["0", "2", "3", "4"],
        lengthLimit: 0
      }
    },
    buildDictTree(data, columnString) {
      let siteOptions = []
      let nodeList = []
      data.forEach(e => {
        if (e.columnName === columnString)
          nodeList.push({value: e.dictCode, label: e.dictValue, children: undefined, parentDictCode: e.parentDictCode});
      })
      nodeList.forEach(e1 => {
        if (e1.parentDictCode !== undefined) {
          nodeList.forEach(e2 => {
            if (e1.parentDictCode === e2.value) {
              if (e2.children === undefined) {
                e2.children = []
              }
              e2.children.push(e1);
            }
          })
        } else {
          siteOptions.push(e1);
        }
      })
      return siteOptions;
    },
    ...
        mapMutations({}),
    ...
        mapActions({
          queryPageable: 'rulesFilter/queryPageable',
        })
  }
  ,
  computed: {
    metadata: function () {
      const temp = [];
      this.$store.state.metadata.metadata.forEach((element) => {
        if (element.tableName === "rules_filter") {
          let tempElement = {...element};
          tempElement.minWidth = tempElement.columnWidth + "%";
          tempElement.columnWidth = 0;
          temp.push(tempElement);
        }
      });
      return temp;
    },
    ...
        mapState({
          tableData: (state) => state.rulesFilter.table,
          total: (state) => state.rulesFilter.count,
          isLoading: (state) => state.rulesFilter.isLoading
        })
  },
  mounted() {
    this.refreshPage();
    metaDictApi.queryByTableName('word_bank', (response) => {
      if (response.data.code === 200) {
        this.wordBankCategoryList = this.buildDictTree(response.data.content, 'category');
      }
    })
  }
}
</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>