<template>
  <div class="NCC-common-layout">
    <div class="NCC-common-layout-left">
      <div class="NCC-common-title">
        <h2>{{ $t('common.organization') }}</h2>
      </div>
      <el-scrollbar class="NCC-common-el-tree-scrollbar">
        <el-tree
          ref="treeBox"
          v-loading="treeLoading"
          :data="treeData"
          :element-loading-text="$t('common.loadingText')"
          :props="defaultProps"
          default-expand-all
          highlight-current
          :expand-on-click-node="false"
          node-key="id"
          class="NCC-common-el-tree"
          @node-click="handleNodeClick"
        >
          <span slot-scope="{ data, node }" class="custom-tree-node">
            <i :class="data.icon" />
            <span class="text">{{ node.label }}</span>
          </span>
        </el-tree>
      </el-scrollbar>
    </div>
    <div class="NCC-common-layout-center NCC-flex-main">
      <el-row class="NCC-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="统计月份">
              <el-date-picker
                v-model="monthStr"
                type="month"
                value-format="yyyy-MM"
                format="yyyy-MM"
                placeholder="选择月份"
                style="width: 100%"
                @change="onMonthChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item :label="$t('common.keyWord')">
              <el-input
                v-model="params.keyword"
                :placeholder="$t('common.enterKeyword')"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()">
                {{ $t('common.search') }}
              </el-button>
              <el-button icon="el-icon-refresh-right" @click="reset()">
                {{ $t('common.reset') }}
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="NCC-common-layout-main NCC-flex-main">
        <div class="NCC-common-head">
          <div class="NCC-common-head-left">
            <span class="roster-hint">在职口径：有汇总以汇总「在职」为准；无汇总时入职≤该月末且（无离职日或离职≥该月1日）——当月离职仍显示；查次月时因离职日&lt;次月1日不满足「≥次月1日」，上月离职者不再出现；未入职该月者不出现在该月</span>
          </div>
          <div class="NCC-common-head-right">
            <el-tooltip effect="dark" :content="$t('common.refresh')" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh NCC-common-head-icon"
                :underline="false"
                @click="initData()"
              />
            </el-tooltip>
            <screenfull />
          </div>
        </div>
        <NCC-table v-loading="listLoading" :data="tableData">
          <el-table-column label="账号" width="168" show-overflow-tooltip>
            <template slot-scope="scope">
              <i class="icon-ym icon-ym-user roster-icon roster-icon--primary" />
              <span>{{ scope.row.account || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="姓名" width="120" show-overflow-tooltip>
            <template slot-scope="scope">
              <i class="icon-ym icon-ym-header-user roster-icon roster-icon--success" />
              <span>{{ scope.row.realName || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="组织" min-width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <i class="icon-ym icon-ym-tree-organization3 roster-icon roster-icon--info" />
              <span>{{ scope.row.department || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="门店" width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <i class="icon-ym icon-ym-generator-store roster-icon roster-icon--warning" />
              <span>{{ scope.row.mdName || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="gw" label="岗位" width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.gw || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="zw" label="职位" width="100" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.zw || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="主档在职" width="100">
            <template slot-scope="scope">
              <el-tag
                :type="(scope.row.isOnJob === 0 || scope.row.isOnJob === '0') ? 'danger' : 'success'"
                size="mini"
                disable-transitions
              >
                {{ (scope.row.isOnJob === 0 || scope.row.isOnJob === '0') ? '离职' : '在职' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="汇总状态" width="100">
            <template slot-scope="scope">
              <span v-if="scope.row.summaryEmployeeStatus == null">无</span>
              <el-tag v-else type="success" size="mini" disable-transitions>在职</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="出勤天数" width="100">
            <template slot-scope="scope">
              <span>{{ scope.row.workDays != null ? scope.row.workDays : '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="启用" width="80">
            <template slot-scope="scope">
              <el-tag :type="scope.row.enabledMark == 1 ? 'success' : 'danger'" size="mini" disable-transitions>
                {{ scope.row.enabledMark == 1 ? '正常' : '停用' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="mobilePhone" label="手机" width="130" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.mobilePhone || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="入职日期" width="120">
            <template slot-scope="scope">
              <span>{{ scope.row.entryDate ? ncc.dateFormat(scope.row.entryDate, 'YYYY-MM-DD') : '无' }}</span>
            </template>
          </el-table-column>
        </NCC-table>
        <Pagination
          :total="total"
          :page.sync="params.currentPage"
          :limit.sync="params.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getDepartmentSelector } from '@/api/permission/department'
import { getEmployeeRosterList } from '@/api/permission/employee-roster'

function currentYearMonthStr() {
  const d = new Date()
  const y = d.getFullYear()
  const m = d.getMonth() + 1
  return `${y}-${m < 10 ? '0' + m : m}`
}

export default {
  name: 'permission-employee-roster',
  data() {
    const ym = currentYearMonthStr()
    const parts = ym.split('-')
    return {
      treeData: [],
      treeLoading: false,
      listLoading: true,
      tableData: [],
      total: 0,
      monthStr: ym,
      params: {
        year: parseInt(parts[0], 10),
        month: parseInt(parts[1], 10),
        organizeId: '',
        keyword: '',
        currentPage: 1,
        pageSize: 20
      },
      defaultProps: {
        children: 'children',
        label: 'fullName'
      }
    }
  },
  created() {
    this.getOrganizeList()
  },
  methods: {
    onMonthChange(val) {
      if (!val) return
      const p = val.split('-')
      this.params.year = parseInt(p[0], 10)
      this.params.month = parseInt(p[1], 10)
    },
    getOrganizeList() {
      this.treeLoading = true
      getDepartmentSelector()
        .then((res) => {
          this.treeData = res.data.list
          this.treeLoading = false
          this.initData()
        })
        .catch(() => {
          this.treeLoading = false
        })
    },
    search() {
      this.params.currentPage = 1
      this.initData()
    },
    reset() {
      const ym = currentYearMonthStr()
      const parts = ym.split('-')
      this.monthStr = ym
      this.params.year = parseInt(parts[0], 10)
      this.params.month = parseInt(parts[1], 10)
      this.params.keyword = ''
      this.params.organizeId = ''
      this.params.currentPage = 1
      this.params.pageSize = 20
      if (this.$refs.treeBox) {
        this.$refs.treeBox.setCurrentKey(null)
      }
      this.initData()
    },
    initData() {
      this.listLoading = true
      getEmployeeRosterList(this.params)
        .then((res) => {
          this.tableData = res.data.list || []
          this.total = (res.data.pagination && res.data.pagination.total) || 0
          this.listLoading = false
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    handleNodeClick(data) {
      if (this.params.organizeId === data.id) return
      this.params.organizeId = data.id
      this.search()
    }
  }
}
</script>

<style scoped lang="scss">
.roster-hint {
  font-size: 12px;
  color: #909399;
}
.roster-icon {
  margin-right: 6px;
}
.roster-icon--primary {
  color: #409eff;
}
.roster-icon--success {
  color: #67c23a;
}
.roster-icon--info {
  color: #909399;
}
.roster-icon--warning {
  color: #e6a23c;
}
</style>
