<!--教职工管理-->
<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="100px">
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="loadData"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="loadData"
        />
      </el-form-item>
      <el-form-item label="所属幼儿园" prop="kindergartenId">
        <el-select :disabled="disabledKindergarten" v-model="queryParams.kindergartenId" filterable placeholder="请选择所属幼儿园"  style="width: 100%">
          <el-option
            v-for="dict in KindergartenList"
            :key="dict.id"
            :label="dict.kindergartenName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="信息来源" prop="informationSource">
        <el-select v-model="queryParams.informationSource" filterable placeholder="请选择信息来源" filterable>
          <el-option
            v-for="dict in dict.type[DictType.information_source]"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="在职状态" prop="jobStatus">
        <el-select v-model="queryParams.jobStatus" filterable placeholder="请选择在职状态" filterable>
          <el-option
            v-for="dict in dict.type[DictType.job_status]"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 207px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
       <template v-if="kindergartenNameList.length > 0 && kindergartenNameList !== null"  >
         <el-col :span="2.5">
           <el-tag closable @close="handleClose(item)" v-for="(item,index) in kindergartenNameList" :key="index">
             {{ item }}
           </el-tag>
         </el-col>
       </template>
       <template v-else>
         <el-col :span="2.5" >
           <el-button
             v-hasPermi="['system:staff:add']"
             type="primary"
             plain
             icon="el-icon-plus"
             size="mini"
             @click="handleAdd"
           >新增
           </el-button>
         </el-col>
       </template>
    </el-row>
    <j-table
      v-loading="loading"
      :data="teacherList"
      :columns="columns"
      :pagination="pagination"
      @pagination-change="loadData"
      :showSelection="false"
    >
      <template slot="sexType" slot-scope="{ row }">
        <dict-tag :options="dict.type[DictType.sys_sex]" :value="row.sex" />
      </template>
      <template slot="jobStatusType" slot-scope="{ row }">
        <dict-tag :options="dict.type[DictType.job_status]" :value="row.jobStatus" />
      </template>
      <template slot="accountStatusType" slot-scope="{ row }">
        <dict-tag :options="dict.type[DictType.staff_account_status]" :value="row.accountStatus" />
      </template>
      <template slot="informationSourceType" slot-scope="{ row }">
        <dict-tag :options="dict.type[DictType.information_source]" :value="row.informationSource" />
      </template>
<!--      <template slot="tenantAccountType" slot-scope="{ row }">-->
<!--        <el-button type="text" :disabled="row.tenantAccount == 0" @click.stop="handleJumpPeople(row)">{{row.tenantAccount || 0}}人</el-button>-->
<!--      </template>-->
<!--      <template slot="gardenName" slot-scope="{ row }">{{ row.params && row.params.gardenName || '&#45;&#45;' }}</template>-->
      <el-table-column label="操作" min-width="200" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button
            v-hasPermi="['system:staff:query']"
            size="mini"
            type="text"
            @click.stop="handleView(row)"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['system:staff:edit']"
            @click.stop="handleEdit(row, false)"
            :disabled="row.accountStatus !== '1'"
          >编辑
          </el-button>
          <el-button
            size="mini"
            v-hasPermi="['system:staff:abandon']"
            type="text"
            @click.stop="handleRemove(row, false)"
            :disabled="row.accountStatus !== '1'"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </j-table>
  </div>
</template>

<script>
import DictType from '@/utils/dict/DictType'
import { parseTime } from '@/utils/vctgo'
import BranchGardenDialog from '@/views/system/information/modules/BranchGardenDialog.vue'
import BranchInfoDialog from '@/views/system/information/modules/BranchInfoDialog.vue'
import JTable from '@/components/Table/index.vue'
import options from '@/utils/dict/options'
import TenantDialog from '@/views/system/tenant/modules/TenantDialog.vue'
import ChangeGarden from '@/views/system/information/modules/ChangeGarden.vue'
import SelectKindergarten from '@/components/Select/SelectKindergarten.vue'
import { deleteTeacher, getKinderList, getStaffList, updateTeacher } from '@/api/system/teacher'

// 设置安全密钥
window._AMapSecurityConfig = {
  securityJsCode: '1091cb7a90c725e6873bb88e37c54f5d'
}

const initParams = {
  userName:undefined,
  phone:undefined,
  kindergartenId: undefined,
  informationSource:undefined,
  jobStatus:undefined,
  beginTime:undefined,
  endTime:undefined
}
export default {
  name: 'Branch',
  dicts: [DictType.kindergarten_scale_type, DictType.information_source,DictType.job_status,DictType.sys_sex,DictType.staff_account_status],
  components: {
    SelectKindergarten,
    BranchGardenDialog,
    BranchInfoDialog,
    JTable,
    TenantDialog,
    ChangeGarden
  },
  data() {
    return {
      kindergartenNameList:[],
      // 表单参数
      DictType,
      options,
      selected: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 日期范围
      dateRange: [],
      teacherList: [],
      pagination: {
        total: 0,
        pageNum: 1,
        pageSize: 10
      },
      KindergartenList:[],
      // 查询参数
      queryParams: Object.assign({}, initParams),
      gardenList: [],
      disabledKindergarten: false,
      routerInfo:[],
      kindergartens:{
        kindergartenId: this.$route.query.kindergartenId || '',
        // kindergartenType: this.$route.query.kindergartenType || ''
      },
      columns: [
        { prop: 'userName', label: '姓名', align: 'left' },
        { prop: 'phone', label: '手机号', minWidth: 120 },
        { prop: 'sex', label: '性别', customRender: 'sexType', minWidth: 100 },
        { prop: 'kindergartenName', label: '所属幼儿园', minWidth: 150 },
        { prop: 'jobStatus', label: '在职状态', customRender: 'jobStatusType', minWidth: 120, align: 'center' },
        { prop: 'informationSource', label: '信息来源', customRender: 'informationSourceType', minWidth: 100, align: 'left' },
        { prop: 'saasAccountNumber', label: 'SAAS账号', minWidth: 160,  align: 'center' },
        { prop: 'accountStatus', label: '账号状态',customRender: 'accountStatusType', minWidth: 160 },
        { prop: 'createBy', label: '创建人', minWidth: 100, align: 'left' },
        { prop: 'createTime', label: '创建时间', width: 160 }
      ]
    }
  },
  created() {
    this.getKindergartenList()
    if(this.$route.query.kindergartenName)  this.renderNumer()
    this.loadData()
    this.routerInfo =this.$route.query
  },
  activated() {
    this.$route.query.kindergartenName ? this.renderNumer() : this.kindergartenNameList = []
    if(!this.$route.query.kindergartenName){
      this.queryParams.kindergartenId = ''
      this.disabledKindergarten = false
    }
    this.loadData()
    this.routerInfo =this.$route.query
  },
  deactivated() {
    this.queryParams = Object.assign({}, initParams)
  },
  methods: {
    /**
     * 获取所有幼儿园列表
     */
    getKindergartenList() {
      getKinderList().then(({data})=>{
        // const options = data.filter((item=> item.accountStatus === '1'))
        this.KindergartenList = data
      })

    },
    /**
     * 获取教职工列表
     */
     loadData(){
      this.loading = true
      this.kindergartens = {
        kindergartenId: this.$route.query.kindergartenId || this.queryParams.kindergartenId ,
        // kindergartenType: this.$route.query.kindergartenType
      }
       getStaffList(this.addDateRange(Object.assign({}, this.pagination,this.queryParams,this.kindergartens), this.dateRange)).then(({ data,total,code }) => {
         if(code === 200){
           this.teacherList = data
           this.pagination.total = total
         }
       }).finally(()=>
         this.loading = false
       )
    },
    /**
     * 搜索
     */
    handleSearch(){
      this.loadData()
    },
    /** 重置按钮操作 */
    handleReset() {
      this.dateRange = []
      if(this.disabledKindergarten) {
          this.queryParams.userName = undefined,
          this.queryParams.phone =undefined,
          this.queryParams.informationSource=undefined,
          this.queryParams.jobStatus= undefined,
          this.queryParams.beginTime=undefined,
          this.queryParams.endTime=undefined
      }else {
          this.queryParams.userName = undefined,
          this.queryParams.phone =undefined,
          this.queryParams.kindergartenId = undefined,
          this.queryParams.informationSource=undefined,
          this.queryParams.jobStatus= undefined,
          this.queryParams.beginTime=undefined,
          this.queryParams.endTime=undefined
      }
      this.pagination.pageNum = 1
      this.loadData()
    },
    /**
     * 新增教职工
     */
    handleAdd(row) {
     this.$router.push({ path: '/information/addTeacher' })
    },
    /**
     * 编辑教职工
     * @param row
     * @param readonly
     */
    handleEdit(row, readonly) {
      Object.keys(this.routerInfo).length !== 0 ? this.$router.push({ path: '/information/editTeacher', query: { teacherId: row.id,routerInfo:this.routerInfo } }) : this.$router.push({ path: '/information/editTeacher', query: { teacherId: row.id } })
    },
    /**
     * 详情
     * @param row
     */
    handleView(row) {
      this.$router.push({ path: '/information/teacherDetails',query: { teacherId: row.id } })
    },
    /** 删除按钮操作 */
    handleRemove(row) {
      // if (row.tenantAccount) return this.$modal.msgError('账号已开通，不可删除')
      const ids = row.id || this.ids
      const data = new FormData();
      data.append('kindergartenId', row.kindergartenId);
      this.$modal.confirm('是否确认删除当前幼儿园教职工管理数据项？').then(function() {
        return deleteTeacher(ids,data)
      }).then(() => {
        this.loadData()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /**
     * 渲染tags
     */
    renderNumer(){
      this.queryParams.kindergartenId = this.$route.query.kindergartenId
      this.disabledKindergarten = true
      this.kindergartenNameList = []
      this.kindergartenNameList.push(this.$route.query.kindergartenName)
    },
    /**
     * 删除tags
     */
    handleClose(val){
      this.kindergartenNameList.splice(this.kindergartenNameList.indexOf(val), 1);
      this.kindergartens.kindergartenId = null
      // this.kindergartens.kindergartenType = null
      this.queryParams.kindergartenId  = null
      this.disabledKindergarten = false
      this.$router.push({ path: '/information/teacher'})
      this.loadData()
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  width: 800px;
  height: 500px;
}
</style>

