<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="层级编码" prop="levelcode" style="white-space: nowrap">
        <el-input
          v-model="queryParams.levelcode"
          placeholder="请输入层级编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="层级名称" prop="levelname" style="white-space: nowrap">
        <el-input
          v-model="queryParams.levelname"
          placeholder="请输入层级名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['level:rule: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="层级编码" prop="levelcode" />
      <el-table-column label="层级名称" align="center" prop="levelname" />
<!--      <el-table-column label="上级层级id" align="center" prop="parentId" />
      <el-table-column label="子层级序号集" align="center" prop="ancestors" />
      <el-table-column label="序号" align="center" prop="xh" />
      <el-table-column label="层级数" align="center" prop="level" />
      <el-table-column label="机构id" align="center" prop="jgid" />
      <el-table-column label="是否注销" align="center" prop="sfzx" />
      <el-table-column label="创建时间" align="center" prop="create_time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.create_time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="update_time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.update_time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
-->
      <el-table-column label="备注" align="center" prop="remark" />
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['level:rule:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['level:rule:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['level:rule:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改层级规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="层级编码" prop="levelcode">
          <el-input v-model="form.levelcode" placeholder="请输入层级编码" />
        </el-form-item>
        <el-form-item label="层级名称" prop="levelname">
          <el-input v-model="form.levelname" placeholder="请输入层级名称" />
        </el-form-item>
        <el-form-item label="上级层级" prop="parentId">
          <treeselect v-model="form.parentId" :options="ruleOptions" :normalizer="normalizer" placeholder="请选择上级层级id" />
        </el-form-item>
<!--        <el-form-item label="序号" prop="xh">
          <el-input v-model="form.xh" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="机构id" prop="jgid">
          <el-input v-model="form.jgid" placeholder="请输入机构id" />
        </el-form-item>

        <el-form-item label="是否注销" prop="sfzx">
                  <el-select v-model="value" placeholder="请输入是否注销"  @change="selectChanged">
                  <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"

                              ></el-option>
                    </el-select>
           </el-form-item>
-->

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </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 { listRule, getRule, delRule, addRule, updateRule, exportRule } from "@/api/level/rule";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Rule",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 层级规则表格数据
      ruleList: [],
      // 层级规则树选项
      ruleOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        levelcode: null,
        levelname: null,
        parentId: null,
        ancestors: null,
        xh: null,
        level: null,
        jgid: null,
        sfzx: null,
        create_time: null,
        update_time: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        create_time: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        update_time: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
      },
      options: [{
        value: 0,
         label: '否'
      },{
      value: 1,
               label: '是'
      }],
    };
  },
  created() {
    this.getList();
  },

  methods: {
    /** 查询层级规则列表 */
    getList() {
      this.loading = true;
      listRule(this.queryParams).then(response => {
        this.ruleList = this.handleTree(response.data, "id", "parentId");
        ;
        this.loading = false;
      });
    },
    /** 转换层级规则数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.levelname,
        children: node.children
      };
    },
	/** 查询层级规则下拉树结构 */
    getTreeselect() {
      listRule().then(response => {
        this.ruleOptions = [];
        const data = { id: 0, levelname: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.ruleOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        levelcode: null,
        levelname: null,
        parentId: null,
        ancestors: null,
        xh: null,
        jgid: null,
        sfzx: null,
        create_time: null,
        update_time: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
      } else {
        this.form.parentId = "0";
      }
      this.open = true;
      this.title = "添加层级规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.id;
      }
      getRule(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改层级规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除层级规则编号为"' + row.id + '"的数据项？').then(function() {
        return delRule(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
 selectChanged(params) {
                  const { value, label } = params;
		    console.log(value, label)

                 }
  }
};
</script>
