<template>
  <div class="app-container">
    <div class="title-container">
      <breadcrumb id="breadcrumb-container" class="breadcrumb-container"/>
    </div>

    <y-page-list-layout :page-list="pageData" :page-para="listQuery" :get-page-list="getList">
      <template slot="left">
        <PermissionButton
          menu-code="_views_user_add"
          class-name="filter-item"
          type="primary"
          name
          size="mini"
          :page-jump="true"
          round
          style="float: left"
        />
        <el-select
          v-model="listQuery.sex"
          placeholder="性别"
          clearable
          filterable
          style="margin-left: 20px;width: 100px;float: left;"
          class="filter-item"
        >
          <el-option label="男" value="男"/>
          <el-option label="女" value="女"/>
        </el-select>
        <el-input
          v-model="listQuery.description"
          placeholder="请输入内容"
          clearable
          style="margin-left: 20px;width: 320px;float: left;"
        >
          <el-select slot="prepend" v-model="listQueryKey" style="width: 90px;" placeholder="请选择">
            <el-option label="全部" value="keyword"/>
            <el-option label="工号" value="staffNo"/>
            <el-option label="姓名" value="name"/>
          </el-select>
        </el-input>
        <el-select clearable style="margin-left: 10px;width: 200px;float: left;" v-model="listQuery.resignState" placeholder="在职状态">
          <el-option label="在职" value="false"></el-option>
          <el-option label="离职" value="true"></el-option>
        </el-select>
        <el-button
          class="filter-item"
          style="margin-left: 10px;float: left;"
          type="primary"
          @click="searchList"
          round
        >搜索
        </el-button>
        <el-button
          class="filter-item"
          style="float: left;"
          round
          type="warning"
          @click="resetSearch()"
        >重置
        </el-button>
      </template>
      <template slot="right">
        <PermissionButton
          menu-code="_views_user_template"
          class-name="filter-item"
          round
          icon="el-icon-download"
          name="下载模板"
          @click="handleDownload"
        />
        <PermissionButton
          menu-code="_views_user_import"
          class-name="filter-item"
          round
          type="text"
          name=""
          style="padding: 0; margin-bottom: 10px"
        >
          <excelImport
            ref="uploadControl"
            flag="user/importExcel"
            :style-type="1"
            title="导入"
            @fath="getList"
          />
        </PermissionButton>
      </template>
      <parentTable
        v-loading="listLoading"
        :data="pageData.records"
        slot="table"
        style="width: 100%;"
      >
        <el-table-column label="工号" align="center">
          <template slot-scope="{row}">
            <span>{{ row.staffNo }}</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名" align="center">
          <template slot-scope="{row}">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="性别" align="center" width="120">
          <template slot-scope="{row}">
            <span>{{ row.sex }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属部门-岗位" align="center" width="360">
          <template slot-scope="{row}">
            <span v-for="(item, index) in row.departmentPostVOList" :key="index">
              <span v-if="index > 0">、</span>
              {{ item.departmentName }}-{{ item.postName }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="在职状态" align="center" width="120">
          <template slot-scope="{row}">
            <span>{{ row.resignState?'离职':'在职' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="系统状态" align="center" width="120">
          <template slot-scope="{row}">
            <span>{{ row.enable===true?'启用':'禁用' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="{row}">
            <PermissionButton
              menu-code="_views_user_detail"
              class-name="filter-item"
              name=""
              type="primary"
              :page-jump="true"
              :page-query="{id: row.id}"
              round
              size="mini"
            />
            <PermissionButton
              menu-code="_views_user_remove"
              class-name="filter-item"
              name=""
              type="danger"
              round
              size="mini"
              @click="deleteInfo(row.id)"
            />
          </template>
        </el-table-column>
      </parentTable>
    </y-page-list-layout>
  </div>
</template>
<script>
  import YPageListLayout from '@/components/YPageListLayout'
  import Breadcrumb from '@/components/Breadcrumb'
  import PermissionButton from '@/components/PermissionButton/PermissionButton'
  import { fileDown } from '../../utils/file'

  export default {
    name: 'ViewsStaffList',
    components: {
      Breadcrumb,
      YPageListLayout,
      PermissionButton,
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'info',
          deleted: 'danger',
        }
        return statusMap[status]
      },
    },
    data() {
      return {
        isDisable:false,
        tableKey: 0,
        pageData: { records: [] },
        total: 20,
        listLoading: true,
        listQuery: {
          page: 1,
          size: 10,
          // name: '',
          // staffNo: '',
          descs: 'id',
        },
        listQueryKey: 'keyword',
        importLoading: false,
      }
    },
    created() {
      const that = this
      that.getList()
    },
    methods: {
      handleDownload(url) {
        this.$utils.exportUtil('/user/download/importTemplate', {}, '模板下载')
      },
      resetSearch() {
        this.listQuery = {
          current: 1,
          size: 10,
          descs: 'id',
        }
        this.getList()
      },
      removeHandle(row) {
        // console.log(data)
        const that = this
        that
          .$confirm('确认删除当前记录吗?', '警告', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          })
          .then(async () => {
            this.$api.term.delete(row.id).then((res) => {
              if (res.code === 200) {
                this.$message({
                  type: 'success',
                  message: '删除成功',
                })
                this.getList()
              }
            })
          })
          .catch((err) => {
            console.error(err)
          })
      },
      searchList() {
        // 重置分页
        this.listQuery.page = 1
        this.listQuery.size = 20
        this.getList()
      },
      getList() {
        const that = this
        this.listLoading = true
        // console.log(that.listQuery)
        const key = {}
        key[this.listQueryKey] = this.listQuery.description
        this.$api.user
          .list(Object.assign({}, that.listQuery, key))
          .then((res) => {
            that.pageData = res.data
            setTimeout(() => {
              that.listLoading = false
            }, 200)
          })
          .catch(() => {
            that.listLoading = false
          })
      },
      deleteInfo(id) {
        const that = this
        that.$confirm('请确认是否删除该数据?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          that.$api.user.delete(id).then(data => {
            that.loading = false
            if (data.code === 200) {
              that.getList()
            } else {
              this.$message({
                type: 'error',
                message: data.msg
              })
            }
          })
        }).catch(() => {
        })
      }
    },
  }
</script>
<style lang="scss" scoped>
  .right {
    flex: 1;

    .title {
      font-size: 16px;
      font-weight: 500;
      color: rgba(51, 51, 51, 1);
      line-height: 35px;
      margin-bottom: 8px;
    }

    .menu-2-box {
      display: flex;
      flex-wrap: wrap;
      width: 100%;
    }

    .menu-2-item {
      display: flex;
      align-items: center;
      color: #656565;
      font-size: 12px;
      width: 230px;
      height: 101px;
      background: rgb(255, 185, 129);
      border-radius: 3px;
      padding-left: 20px;
      margin-right: 10px;
      margin-bottom: 10px;
      cursor: pointer;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);

      .text {
        margin-left: 16px;
      }
    }
  }
</style>
