<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="父id" prop="parentId">-->
<!--        <el-input-->
<!--          v-model="queryParams.parentId"-->
<!--          placeholder="请输入父id"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="祖级列表" prop="ancestors">-->
<!--        <el-input-->
<!--          v-model="queryParams.ancestors"-->
<!--          placeholder="请输入祖级列表"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="岗位简称" prop="postCode">
        <el-input
          v-model="queryParams.postCode"
          placeholder="请输入岗位简称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位名称" prop="postName">
        <el-input
          v-model="queryParams.postName"
          placeholder="请输入岗位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="状态" prop="status">-->
<!--        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.sys_normal_disable"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="机构代码" prop="jgid">-->
<!--        <el-input-->
<!--          v-model="queryParams.jgid"-->
<!--          placeholder="请输入机构代码"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="所属部门" prop="deptId">-->
<!--        <el-input-->
<!--          v-model="queryParams.deptId"-->
<!--          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 :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:deptPost:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="deptPostList"
      row-key="postId"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="父id" prop="parentId" v-if="false"/>
      <el-table-column label="祖级列表" align="center" prop="ancestors" v-if="false"/>
      <el-table-column label="岗位名称" align="center" prop="postName" />
      <el-table-column label="岗位简称" align="center" prop="postCode" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="机构代码" align="center" prop="jgid" >
        <template slot-scope="scope">
          {{ orgFormatter(scope.row.jgid) }}
        </template>
      </el-table-column>
      <el-table-column label="所属部门" align="center" prop="deptId" >
        <template slot-scope="scope">
          {{ deptFormatter(scope.row.deptId) }}
        </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="['system:deptPost:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-plus"-->
<!--            @click="handleAdd(scope.row)"-->
<!--            v-hasPermi="['system:deptPost:add']"-->
<!--          >新增</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:deptPost:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改岗位设置2对话框 -->
    <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="parentId">-->
<!--          <treeselect v-model="form.parentId" :options="deptPostOptions" :normalizer="normalizer" placeholder="请选择父id" />-->
<!--        </el-form-item>-->
        <el-form-item label="岗位名称" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入岗位名称" />
        </el-form-item>
        <el-form-item label="岗位简称" prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入岗位简称" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="机构代码" prop="jgid">
          <el-input v-model="form.jgid" placeholder="请输入机构代码" />
        </el-form-item>
        <el-form-item label="归属部门" prop="deptId">
          <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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 { listDeptPost, getDeptPost, delDeptPost, addDeptPost, updateDeptPost, exportDeptPost } from "@/api/system/deptPost";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import user from "@/store/modules/user";
import { treeselect } from "@/api/system/dept";

export default {
  name: "DeptPost",
  dicts: ['sys_normal_disable'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 岗位设置2表格数据
      deptPostList: [],
      // 岗位设置2树选项
      deptPostOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 部门树选项
      deptOptions: undefined,
      // 查询参数
      queryParams: {
        // parentId: null,
        // ancestors: null,
        postCode: null,
        postName: null,
        // status: null,
        // jgid: null,
        // deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "岗位名称不能为空", trigger: "blur" }
        ],
        postCode: [
          { required: true, message: "岗位简称不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "所属部门不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDeptSelect();
  },
  methods: {
    /** 查询岗位设置2列表 */
    getList() {
      this.queryParams.jgid = user.state.organ;
      this.loading = true;
      listDeptPost(this.queryParams).then(response => {
        this.deptPostList = this.handleTree(response.data, "postId", "parentId");
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptSelect() {
      treeselect({roleTypes: 2,
        organizations: user.state.organ}).then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 转换岗位设置2数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.postId,
        label: node.postName,
        children: node.children
      };
    },
	/** 查询岗位设置2下拉树结构 */
    getTreeselect() {
      listDeptPost().then(response => {
        this.deptPostOptions = [];
        const data = { postId: 0, postName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "postId", "parentId");
        this.deptPostOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        postId: null,
        parentId: null,
        ancestors: null,
        postCode: null,
        postName: null,
        status: "0",
        jgid: null,
        deptId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.postId) {
        this.form.parentId = row.postId;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.form.jgid = user.state.organ;
      this.title = "添加岗位设置2";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.postId;
      }
      getDeptPost(row.postId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改岗位设置2";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.postId != null) {
            updateDeptPost(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeptPost(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除岗位设置2编号为"' + row.postId + '"的数据项？').then(function() {
        return delDeptPost(row.postId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    orgFormatter(val) {
      let name = '';
      user.state.organDicts.forEach(item => {
        if(item.deptCode == val) {
          name = item.deptName;
        }
      })
      return name;
    },
    deptFormatter(val) {
      let name = '';
      user.state.depts.forEach(item => {
        if(item.deptId == val) {
          name = item.deptName;
        }
      })
      return name;
    }
  }
};
</script>
