<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="职位简称" prop="jobCode">
        <el-input
          v-model="queryParams.jobCode"
          placeholder="请输入职位简称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职位名称" prop="jobName">
        <el-input
          v-model="queryParams.jobName"
          placeholder="请输入职位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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:deptJob:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="deptJobList"
      row-key="jobId"
      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="jobCode" />
      <el-table-column label="职位名称" align="center" prop="jobName" />
      <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" prop="isDelete" v-if="false"/>
      <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:deptJob:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:deptJob:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:deptJob: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="parentId">
          <treeselect v-model="form.parentId" :options="deptJobOptions" :normalizer="normalizer" placeholder="请选择父id" />
        </el-form-item>
        <el-form-item label="职位简称" prop="jobCode">
          <el-input v-model="form.jobCode" placeholder="请输入职位简称" />
        </el-form-item>
        <el-form-item label="职位名称" prop="jobName">
          <el-input v-model="form.jobName" 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 { listDeptJob, getDeptJob, delDeptJob, addDeptJob, updateDeptJob, exportDeptJob } from "@/api/system/deptJob";
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: "DeptJob",
  dicts: ['sys_normal_disable'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 职位表格数据
      deptJobList: [],
      // 职位树选项
      deptJobOptions: [],
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        jobCode: null,
        jobName: null,
        status: null,
        jgid: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDeptSelect();
  },
  methods: {
    /** 查询职位列表 */
    getList() {
      this.loading = true;
      listDeptJob(this.queryParams).then(response => {
        this.deptJobList = this.handleTree(response.data, "jobId", "parentId");
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptSelect() {
      treeselect({roleTypes: 2,
        organizations: user.state.organ}).then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 转换职位数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.jobId,
        label: node.jobName,
        children: node.children
      };
    },
	/** 查询职位下拉树结构 */
    getTreeselect() {
      listDeptJob().then(response => {
        this.deptJobOptions = [];
        const data = { jobId: 0, jobName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "jobId", "parentId");
        this.deptJobOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jobId: null,
        parentId: null,
        ancestors: null,
        jobCode: null,
        jobName: 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.jobId) {
        this.form.parentId = row.jobId;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.form.jgid = user.state.organ;
      this.title = "添加职位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.jobId;
      }
      getDeptJob(row.jobId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改职位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.jobId != null) {
            updateDeptJob(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeptJob(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除职位编号为"' + row.jobId + '"的数据项？').then(function() {
        return delDeptJob(row.jobId);
      }).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>
