<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="科室名称" prop="deptname">
        <el-input
          v-model="queryParams.deptname"
          placeholder="请输入科室名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @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"
          @click="op('add')"
          v-hasPermi="['work:dept:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      border
      :data="deptList"
      row-key="id"
      :default-expand-all="isExpandAll"
      @selection-change="selects"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      height="calc(100vh - 270px)"
    >
      <el-table-column label="科室名称" align="center" prop="deptname" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="op('edit', scope.row)"
            v-hasPermi="['work:dept:edit']"
          >修改</el-button>
          <el-button
            type="text"
            icon="el-icon-plus"
            @click="op('add', scope.row)"
            v-hasPermi="['work:dept:add']"
          >新增</el-button>
          <el-button
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['work:dept:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import { listDept, getDept, delDept, addDept, updateDept } from "@/api/work/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import edit from './edit';

export default {
  name: "Dept",
  components: {
    Treeselect
  },
  data() {
    return {
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 医院科室表格数据
      deptList: [],
      // 医院科室树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        deptname: null,
        deptdesc: null,
        parentId: 0,
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "父级ID不能为空", trigger: "blur" }
        ],
        deptname: [
          { required: true, message: "科室名称不能为空", trigger: "blur" }
        ],
        deptdesc: [
          { required: true, message: "科室描述不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    selects(rows) {
      this.ids = rows.map((item) => item.id);
    },
    op(tag, row) {
      if (tag == 'add') {
        const parentId = row != null && row.id ? row.id : 0;
        this.iframe({ obj: edit, param: { parentId: parentId}, title: '新增科室', width: '1000px', height: '700px' });
      }
      if (tag == 'edit') {
        const id = row.id || this.ids[0];
        this.iframe({ obj: edit, param: { id: id}, title: '编辑科室', width: '1000px', height: '700px' });
      }
    },
    /** 查询医院科室列表 */
    getList() {
      this.ajax({ url: '/work/dept/list', data: this.queryParams }).then((response) => {
        this.deptList = this.handleTree(response.rows, "id", "parentId");
      });
    },
    /** 转换医院科室数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.deptname,
        children: node.children
      };
    },
	/** 查询医院科室下拉树结构 */
    getTreeselect() {
      listDept().then(response => {
        this.deptOptions = [];
        const data = { id: 0, deptname: '顶级节点', children: [] };
        data.children = this.handleTree(response.rows, "id", "parentId");
        this.deptOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        deptname: null,
        deptdesc: 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 = "添加医院科室";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.id;
      }
      this.ajax({ url: '/work/dept/detail/' + 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) {
            this.ajax({ method: 'post',url: '/work/dept/edit', data: this.form }).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.ajax({ method: 'post',url: '/work/dept/add', data: this.form }).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除医院科室编号为"' + row.id + '"的数据项？').then(function() {
        return delDept(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
