<template>
  <div class="app-container">
      
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain
          icon="el-icon-refresh"  size="mini"
          @click="handlSynch"
        >同步</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain
          icon="el-icon-sort" size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
    </el-row>

    <div v-tableHeight="{customHeight: 35}"> 
    
      <el-table  v-if="refreshTable" height="100%"
      header-cell-class-name="table-header-style"
        v-loading="loading"
        :data="deptList"
        row-key="deptId"
        :default-expand-all="isExpandAll"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}" >
        <el-table-column prop="deptName" label="部门名称" ></el-table-column>
        <!-- <el-table-column prop="allPathName" label="全路径" width="200"></el-table-column> -->
        <el-table-column prop="originalSort" label="机构编码" > </el-table-column>
        <!-- <el-table-column prop="customsCode" label="关区代码" width="100"></el-table-column> -->
        <!-- <el-table-column prop="orgClass" label="机构类型" width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.bus_org_class" :value="scope.row.orgClass"/>
          </template>
        </el-table-column>
        <el-table-column prop="orgCategory" label="单位类型" width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.bus_org_category" :value="scope.row.orgCategory"/>
          </template>
        </el-table-column> -->
        <el-table-column label="启用" align="center"  prop="useFlag" >
          <template slot-scope="scope">
           <el-switch :value="scope.row.useFlag == '0' " @change="handleStatusChange(scope.row)" 
           :disabled="scope.row.orgType==1" > </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="200">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column> 
      </el-table>
    </div>
 
  </div>
</template>

<script>
import { listDept, deptSynch,changeUseFlag } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Dept",
  dicts: ['sys_normal_disable', 'bus_org_category', 'bus_org_class'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // 部门树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        deptName: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      listDept(this.queryParams).then(response => {
        this.deptList = this.handleTree(response.data, "deptId");
        this.loading = false;
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
     
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 同步按钮操作 */
    handlSynch(row) {
      deptSynch().then(res => {
        // this.getList();
        this.$modal.msgSuccess("部门同步中，请稍后刷新列表查看！");
      })
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
   handleStatusChange(row){
    console.log(row);
    const useFlagName = row.useFlag=='0'?'禁用':'启用';
    this.$modal.confirm('是否确认' + useFlagName +'"'+ row.deptName + '"部门及其下属部门？').then(function() {
        return changeUseFlag(row.deptId, row.useFlag=='0'?'1':'0');
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("修改成功");
      }).catch(() => {});
     
   }
  }
};
</script>
