<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-col-gutter-md">
        <div class="col">
          <selectSearch :Data='schoolList' optionValue="SId" optionLabel="SName" :densetype="false"
            :vModel="msg.School_Id" label="校区" @returnData="returnData" :useInput="true" :reactiveRules="false">
          </selectSearch>
        </div>
        <div class="col">
          <q-input @change="resetSearch" clearable standout="bg-primary text-white" v-model="msg.DeptName"
            label="部门名称" maxlength="20" />
        </div>
        <div class="col">
          <q-select @input="resetSearch" filled stack-label option-value="value" option-label="label"
            v-model="msg.DeptTier" ref="DeptTier" :options="DeptTierList" label="部门层级" :dense="false" emit-value
            map-options clearable @clear="resetSearch" />
        </div>
        <div class="col">
          <selectTree v-if="DeptTreeList&&DeptTreeList.length>0" :treeData='DeptTreeList' :defaultArray="returnString"
            nodeKey="DeptId" :multiple="false" labelKey="DeptName" childrenKey="ChildList" tipText="选择上级部门"
            @getChild="getChild"></selectTree>
        </div>
        <div class="col">
          <q-select @input="resetSearch" standout="bg-primary text-white" v-model="msg.Status" :options="ShowOpts"
            emit-value map-options label="状态" />
        </div>
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat class="sticky-tow-column-table"
        separator="none" :data="data" :columns="columns" row-key="name">
        <template v-slot:top="props">
          <div class="col-2 q-table__title">部门信息</div>
          <q-space />
          <div class="page-option">
            <q-btn color="accent" size="sm" class="q-mr-md" icon="add" label="新增部门" @click="EditDept(null)" />
            <q-btn color="secondary" flat size="sm" icon="iconfont icon-zuzhijiagou" label="组织机构图"
              @click="gotoRelation()" />
          </div>
        </template>
        <template v-slot:body-cell-Status="props">
          <q-td :props="props">
            <q-toggle size="sm" color="secondary" :false-value="1" :true-value="0" v-model="props.row.Status"
              title="注意：关闭后，分类将无法正常使用." @input="SetDeptStatus(props.row)" />
          </q-td>
        </template>
        <template v-slot:body-cell-School_Id="props">
          <q-td :props="props">
            {{props.row.School_Id>0? props.row.SchoolName:'总部'}}
          </q-td>
        </template>
        <template v-slot:body-cell-DeptPostList="props">
          <q-td :props="props">
            <template v-if="props.row.DeptPostList&&props.row.DeptPostList.length>0"
              v-for="(item,index) in props.row.DeptPostList">
              <q-badge :key="index" color="red" :label="item.PostName" class="q-mr-xs" />
            </template>
          </q-td>
        </template>
        <template v-slot:bottom>
          <q-pagination class="full-width justify-end" v-model="msg.pageIndex" color="primary" :max="pageCount"
            :input="true" @input="changePage" />
        </template>
        <template v-slot:body-cell-optioned="props">
          <q-td :props="props">
            <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
              @click="EditDept(props.row)" />
          </q-td>
        </template>
      </q-table>
      <dept-form v-if="isShowDeptForm" :save-obj="deptObjOption" @close="closeDeptSaveForm" @success="refreshPage">
      </dept-form>
    </div>
  </div>
</template>
<script>
  import {
    getDeptPage,
    setDeptStatusInfo,
    getDeptTree
  } from '../../api/system/dept'
  import {
    getSchoolDropdown,
  } from '../../api/school/index'
  import {
    getDeptTierList
  } from '../../api/system/index'
  import deptForm from '../../components/system/dept-form'
  import selectTree from '../../components/common/select-tree'
  import selectSearch from '../../components/common/select-search'
  export default {
    meta: {
      title: "部门管理"
    },
    components: {
      deptForm,
      selectTree,
      selectSearch
    },
    data() {
      return {
        currentUrl: "",
        columns: [{
            name: 'DeptName',
            label: '部门名称',
            align: 'left',
            field: row => row.DeptName
          },
          {
            name: 'School_Id',
            label: '所属校区',
            field: 'School_Id',
            align: 'left'
          },
          {
            name: 'DeptTier',
            label: '部门层级',
            field: 'DeptTier',
            align: 'left'
          },
          {
            name: 'ManagerName',
            label: '负责人',
            field: 'ManagerName',
            align: 'left'
          },
          {
            name: 'DeptTel',
            label: '电话',
            field: 'DeptTel',
            align: 'left'
          },
          {
            name: 'DeptPostList',
            label: '绑定岗位',
            field: 'DeptPostList',
            align: 'left'
          },
          {
            name: 'DeptSort',
            label: '排序',
            field: 'DeptSort',
            align: 'left'
          },
          {
            name: 'Status',
            label: '状态',
            align: 'left',
            field: 'Status'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'DeptId'
          }
        ],
        data: [],
        loading: true,
        ShowOpts: [{
            label: '全部',
            value: '-1'
          },
          {
            label: '正常',
            value: '0'
          },
          {
            label: '删除',
            value: '1'
          }
        ],
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          DeptName: "", //部门名称
          DeptId: 0, //部门编号
          Status: "-1",
          School_Id: 0,
          DeptTier: "",
          ParentId: "",
        },
        pageCount: 0,
        returnString: [], //树形结构初始化数组
        isShowDeptForm: false,
        deptObjOption: null,
        schoolList: [], //校区列表
        DeptTierList: [], //部门层级
        DeptTreeList: [], //部门树形结构列表
      }
    },
    created() {
      this.queryDeptTier();
      this.queryDeptTree();
      this.getSchool();
    },
    mounted() {
      this.currentUrl = this.$route.path;
      this.queryDeptPage();
    },
    methods: {
      //获取部门树形结构
      queryDeptTree() {
        getDeptTree({}).then(res => {
          if (res.Code == 1) {
            this.DeptTreeList = res.Data;
          }
        })
      },
      //获取选中的部门
      getChild(menuArray) {
        var tempStr = "0";
        if (menuArray && menuArray != '') {
          tempStr = menuArray;
        }
        this.msg.ParentId = tempStr;
        this.resetSearch();
      },
      //部门层级列表
      queryDeptTier() {
        this.DeptTierList = getDeptTierList();
      },
      //获取校区
      getSchool() {
        getSchoolDropdown({}).then(res => {
          var tempSchoolArray = res.Data;
          if (!tempSchoolArray) {
            tempSchoolArray = [];
          }
          tempSchoolArray.unshift({
            SId: 0,
            SName: "不限"
          });
          this.schoolList = tempSchoolArray;
          this.$forceUpdate();
        })
      },
      gotoRelation() {
        var tempStr = '/system/deptrelation';
        this.$router.push({
          path: tempStr
        });
      },
      //重新查询
      resetSearch() {
        this.msg.pageIndex = 1;
        this.queryDeptPage();
      },
      //翻页
      changePage(val) {
        this.msg.pageIndex = val;
        this.queryDeptPage();
      },
      //获取部门分页列表
      queryDeptPage() {
        if (this.msg.DeptTier == "" || this.msg.DeptTier == null) {
          this.msg.DeptTier = "0"
        }
        if (this.msg.ParentId == "" || this.msg.ParentId == null) {
          this.msg.ParentId = "0"
        }
        this.loading = true;
        getDeptPage(this.msg).then(res => {
          this.loading = false;
          this.data = res.Data.PageData;
          this.pageCount = res.Data.PageCount;

          if (this.msg.DeptTier == "0") {
            this.msg.DeptTier = ""
          }
          if (this.msg.ParentId == "0") {
            this.msg.ParentId = ""
          }
        }).catch(() => {
          this.loading = false;
        })
      },
      //刷新页面
      refreshPage() {
        if (!this.deptObjOption) {
          this.msg.pageIndex = 1;
          this.msg.DeptName = "";
          this.msg.DeptId = 0;
          this.msg.Status = "-1";
        }
        this.queryDeptPage();
      },
      //新增修改部门
      EditDept(obj) {
        if (obj) {
          this.deptObjOption = obj;
        } else {
          this.deptObjOption = null;
        }
        this.isShowDeptForm = true;
      },
      //关闭弹窗
      closeDeptSaveForm() {
        this.isShowDeptForm = false;
      },
      //修改部门状态
      SetDeptStatus(obj) {
        var that = this;
        var delMsg = {
          DeptId: obj.DeptId,
          Status: obj.Status
        };
        var tipMsg = "";
        if (delMsg.Status == 0) {
          tipMsg = "是否启用【" + obj.DeptName + "】部门?";
        } else {
          tipMsg = "是否禁用【" + obj.DeptName + "】部门?";
        }
        this.$q.dialog({
          title: '提示信息',
          message: tipMsg,
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          setDeptStatusInfo(delMsg).then(res => {
            if (res.Code == 1) {
              that.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '操作成功',
                position: 'top'
              })
              that.queryDeptPage();
            }
          })
        }).onCancel(() => {
          obj.Status = obj.Status == 1 ? 0 : 1;
        });
      },
      returnData(data) {
        this.msg.School_Id = data;
        this.resetSearch()
      }
    }
  }

</script>
<style lang="sass">
  @import url('~assets/css/table.sass');

</style>
