<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-mr-lg q-col-gutter-md">
        <div class="col-3">
          <q-input @change="resetSearch" clearable standout="bg-primary text-white" v-model="msg.SName" label="校区名称" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" standout="bg-primary text-white" v-model="StatusTemp" :options="StatusOpts"
            option-value="value" option-label="label" label="校区状态" />
        </div>
      </div>
      <div class="page-option">
        <!-- 原则上不能操作过三个按钮，建议不要使用查询按钮，增加用户操作次数 -->
        <!-- <q-btn color="primary" outline label="立即查询" /> -->
        <q-btn color="accent" class="q-mr-md" icon="add" label="新增校区" @click="initObj(null)" />
        <q-btn-dropdown outline color="dark" label="更多">
          <q-list>
            <q-item clickable v-close-popup @click="onItemClick">
              <!-- <q-item-section side>
                            <q-icon name="style" />
                        </q-item-section> -->
              <q-item-section>
                <q-item-label>导出</q-item-label>
              </q-item-section>
            </q-item>

            <q-item clickable v-close-popup @click="onItemClick">
              <q-item-section>
                <q-item-label>导入</q-item-label>
              </q-item-section>
            </q-item>
          </q-list>
        </q-btn-dropdown>
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat class="sticky-column-table"
        separator="none" title="校区信息" :data="data" :columns="columns" row-key="name">
        <template v-slot:body-cell-Status="props">
          <q-td :props="props">
            <q-badge :color="props.value==1?'negative':'primary'" :label="props.value==1?'关闭':'正常'" />
          </q-td>
        </template>
        <template v-slot:body-cell-Remark="props">
          <q-td :props="props">
            <q-badge color="grey-3 text-grey-10" :label="props.value" />
          </q-td>
        </template>
        <template v-slot:body-cell-optioned="props">
          <q-td :props="props">
            <q-btn flat size="xs" icon="edit" color="accent" style="font-family:perfectFont" label="编辑"
              @click="initObj(props.value)" />
          </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>
      </q-table>
      <q-dialog v-model="persistent" content-class="bg-grey-1" persistent transition-show="scale"
        transition-hide="scale">
        <q-card style="width: 800px;max-width:900px;">
          <q-card-section>
            <div class="text-h6">{{objOption.SId==0?'新增校区':'修改校区'}}</div>
          </q-card-section>
          <div class="text-caption q-mb-lg q-px-md text-grey-6">基本资料</div>
          <q-card-section class="q-pt-none">
            <div class="row wrap">
              <q-input filled stack-label maxlength="20" :dense="false" v-model="objOption.SName" ref="SName"
                class="col-6 q-pr-lg q-pb-lg" label="校区名称" :rules="[val => !!val || '请填写校区名称']" />
              <q-input filled stack-label maxlength="20" :dense="false" v-model="objOption.SAddress" ref="SAddress"
                class="col-6 q-pb-lg" label="校区地址" :rules="[val => !!val || '请填写校区地址']" />
              <q-select class="col-6 q-pb-lg q-pr-lg" clearable filled stack-label use-input option-value="Id"
                option-label="EmployeeName" v-model="objOption.ManagerId" ref="ManagerId" :options="EmployeeList"
                label="校区联系人" :dense="false" emit-value map-options @filter="filterFn">
                <template v-slot:no-option>
                  <q-item>
                    <q-item-section class="text-grey">
                      未找到相关数据
                    </q-item-section>
                  </q-item>
                </template>
              </q-select>
              <q-input filled stack-label maxlength="20" :dense="false" v-model="objOption.SLinkTel" ref="SLinkTel"
                class="col-6 q-pb-lg" label="校区联系电话" :rules="[val => !!val || '请填写校区联系电话']" />
              <q-input filled stack-label maxlength="20" :dense="false" v-model="objOption.Remark"
                class="col-6 q-pb-lg q-pr-lg" label="校区备注" />
              <selectTree v-if="DeptList&&DeptList.length>0" :treeData='DeptList' :defaultArray="returnString"
                nodeKey="DeptId" :multiple="false" labelKey="DeptName" childrenKey="ChildList" tipText="所属部门"
                @getChild="getChild" classStr="col-6 q-pr-lg q-pb-lg"></selectTree>
              <div class="col-6">
                <q-toggle size="md" label="校区状态" color="primary" :false-value="1" :true-value="0"
                  v-model="objOption.Status" />
                <div class="text-grey-6 text-caption">注意：关闭校区后，所有下属教师、学生将无法继续使用系统</div>
              </div>
            </div>
          </q-card-section>
          <q-separator />
          <q-card-actions align="right" class="bg-white">
            <q-btn label="取消" color="dark" style="font-weight:400 !important" @click="closeSaveForm" />
            <q-btn label="立即提交" color="accent q-px-md" style="font-weight:400 !important" :loading="saveLoading"
              @click="saveSchool" />
          </q-card-actions>
        </q-card>
      </q-dialog>
    </div>
  </div>
</template>

<script>
  import {
    getSchoolPage,
    saveSchool
  } from '../../api/school/index'
  //部门
  import {
    getDeptTree
  } from '../../api/system/dept'
  //员工
  import {
    queryEmployee
  } from '../../api/users/user'
  import selectTree from '../../components/common/select-tree'
  export default {
    meta: {
      title: "校区管理"
    },
    components: {
      selectTree
    },
    data() {
      return {
        currentUrl: "",
        columns: [{
            name: 'SName',
            required: true,
            label: '学校名称',
            align: 'left',
            field: row => row.SName,
            format: val => `${val}`,
            sortable: true
          },
          {
            name: 'GroupName',
            label: '所属机构',
            field: 'GroupName',
            align: 'left',
            sortable: true
          },
          {
            name: 'DeptName',
            label: '所属部门',
            field: 'DeptName',
            align: 'left',
            sortable: true
          },
          {
            name: 'ManagerName',
            label: '负责人',
            field: 'ManagerName',
            align: 'left',
            sortable: true
          },
          {
            name: 'SLinkTel',
            label: '负责人电话',
            align: 'left',
            field: 'SLinkTel'
          },
          {
            name: 'Status',
            label: '状态',
            align: 'left',
            field: 'Status'
          },
          {
            name: 'CreateByName',
            label: '创建人',
            align: 'left',
            field: 'CreateByName'
          },
          {
            name: 'CreateTimeStr',
            label: '建立时间',
            align: 'left',
            field: 'CreateTimeStr'
          },
          {
            name: 'Remark',
            label: '备注',
            align: 'left',
            field: 'Remark'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'SId'
          },
        ],
        data: [],
        loading: true,
        StatusOpts: [{
            label: '全部',
            value: '-1'
          },
          {
            label: '开启',
            value: '0'
          },
          {
            label: '关闭',
            value: '1'
          }
        ],
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          SName: "",
          Status: '-1'
        },
        StatusTemp: "",
        pageCount: 0,
        persistent: false,
        objOption: {
          SId: 0,
          SName: "",
          SAddress: "",
          SLinkTel: "",
          SDomain: "",
          SLogo: "",
          Remark: "",
          Status: '0',
          Dept_Id: 0, //管理部门
          ManagerId: 0, //负责人编号
        },
        tempManager: "",
        saveLoading: false,
        DeptList: [], //部门列表
        returnString: [], //默认值
        EmployeeList: [], //员工列表
        AllemployeeList: [], //所有员工
      }
    },
    created() {
      this.queryDeptTree();
      this.getEmployee();
      this.StatusTemp = this.StatusOpts[0]
    },
    mounted() {
      this.currentUrl = this.$route.path
      this.getSchool()
    },
    methods: {
      filterFn(val, update) {
        if (val && val != '') {
          update(() => {
            const needle = val.toLowerCase()
            this.EmployeeList = this.AllemployeeList.filter(v => v.EmployeeName.toLowerCase().indexOf(needle) > -1)
          })
        } else {
          this.EmployeeList = JSON.parse(JSON.stringify(this.AllemployeeList));
        }
      },
      //获取员工列表
      getEmployee() {
        var qMsg = {
          EmployeeName: ""
        }
        queryEmployee(qMsg).then(res => {
          if (res.Code == 1) {
            var jsonData = res.Data;
            if (jsonData && jsonData > 0) {
              this.AllemployeeList = JOSN.parse(JSON.stringify(jsonData));
              this.EmployeeList = JOSN.parse(JSON.stringify(jsonData));;
            }
          }
        })
      },
      getChild(deptArray) {
        var tempStr = "";
        if (deptArray && deptArray != '') {
          tempStr = deptArray;
        }
        this.objOption.Dept_Id = tempStr;
      },
      //获取部门结构树
      queryDeptTree() {
        getDeptTree({}).then(res => {
          if (res.Code == 1) {
            this.DeptList = res.Data;
          }
        })
      },
      onItemClick() {

      },
      //初始化对象
      initObj(id) {
        if (id) {
          let obj = this.data.filter(x => {
            if (x.SId == id) {
              return x
            }
            return false
          })
          this.objOption = obj[0];
          this.returnString.push(this.objOption.Dept_Id.toString());
        } else {
          this.objOption.SId = 0;
          this.objOption.SName = "";
          this.objOption.SAddress = "";
          this.objOption.SLinkTel = "";
          this.objOption.SDomain = "";
          this.objOption.SLogo = "";
          this.objOption.Remark = "";
          this.objOption.Status = "0";
          this.objOption.Dept_Id = 0;
          this.objOption.ManagerId = 0;
        }
        this.persistent = true;
      },
      //重新查询
      resetSearch() {
        this.msg.pageIndex = 1
        this.loading = true
        this.msg.Status = this.StatusTemp ? this.StatusTemp.value : '-1'
        this.getSchool()
      },
      //分页改变
      changePage(val) {
        this.msg.Status = this.StatusTemp ? this.StatusTemp.value : '-1'
        this.msg.pageIndex = val
        this.loading = true
        this.getSchool()
      },
      //获取学校分页列表
      getSchool() {
        getSchoolPage(this.msg).then(res => {
          this.loading = false
          this.data = res.Data.PageData;
          this.pageCount = res.Data.PageCount;
        }).catch(() => {
          this.loading = false
        })
      },
      closeSaveForm() {
        this.persistent = false
      },
      //保存校区
      saveSchool() {
        this.saveLoading = true
        this.$refs.SName.validate()
        this.$refs.SAddress.validate()
        this.$refs.SLinkTel.validate()
        if (
          !this.$refs.SName.hasError &&
          !this.$refs.SAddress.hasError &&
          !this.$refs.SLinkTel.hasError
        ) {
          if (this.tempManager && this.tempManager.Id) {
            this.objOption.ManagerId = this.tempManager.Id;
          }
          saveSchool(this.objOption).then(res => {
            this.saveLoading = false
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '数据保存成功！',
              position: 'top'
            })
            if (this.objOption.SId == 0) {
              this.msg.pageIndex = 1
            }
            this.getSchool()
            this.closeSaveForm()
          }).catch(() => {
            this.saveLoading = false
          })
        } else {
          this.saveLoading = false
        }
      }
    },
  }

</script>

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

</style>
