<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="levelname"
            placeholder="请输入层级名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm"  :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="编码名称" prop="name1" style="white-space: nowrap">
            <el-input
              v-model="queryParams.name1"
              placeholder="请输入编码名称"
              clearable
              size="small"
              style="width: 240px"
              @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 :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['code:rule:add']"
            >新增</el-button>
          </el-col>
   <!--       <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['code:rule:edit']"
            >修改</el-button>
          </el-col>

          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['code:rule:remove']"
            >删除</el-button>
          </el-col>
 -->
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="codeList"   row-key="id"  >
          <el-table-column type="selection" width="50" align="center" />
          <!--<el-table-column label="编码类型" align="center" key="codeid1" prop="codeid" v-if="columns[0].visible" /> -->
          <el-table-column label="编码id" align="center" key="id" prop="id" v-if="false" />
          <el-table-column label="规则名称" align="center" key="summary" prop="summary"  />
          <el-table-column label="层级规则" align="center" key="remark" prop="remark"  />
          <el-table-column label="编码类型1" align="center" key="name1" prop="name1" />
          <el-table-column label="编码类型2" align="center" key="name2" prop="name2" v-if="columns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="编码类型3" align="center" key="name3" prop="name3" v-if="columns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="编码类型4" align="center" key="name4" prop="name4" v-if="columns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="编码类型5" align="center" key="name5" prop="name5" v-if="columns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="编码类型6" align="center" key="name6" prop="name6" v-if="columns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="编码类型7" align="center" key="name7" prop="name7" v-if="false"  :show-overflow-tooltip="true" />
          <el-table-column label="编码类型8" align="center" key="name8" prop="name8" v-if="false" :show-overflow-tooltip="true" />
          <el-table-column
            label="操作"
            align="center"
            width="160"
            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="['code:rule:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['code:rule:remove']"
              >删除</el-button>

            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

<!-- 新增编辑   -->

    <el-dialog :title="title" :visible.sync="open" width="50%" >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="10">
            <el-form-item label="层级代码" prop="levelid">
              <treeselect v-model="form.levelid" :options="deptOptions" :show-count="true" placeholder="请选择归属层级" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="规则名称" prop="length1" style="white-space: nowrap">
              <el-input v-model="form.summary" placeholder="规则名称" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
                                <el-form-item label="编码类型1">
                                  <el-select v-model="form.codeid1" placeholder="请选择">
                                    <el-option
                                      v-for="dict in dict.type.code_dic"
                                      :key="dict.value"
                                      :label="dict.label"
                                      :value="dict.value"
                                    ></el-option>
                                  </el-select>
                                </el-form-item>
                    </el-col>
          <el-col :span="6">
            <el-form-item label="长度" prop="length1">
              <el-input v-model="form.length1" placeholder="长度" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="编码值" prop="name1">
              <el-input v-model="form.name1" placeholder="请输入编码值" @input="inputArea()"   />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
                  <el-col :span="10">
                                        <el-form-item label="编码类型2">
                                          <el-select v-model="form.codeid2" placeholder="请选择">
                                            <el-option
                                              v-for="dict in dict.type.code_dic"
                                              :key="dict.value"
                                              :label="dict.label"
                                              :value="dict.value"
                                            ></el-option>
                                          </el-select>
                                        </el-form-item>
                            </el-col>
          <el-col :span="6">
            <el-form-item label="长度" prop="length2">
              <el-input v-model="form.length2" placeholder="长度" maxlength="50" />
            </el-form-item>
          </el-col>
                  <el-col :span="8">
                    <el-form-item label="编码值" prop="name2">
                      <el-input v-model="form.name2" placeholder="请输入编码值" maxlength="50" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                          <el-col :span="10">
                                                <el-form-item label="编码类型3">
                                                  <el-select v-model="form.codeid3" placeholder="请选择">
                                                    <el-option
                                                      v-for="dict in dict.type.code_dic"
                                                      :key="dict.value"
                                                      :label="dict.label"
                                                      :value="dict.value"
                                                    ></el-option>
                                                  </el-select>
                                                </el-form-item>
                                    </el-col>
                  <el-col :span="6">
                    <el-form-item label="长度" prop="length3">
                      <el-input v-model="form.length3" placeholder="长度" maxlength="50" />
                    </el-form-item>
                  </el-col>
                          <el-col :span="8">
                            <el-form-item label="编码值" prop="name3">
                              <el-input v-model="form.name3" placeholder="请输入编码值" maxlength="50" />
                            </el-form-item>
                          </el-col>
                        </el-row>
                        <el-row>
                                  <el-col :span="10">
                                                        <el-form-item label="编码类型4">
                                                          <el-select v-model="form.codeid4" placeholder="请选择">
                                                            <el-option
                                                              v-for="dict in dict.type.code_dic"
                                                              :key="dict.value"
                                                              :label="dict.label"
                                                              :value="dict.value"
                                                            ></el-option>
                                                          </el-select>
                                                        </el-form-item>
                                            </el-col>
                          <el-col :span="6">
                            <el-form-item label="长度" prop="length4">
                              <el-input v-model="form.length4" placeholder="长度" maxlength="50" />
                            </el-form-item>
                          </el-col>
                                  <el-col :span="8">
                                    <el-form-item label="编码值" prop="name4">
                                      <el-input v-model="form.name4" placeholder="请输入编码值" maxlength="50" />
                                    </el-form-item>
                                  </el-col>
                                </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="编码类型5">
              <el-select v-model="form.codeid5" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.code_dic"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="长度" prop="length5">
              <el-input v-model="form.length5" placeholder="长度" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="编码值" prop="name5">
              <el-input v-model="form.name5" placeholder="请输入编码值" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="编码类型6">
              <el-select v-model="form.codeid6" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.code_dic"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="长度" prop="length6">
              <el-input v-model="form.length6" placeholder="长度" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="编码值" prop="name6">
              <el-input v-model="form.name6" placeholder="请输入编码值" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>

        <!--<el-row>
          <el-col :span="10">
            <el-form-item label="编码类型7">
              <el-select v-model="form.codeid7" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.code_dic"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="长度" prop="length7">
              <el-input v-model="form.length7" placeholder="长度" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="编码值" prop="name7">
              <el-input v-model="form.name7" placeholder="请输入编码值" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item label="编码类型8">
              <el-select v-model="form.codeid8" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.code_dic"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="长度" prop="length8">
              <el-input v-model="form.length8" placeholder="长度" maxlength="50" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="编码值" prop="name8">
              <el-input v-model="form.name8" placeholder="请输入编码值" maxlength="50" />
            </el-form-item>
          </el-col>
        </el-row>
-->


      </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>


    <!-- 用户导入对话框 -->
<!--    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
-->

  </div>
</template>

<script>

import { treeselect, listRule, getRule, delRule, addRule, updateRule, exportRule } from "@/api/code/rule";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Code",
   dicts: [ 'code_dic'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      codeList: null,
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      levelname: undefined,

      len1:2,

      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },


      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined
      },
      // 列信息
      columns: [
        { key: 0, label: `用户编号`, visible: true },
        { key: 1, label: `用户名称`, visible: true },

      ],
      // 表单校验
      rules: {



      }

    };
  },

        watch: {
          // 根据名称筛选部门树
          levelname(val) {
            this.$refs.tree.filter(val);
          }
        },


  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listRule(this.queryParams).then(response => {
          this.codeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },


    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
       this.queryParams.levelid = data.id;
      this.getList();
    },
    // 用户状态修改

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name:undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.Id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleResetPwd":
          this.handleResetPwd(row);
          break;
        case "handleAuthRole":
          this.handleAuthRole(row);
          break;
        default:
          break;
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTreeselect();
        this.open = true;
        this.title = "添加编码";

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();

      var a = row;
      const id = row.Id || this.ids;
      getRule(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改编码";
      });
    },
    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      this.$prompt('请输入"' + row.userName + '"的新密码', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputPattern: /^.{5,20}$/,
        inputErrorMessage: "用户密码长度必须介于 5 和 20 之间",
      }).then(({ value }) => {
          resetUserPwd(row.userId, value).then(response => {
            this.$modal.msgSuccess("修改成功，新密码是：" + value);
          });
        }).catch(() => {});
    },
    /** 分配角色操作 */
    handleAuthRole: function(row) {
      const userId = row.userId;
      this.$router.push("/system/user-auth/role/" + userId);
    },

    inputArea() {
      this.form.name1.maxlength = "2";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
        var a = this.form.id;
          if (this.form.id != undefined) {
            updateRule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {

          var a = this.form;
            addRule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {

    var a = row;
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return delRule(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有用户数据项？').then(() => {
        this.exportLoading = true;
        return exportUser(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then(response => {
        this.$download.name(response.msg);
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
