<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="规则名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入规则名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row>
      <el-col>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:ruleManagement:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="ruleList"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="序号" type="index" :index="indexFormat"></el-table-column>
      <el-table-column prop="name" label="规则名称" width="400"></el-table-column>
      <el-table-column prop="jsons" label="规则内容"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:ruleManagement:add']"
          >新增</el-button>-->

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:ruleManagement:edit']"
          >修改</el-button>

          <el-button
            v-if="scope.row.parentId != 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:ruleManagement:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改规则 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" style="height: 800px" append-to-body>
      <el-form ref="form" :model="form"  label-width="80px">

        <el-row>
          <el-col style="width: 50%">
            <el-form-item label="规则名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入规则名称" />
            </el-form-item>
          </el-col>
          <el-col style="width: 1px;margin: 10px;">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="lableAdd"
              v-hasPermi="['system:ruleManagement:add']"
            >添加</el-button>
          </el-col>
        </el-row>
        <table cellspacing="0" cellpadding="0" border="1" style="border-color: aliceblue;">
          <thead style="font-size: 20px">
            <tr>
              <th style="text-align: center;width: 177px;">导入sheet</th>
              <th style="text-align: center;width: 177px;">导入行</th>
              <th style="text-align: center;width: 177px;">导入列</th>
              <th style="text-align: center;width: 177px;">模板sheet</th>
              <th style="text-align: center;width: 177px;">模板行</th>
              <th style="text-align: center;width: 177px;">模板列</th>
            </tr>
          </thead>
          <tbody id="labadd">
            <!--<tr style="margin-top: 10px">
              <td><input class="se" type="number" style="text-align: center" value=""  placeholder="se"/></td>
              <td><input class="re" type="number" style="text-align: center" value="" placeholder="re"/></td>
              <td><input class="ce" type="number" style="text-align: center" value="" placeholder="ce"/></td>
              <td><input class="st" type="number" style="text-align: center" value="" placeholder="st"/></td>
              <td><input class="rt" type="number" style="text-align: center" value="" placeholder="rt"/></td>
              <td><input class="ct" type="number" style="text-align: center" value="" placeholder="ct"/></td>
            </tr>-->
          </tbody>
        </table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import $ from 'jquery'
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import {listRule,getRule,addRuleManagement,updateRuleManagement,delRuleManagement} from "@/api/ruoyi-myLuckyexcel/myluckyexcel";

  export default {
    name: "Rule",
    components: { Treeselect },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //查询规则
        ruleList: [],
        // 查询参数
        queryParams: {
          id: undefined,
          name: undefined,
          jsons: undefined
        },
        // 表单参数
        form: {},

      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询部门列表 */
      getList() {
        this.loading = true;
        listRule(this.queryParams).then(response => {
          this.ruleList = response.rows;
          this.loading = false;
        });
      },
      indexFormat(index){
        return index +1
      },
      // 取消按钮
      cancel() {
        $("#labadd").html("");

        this.open = false;
        this.reset();

      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          jsons: undefined,

        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        if (row != undefined) {
          this.form.parentId = row.id;
        }
        this.open = true;
        this.title = "新增规则";
        listRule().then(response => {

        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        if (row != undefined) {
          this.form.parentId = row.id;
        }
        this.open = true;
        this.title = "修改规则";
        getRule(row.id).then(response => {
          $("#labadd").html("");
          var datas =response.data;
          this.form.id = datas.id;
          this.form.name = datas.name;
          var s = JSON.parse(datas.jsons) ;
          for (var i = 0; i < s.length; i++) {
            var se = s[i].se;
            var re = s[i].re;
            var ce = s[i].ce;
            var st = s[i].st;
            var rt = s[i].rt;
            var ct = s[i].ct;

            $("#labadd").append(
              `<tr  style="margin-top: 10px">
              <td><input class="se" type="number" style="text-align: center;border: aliceblue;" value="`+se+`"  placeholder="se"/></td>
              <td><input class="re" type="number" style="text-align: center;border: aliceblue;" value="`+re+`" placeholder="re"/></td>
              <td><input class="ce" type="number" style="text-align: center;border: aliceblue;" value="`+ce+`" placeholder="ce"/></td>
              <td><input class="st" type="number" style="text-align: center;border: aliceblue;" value="`+st+`" placeholder="st"/></td>
              <td><input class="rt" type="number" style="text-align: center;border: aliceblue;" value="`+rt+`" placeholder="rt"/></td>
              <td><input class="ct" type="number" style="text-align: center;border: aliceblue;" value="`+ct+`" placeholder="ct"/></td>
          </tr>`
            );
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除名称为"' + row.name + '"的规则?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRuleManagement(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 提交按钮 */
      submitForm: function() {

        var ss= this.form.name;
        if("" == ss || "undefined" == ss || ss == null || ss == "null" || ss == undefined){
          this.msgSuccess("请输入规则名称");
          return
        }
        //获取表格数据转json赋值给form

        var arraylist = new Array();
        var trs = $('#labadd').find("tr");
        for (var i = 0;i<trs.length;i++){
          var tds = trs[i].children;
          var slist = {"se":"","re":"","ce":"","st":"","rt":"","ct":"",};
          for (var j = 0;j<tds.length;j++){
            var val = tds[j].children[0].value;
            var cn = tds[j].children[0].className;
            if("se" == cn){
              if("" == val || "undefined" == val || val == null || val == "null" || val == undefined){
                this.msgSuccess("请输入se的规则");
                return
              }
              slist.se= val;
            }
            else if ("re" == cn){
              if("" == val || "undefined" == val || val == null || val == "null" || val == undefined){
                this.msgSuccess("请输入re的规则");
                return
              }
              slist.re = val;
            }
            else if ("ce" == cn){
              if("" == val || "undefined" == val || val == null || val == "null" || val == undefined){
                this.msgSuccess("请输入ce的规则");
                return
              }
              slist.ce = val;
            }
            else if ("st" == cn){
              if("" == val || "undefined" == val || val == null || val == "null" || val == undefined){
                this.msgSuccess("请输入st的规则");
                return
              }
              slist.st = val;
            }
            else if ("rt" == cn){
              if("" == val || "undefined" == val || val == null || val == "null" || val == undefined){
                this.msgSuccess("请输入rt的规则");
                return
              }
              slist.rt = val;
            }
            else if ("ct" == cn){
              if("" == val || "undefined" == val || val == null || val == "null" || val == undefined){
                this.msgSuccess("请输入ct的规则");
                return
              }
              slist.ct = val;
            }else {
              return
            }
          }
          arraylist.push(slist);
        }
        var jsona= JSON.stringify(arraylist);
        this.form.jsons=jsona;
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {

              updateRuleManagement(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {

              addRuleManagement(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      lableAdd(){
        $("#labadd").append(
          `<tr  style="margin-top: 10px">
              <td><input class="se" type="number" style="text-align: center;border: aliceblue;" value=""  placeholder="se"/></td>
              <td><input class="re" type="number" style="text-align: center;border: aliceblue;" value="" placeholder="re"/></td>
              <td><input class="ce" type="number" style="text-align: center;border: aliceblue;" value="" placeholder="ce"/></td>
              <td><input class="st" type="number" style="text-align: center;border: aliceblue;" value="" placeholder="st"/></td>
              <td><input class="rt" type="number" style="text-align: center;border: aliceblue;" value="" placeholder="rt"/></td>
              <td><input class="ct" type="number" style="text-align: center;border: aliceblue;" value="" placeholder="ct"/></td>
          </tr>`
        );

      }
    }
  };
</script>
