<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="kindergartenName">
        <el-input
          v-model="queryParams.kindergartenName"
          placeholder="请输入幼儿园名称"
          clearable
          @keyup.enter.native="getList"
        />
      </el-form-item>
      <el-form-item label="幼儿园类型" prop="kindergartenType">
        <el-select v-model="queryParams.kindergartenType" filterable placeholder="请选择幼儿园类型" clearable>
          <el-option
            v-for="dict in dict.type[DictType.kindergarten_scale_type]"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="账号状态">
        <el-select v-model="queryParams.accountStatus" filterable placeholder="请选择账号状态" clearable>
          <el-option
            v-for="dict in options.tenantAccountStatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="getList"
        />
      </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="getList">搜索</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="groupNameList.length > 0 && groupNameList !== null && branchList !== undefined">
        <el-col :span="2.5">
          <el-tag v-for="(item,index) in groupNameList" :key="index" closable @close="handleClose(item)">
            {{ item }}
          </el-tag>
        </el-col>
      </template>
      <template v-else>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['system:branch:add']"
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增
          </el-button>
        </el-col>
      </template>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-hasPermi="['system:branch:export']"-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <j-table
      v-loading="loading"
      :data="branchList"
      :columns="columns"
      :pagination="pagination"
      :show-selection="false"
      @selection-change="handleSelectionChange"
      @pagination-change="getList"
    >
      <template slot="kindergartenType" slot-scope="{ row }">
        <dict-tag :options="dict.type[DictType.kindergarten_scale_type]" :value="row.kindergartenType" />
      </template>
      <template slot="staffCountType" slot-scope="{ row }">
        <el-button type="text" :disabled="!row.staffCount" @click.stop="handleJumpPeople(row)">{{ row.staffCount || 0 }}人</el-button>
      </template>

      <template slot="gardenName" slot-scope="{ row }">{{ row.params && row.params.gardenName || '--' }}</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:branch:detail']"
            size="mini"
            type="text"
            @click.stop="handleView(row)"
          >详情
          </el-button>
          <el-button
            v-hasPermi="['system:branch:edit']"
            size="mini"
            type="text"
            @click.stop="handleEdit(row, false)"
          >编辑
          </el-button>
          <span @click.stop>
            <el-dropdown trigger="click" size="mini" @command="(command) => handleCommand(command, row)">
              <el-button size="mini" type="text" icon="el-icon-d-arrow-right" @click.prevent>更多</el-button>
              <el-dropdown-menu slot="dropdown">
                <template>
                  <el-dropdown-item
                    v-if="row.accountStatus !== '已开通'"
                    command="handleAddPeople(row)"
                  >新增教职工</el-dropdown-item>
                  <el-dropdown-item
                    v-if="row.accountStatus !== '未开通'"
                    command="handleManger(row)"
                  >账号管理</el-dropdown-item>
                  <el-dropdown-item
                    v-hasPermi="['system:branch:adjust']"
                    command="handleChangeGarden(row)"
                  >调整集团</el-dropdown-item>
                </template>
                <el-dropdown-item
                  v-hasPermi="['system:branch:abandon']"
                  command="handleRemove(row)"
                  :disabled="!!row.staffCount"
                >删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </span>
        </template>
      </el-table-column>
    </j-table>
    <branch-garden-dialog ref="gardenDialogRef" @ok="getList" />
    <tenant-dialog ref="tenantDialog" @ok="getList" />
    <change-garden ref="changeGardenRef" @ok="getList" />
    <branch-info-dialog ref="infoDialogRef" />

  </div>
</template>

<script>
import DictType from '@/utils/dict/DictType'
import { listBranch, logicalDelete } from '@/api/system/branch'
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'

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

const initParams = {
  kindergartenName: null,
  kindergartenType: null,
  accountStatus: null,
  createBy: null
}
export default {
  name: 'Branch',
  dicts: [DictType.kindergarten_scale_type],
  components: {
    BranchGardenDialog,
    BranchInfoDialog,
    JTable,
    TenantDialog,
    ChangeGarden
  },
  data() {
    return {
      groupNameList: [],
      DictType,
      options,
      selected: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 日期范围
      dateRange: [],
      // 幼儿园管理基本信息表格数据
      branchList: [],
      // 默认图片
      img: require('@/assets/images/no-picture.jpg'),
      pagination: {
        total: 0,
        pageNum: 1,
        pageSize: 10
      },
      garden: {
        gardenId: this.$route.query.groupId || ''
      },
      // 查询参数
      queryParams: Object.assign({}, initParams),
      gardenList: [],
      columns: [
        { prop: 'kindergartenName', label: '幼儿园名称', minWidth: 180, align: 'left' },
        { prop: 'staffCount', label: '园内教职工', customRender: 'staffCountType', minWidth: 100 },
        { prop: 'kindergartenType', label: '幼儿园类型', customRender: 'kindergartenType', minWidth: 100 },
        { prop: 'gardenName', label: '所属集团', customRender: 'gardenName', minWidth: 120, align: 'left' },
        // { prop: 'isGarden', label: '是否集团园', customRender: 'isGarden', width: 100 },
        { prop: 'belongRegion', label: '所属区域', minWidth: 160, formatter: ({ belongRegion }) => belongRegion || '--', align: 'left' },
        { prop: 'accountStatus', label: '账号状态', width: 80 },
        { prop: 'createBy', label: '创建人', minWidth: 100, align: 'left' },
        { prop: 'createTime', label: '创建时间', width: 160 }
      ]
    }
  },
  created() {
    this.getList()
    this.getKindergarten()
  },
  activated() {
    this.getList()
    this.getKindergarten()
  },
  methods: {
    /**
     * 渲染tags
     */
    getKindergarten() {
      this.groupNameList = []
      if (this.$route.query?.groupName) {
        this.groupNameList.push(this.$route.query?.groupName)
      }
    },
    handleChangeGarden(row) {
      if (/禁用/g.test(row.accountStatus)) return this.$modal.msgError('该幼儿园租户账号已禁用，不可调整集团')
      this.$refs.changeGardenRef.handleOpen(row)
    },
    /** 查询幼儿园管理基本信息列表 */
    getList() {
      this.loading = true
      this.garden = {
        gardenId: this.$route.query.groupId
      }
      listBranch(this.addDateRange(Object.assign({}, this.pagination, this.garden, this.queryParams), this.dateRange))
        .then(({ data, total }) => {
          this.branchList = data
          this.pagination.total = total
        }).finally(() => {
          this.loading = false
        })
    },
    /** 重置按钮操作 */
    handleReset() {
      this.dateRange = []
      this.pagination.pageNum = 1
      this.queryParams = Object.assign({}, initParams)
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      this.selected = selection
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.$refs.gardenDialogRef.handleOpen(row)
    },
    handleEdit(row, readonly) {
      row = row || this.selected[0]
      this.$refs.gardenDialogRef.handleEdit(row, readonly)
    },
    handleView(row) {
      row.gardenName = row.params && row.params.gardenName || null
      this.$refs.infoDialogRef.handleOpen(row)
    },
    /** 删除按钮操作 */
    handleRemove(row) {
      // if (row.tenantAccount) return this.$modal.msgError('账号已开通，不可删除')
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除当前幼儿园管理基本信息数据项？').then(function() {
        return logicalDelete(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /**
     * 人员跳转
     * @param row
     */
    handleJumpPeople(row) {
      this.$router.push({ path: '/information/teacher', query: { kindergartenName: row.kindergartenName, kindergartenId: row.id, kindergartenType: row.kindergartenType }})
    },
    handleManger(row) {
      this.$refs.tenantDialog.handleEdit({ id: row.tenantAccount }, true)
    },

    /**
     * 新增教职工
     */
    handleAddPeople(row) {
      this.$router.push({ path: '/information/addTeacher', query: { kindergartenName: row.kindergartenName, kindergartenId: row.id, kindergartenType: row.kindergartenType }})
    },

    /**
     * 更多触发操作
     */
    handleCommand(command, row) {
      switch (command) {
        case 'handleAddPeople(row)':
          this.handleAddPeople(row)
          break
        case 'handleChangeGarden(row)':
          this.handleChangeGarden(row)
          break
        case 'handleRemove(row)':
          this.handleRemove(row)
          break
        case 'handleManger(row)':
          this.handleManger(row)
          break
        default:
          break
      }
    },
    /**
     * 关闭tags
     */
    handleClose(val) {
      this.groupNameList.splice(this.groupNameList.indexOf(val), 1)
      this.garden.gardenId = ''
      this.$router.push({ path: '/information/branch' })
      this.getList()
    }
  }
}
</script>

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