<template>
  <div class="app-container">
    <el-form ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="岗位名称：">
        <el-input
          v-model="queryParams.postName"
          placeholder="请输入岗位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      border
      v-loading="loading"
      :data="tableData"
      height="calc(100% - 150px)"
    >
      <el-table-column label="序号" align="center" width="80">
        <template slot-scope="scope">
          <span>{{
            scope.$index + 1 + (queryParams.pageNum - 1) * queryParams.pageSize
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属部门" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.deptName ? scope.row.deptName : '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="岗位名称" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.postName ? scope.row.postName : '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提交日期" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{
            scope.row.revisionDate ? scope.row.revisionDate : '--'
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="健康安全环保中心"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span
            v-if="initChildren(scope.row, 3) == `未通过`"
            style="color: red"
            >{{ initChildren(scope.row, 3) }}</span
          >
          <span
            v-else-if="initChildren(scope.row, 3) == `审核通过`"
            style="color: green"
            >{{ initChildren(scope.row, 3) }}</span
          >
          <span v-else>{{ initChildren(scope.row, 3) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="党建工作部" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span
            v-if="initChildren(scope.row, 2) == `未通过`"
            style="color: red"
            >{{ initChildren(scope.row, 2) }}</span
          >
          <span
            v-else-if="initChildren(scope.row, 2) == `审核通过`"
            style="color: green"
            >{{ initChildren(scope.row, 2) }}</span
          >
          <span v-else>{{ initChildren(scope.row, 2) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="内控法务部&办公室"
        align="center"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span
            v-if="initChildren(scope.row, 4) == `未通过`"
            style="color: red"
            >{{ initChildren(scope.row, 4) }}</span
          >
          <span
            v-else-if="initChildren(scope.row, 4) == `审核通过`"
            style="color: green"
            >{{ initChildren(scope.row, 4) }}</span
          >
          <span v-else>{{ initChildren(scope.row, 4) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="人力资源部" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span
            v-if="initChildren(scope.row, 1) == `未通过`"
            style="color: red"
            >{{ initChildren(scope.row, 1) }}</span
          >
          <span
            v-else-if="initChildren(scope.row, 1) == `审核通过`"
            style="color: green"
            >{{ initChildren(scope.row, 1) }}</span
          >
          <span v-else>{{ initChildren(scope.row, 1) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120"
      >
        <template slot-scope="scope">
          <el-button
            v-if="(scope.row.status == 1 || scope.row.status == 2) && flag"
            type="text"
            @click="review(scope.row)"
            >审核</el-button
          >
          <el-dropdown
            size="mini"
            @command="(command) => handleCommand(command, scope.row)"
            v-hasPermi="['system:user:resetPwd', 'system:user:edit']"
          >
            <el-button size="mini" type="text" icon="el-icon-s-promotion"
              >邮件通知</el-button
            >
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item icon="el-icon-message" command="handleResetPwd"
                >审核人</el-dropdown-item
              >
              <el-dropdown-item command="handleAuthRole" icon="el-icon-message"
                >制单人</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

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

<script>
import {
  postCheckListList,
  approveTypeUserList,
  sendMailFile
} from '@/api/allApi/index.js'
import { getInfo } from '@/api/login'

export default {
  name: 'PostResponsibilityMeun',

  data() {
    return {
      queryParams: {
        statusList: [1,2,3,4], //待审核、审核中、审核通过、驳回
        postName: '',
        pageNum: 1,
        pageSize: 10
      },
      emailQuery: {
        content: '',
        serviceId: '',
        serviceType: ''
      },
      tableData: [],
      total: 0,
      loading: false,
      showSearch: true,
      isFlagArr: [],
      userList: [],
      userId: '',
      flag: false
    }
  },

  mounted() {
    this.initGetuserInfo()
    this.getList()
  },

  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      postCheckListList(this.queryParams).then((response) => {
        this.tableData = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        postName: '',
        pageNum: 1,
        pageSize: 10
      }
      this.handleQuery()
    },
    review(row) {
      this.$router.push({
        path: '/postResponsibility/postResponsibilityOrganizationLook',
        query: {
          id: row.id,
          type: 2
        }
      })
    },
    initChildren(val, type) {
      if (Array.isArray(val.children)) {
        if (val.children.length !== 0) {
          let obj = val.children.find((item) => item.type == type)
          return this.initStatus(obj?.status)
        }
      }
    },
    async initGetuserInfo() {
      getInfo().then((res) => {
        this.userId = res.user.userId
        this.initTypeUserList()
      })
    },
    async initTypeUserList() {
      const res = await approveTypeUserList()
      if (res.code !== 200) return this.$message.error(res.msg)
      this.userList = res.data
      res.data.forEach((item) => {
        this.isFlagArr.push(item.userList.some((it) => it === this.userId))

        item.userList.forEach((it) => {
          if (it === this.userId) {
            this.reviewType = item.type
          }
        })
      })
      if (this.isFlagArr.every((item) => !item)) {
        this.flag = false
      } else {
        this.flag = true
      }
    },
    initStatus(val) {
      switch (val) {
        case 1:
          return '待审核'
        case 2:
          return '审核中'
        case 3:
          return '审核通过'
        case 4:
          return '未通过'
      }
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case 'handleResetPwd':
          this.emailQuery.serviceType = 'list_approve'
          this.emailQuery.serviceId = row.id
          this.handleResetPwd(row)
          break
        case 'handleAuthRole':
          this.emailQuery.serviceType = 'list_create_user'
          this.emailQuery.serviceId = row.id
          this.handleAuthRole(row)
          break
        default:
          break
      }
    },
    handleResetPwd(row) {
      this.$confirm('确认给审核人发邮件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await sendMailFile(this.emailQuery)
          if (res.code !== 200) return this.message.error(res.msg)
          this.$message({
            type: 'success',
            message: '发送成功!'
          })
          this.getList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消发送'
          })
        })
    },
    handleAuthRole(row) {
      this.$confirm('确认给制单人发邮件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await sendMailFile(this.emailQuery)
          if (res.code !== 200) return this.message.error(res.msg)
          this.$message({
            type: 'success',
            message: '发送成功!'
          })
          this.getList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消发送'
          })
        })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
