<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="用户编号" prop="userCode">
        <el-input
          v-model="queryParams.userCode"
          placeholder="请输入用户编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工号" prop="jobNo">
        <el-input
          v-model="queryParams.jobNo"
          placeholder="请输入工号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入年龄"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="民族" prop="nation">
        <el-input
          v-model="queryParams.nation"
          placeholder="请输入民族"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="籍贯" prop="origo">
        <el-input
          v-model="queryParams.origo"
          placeholder="请输入籍贯"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="政治面貌" prop="politicsStatus">
        <el-input
          v-model="queryParams.politicsStatus"
          placeholder="请输入政治面貌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文化程度" prop="educationalLevel">
        <el-input
          v-model="queryParams.educationalLevel"
          placeholder="请输入文化程度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="取得职称" prop="jobTitle">
        <el-select v-model="queryParams.jobTitle" placeholder="请选择取得职称">
          <el-option
            v-for="dict in dict.type.job_title"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="入职时间" prop="hiredate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.hiredate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择入职时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="离职时间" prop="resignationTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.resignationTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择离职时间">
        </el-date-picker>
      </el-form-item>
      <el-row :gutter="10">
        <el-col :span="5">
          <el-form-item prop="department">
            <div style="display: inline-flex;position: relative;top: -10px;margin-right: 10px">
              所属部门
            </div>
            <div style="display: inline-flex;width: 80%;">
              <treeselect v-model="queryParams.department" :options="deptOptions"
                          placeholder="请选择归属部门"/>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="用户类型">
            <el-select v-model="queryParams.userType" placeholder="请选择用户类型">
              <el-option
                v-for="dict in dict.type.user_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <div class="grid-content bg-purple">
            <el-form-item label="操作">
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['files:files:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['files:files:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['files:files:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['files:files:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleImport"
          v-hasPermi="['salary:salaryslip:add']"
        >导入
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="filesList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="操作" align="center" width="110px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['files:files:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['files:files:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="用户编号" width="100px" align="center" prop="userCode"/>
      <el-table-column label="工号" align="center" prop="jobNo"/>
      <el-table-column label="用户类型" align="center" prop="userType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_type" :value="scope.row.userType"/>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="userName"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age"/>
      <el-table-column label="民族" align="center" prop="nation"/>
      <el-table-column label="籍贯" align="center" prop="origo"/>
      <el-table-column label="政治面貌" align="center" prop="politicsStatus"/>
      <el-table-column label="文化程度" align="center" prop="educationalLevel"/>
      <el-table-column label="生日" align="center" prop="birthday" width="180"/>
      <el-table-column label="居住地" align="center" prop="residence"/>
      <el-table-column label="联系电话" align="center" prop="phone"/>
      <el-table-column label="邮箱" align="center" prop="email"/>
      <el-table-column label="座机号" align="center" prop="seatNo"/>
      <el-table-column label="所属部门" align="center" prop="department"/>
      <el-table-column label="岗位名称" align="center" prop="post" width="100">
        <template slot-scope="scope">
          <div v-for="item in postData">
            <span v-if="item.postId === scope.row.post">{{ item.postName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="取得职称" align="center" prop="jobTitle" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.job_title" :value="scope.row.jobTitle"/>
        </template>
      </el-table-column>
      <el-table-column label="入职时间" align="center" prop="hiredate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hiredate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离职时间" align="center" prop="resignationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.resignationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改机构人员档案对话框 -->
    <el-dialog :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-card>
          <div style="padding: 10px">基本信息</div>
          <el-row :gutter="20">
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="用户编号" prop="userCode">
                  <el-input v-model="form.userCode" placeholder="请输入用户编号"/>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="工号" prop="jobNo">
                  <el-input v-model="form.jobNo" placeholder="请输入工号"/>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="姓名" prop="userName">
                  <el-input v-model="form.userName" placeholder="请输入姓名"/>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="性别" prop="sex">
                  <el-select v-model="form.sex" placeholder="请选择性别">
                    <el-option
                      v-for="dict in dict.type.sys_user_sex"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="年龄" prop="age">
                  <el-input v-model="form.age" placeholder="请输入年龄"/>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="民族" prop="nation">
                  <el-input v-model="form.nation" placeholder="请输入民族"/>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="籍贯" prop="origo">
                  <el-input v-model="form.origo" placeholder="请输入籍"/>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="政治面貌" prop="seatNo">
                  <el-input v-model="form.politicsStatus" placeholder="请输入政治面貌"/>
                </el-form-item>
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="文化程度" prop="educationalLevel">
                  <el-input v-model="form.educationalLevel" placeholder="请输入文化程度"/>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="生日" prop="birthday">
                  <el-date-picker clearable size="small"
                                  v-model="form.birthday"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  placeholder="选择生日">
                  </el-date-picker>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="居住地" prop="residence">
                  <el-input v-model="form.residence" placeholder="请输入居住地"/>
                </el-form-item>
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="手机号" prop="phone">
                  <el-input v-model="form.phone" placeholder="请输入手机号"/>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="邮箱" prop="eMail">
                  <el-input v-model="form.eMail" placeholder="请输入邮箱"/>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="座机号" prop="seatNo">
                  <el-input v-model="form.seatNo" placeholder="请输入座机号"/>
                </el-form-item>
              </div>
            </el-col>
          </el-row>
        </el-card>
        <el-card style="margin-top: 10px">
          <div style="padding: 10px">岗位信息</div>
          <el-row :gutter="20">
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="所属部门" prop="department">
                  <treeselect v-model="form.department" :options="deptOptions" :show-count="true"
                              placeholder="请选择归属部门"/>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="岗位名称" prop="post">
                  <el-select v-model="form.post" placeholder="请选择岗位名称">
                    <el-option
                      v-for="dict in postData"
                      :key="dict.postId"
                      :label="dict.postName"
                      :value="dict.postId"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="取得职称" prop="jobTitle">
                  <el-select v-model="form.jobTitle" placeholder="请选择取得职称">
                    <el-option
                      v-for="dict in dict.type.job_title"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <el-form-item label="用户类型">
                <el-select v-model="form.userType" placeholder="请选择用户类型">
                  <el-option
                    v-for="dict in dict.type.user_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="常用机构" prop="jobTitle">
                  <el-select v-model="form.jobTitle" placeholder="请选择常用机构">
                    <el-option
                      v-for="dict in dict.type.job_title"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="入职时间" prop="hiredate">
                  <el-date-picker clearable size="small"
                                  v-model="form.hiredate"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  placeholder="选择入职时间">
                  </el-date-picker>
                </el-form-item>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                <el-form-item label="离职时间" prop="resignationTime">
                  <el-date-picker clearable size="small"
                                  v-model="form.resignationTime"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  placeholder="选择离职时间">
                  </el-date-picker>
                </el-form-item>
              </div>
            </el-col>
          </el-row>
        </el-card>
        <el-card style="margin-top: 10px">
          <el-row>
            <el-col>
              <div class="grid-content bg-purple">
                <el-form-item label="备注" prop="remark">
                  <el-input type="textarea" :rows="2" v-model="form.remark" placeholder="请输入备注"/>
                </el-form-item>
              </div>
            </el-col>
          </el-row>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="cancelTemplate">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFiles,
  getFiles,
  delFiles,
  addFiles,
  updateFiles,
  exportFiles,
  importTemp,
  apiGetOrgInfos
} from '@/api/files/files'
import {treeselect} from '@/api/system/dept'
import user from '@/store/modules/user'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {importTempSalaryslip} from '@/api/salary/salaryslip'
import {getToken} from '@/utils/auth'
import {listDeptPost} from "@/api/system/deptPost";
import result from "@/components/Crontab/result";

export default {
  name: 'Files',
  dicts: ['sys_user_sex', 'job_title', 'user_type'],
  components: {Treeselect},
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 部门树选项
      deptOptions: undefined,
      // 总条数
      total: 0,
      // 机构人员档案表格数据
      filesList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        // 设置上传的请求头部
        headers: {Authorization: 'Bearer ' + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/files/files/importData'
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userCode: null,
        jobNo: null,
        userName: null,
        sex: null,
        age: null,
        nation: null,
        origo: null,
        politicsStatus: null,
        educationalLevel: null,
        idCard: null,
        birthday: null,
        residence: null,
        phone: null,
        email: null,
        seatNo: null,
        jobTitle: null,
        department: null,
        hiredate: null,
        resignationTime: null
      },
      // 表单参数
      form: {},
      postData: [],
      // 表单校验
      rules: {}
    }
  }, watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getTreeselect()
    this.getList()
    this.doGetOrgInfos()
    listDeptPost().then(response => {
      this.postData = response.data
    })
  },
  methods: {
    doGetOrgInfos() {
      apiGetOrgInfos().then(result => {
        console.log(result)
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id
      this.getList()
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect({
        roleTypes: user.state.roleTypes.join(','),
        organizations: user.state.organizations.join(',')
      }).then(response => {
        this.deptOptions = response.data
      })
    },
    /** 查询机构人员档案列表 */
    getList() {
      this.loading = true
      listFiles(this.queryParams).then(response => {
        this.filesList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userCode: null,
        jobNo: null,
        userName: null,
        sex: null,
        age: null,
        nation: null,
        origo: null,
        politicsStatus: null,
        educationalLevel: null,
        idCard: null,
        birthday: null,
        residence: null,
        phone: null,
        eMail: null,
        seatNo: null,
        jobTitle: null,
        department: null,
        hiredate: null,
        resignationTime: null,
        isDelete: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加机构人员档案'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      this.form = row
      this.form.orgCode = user.state.organ
      console.log(this.form)
      this.open = true
      this.title = '修改机构人员档案'
      // getFiles(id).then(response => {
      // })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFiles(this.form).then(() => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addFiles(this.form).then(() => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除机构人员档案编号为"' + ids + '"的数据项？').then(function () {
        return delFiles(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal.confirm('是否确认导出所有机构人员档案数据项？').then(() => {
        this.exportLoading = true
        return exportFiles(queryParams)
      }).then(response => {
        this.$download.name(response.msg)
        this.exportLoading = false
      }).catch(() => {
      })
    },
    handleImport() {
      this.upload.title = '员工档案导入'
      this.upload.open = true
    },
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.msg, '导入结果', {dangerouslyUseHTMLString: true})
      this.cancel()
      this.getList()
    },
    importTemplate() {
      importTemp().then(response => {
        this.$download.name(response.msg)
      })
    },
    submitFileForm() {
      this.$refs.upload.submit()
    },
    cancelTemplate() {
      this.upload.open = false
      this.form.moneyType = '2'
    }
  }
}
</script>
