<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请人" prop="userId">
        <el-select
          v-model="queryParams.applier"
          filterable
          clearable
          @change="handleQuery">
          <el-option
            v-for="item in $store.state.user.userList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <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>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['oa:transfer:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="transferList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="申请人" align="center" prop="userId">
        <template slot-scope="scope">
          <span>{{ getUserName(scope.row.applier) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请日期" align="center" prop="applyDate" width="180">
        
      </el-table-column>
      <el-table-column label="调整前所在部门" align="center" prop="beforeDeptId" >
        <template slot-scope="scope">
          <span>{{ getDeptName(scope.row.beforeDeptId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="拟调整岗位" align="center" prop="beforePostId" >
        <template slot-scope="scope">
          <span>{{ getPostById(scope.row.beforePostId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调整后所在部门" align="center" prop="afterDeptId" >
        <template slot-scope="scope">
          <span>{{ getDeptName(scope.row.afterDeptId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调整后岗位" align="center" prop="afterPostId" >
        <template slot-scope="scope">
          <span>{{ getPostById(scope.row.afterPostId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总经理意见" align="center" prop="zjlComment" />
      <el-table-column label="总经理审批时间" align="center" prop="zjlTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.zjlTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="65%" append-to-body>
      <transfer-tab :taskForm="taskForm"></transfer-tab>
    </el-dialog>
  </div>
</template>

<script>
import { listTransfer, getTransfer, delTransfer, addTransfer, updateTransfer } from "@/api/oa/transfer/transfer";
import { getUser } from "@/api/system/user";
import TransferTab from './transferTab.vue'

export default {
  components: { TransferTab },
  name: "Transfer",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc调岗申请表格数据
      transferList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //所有岗位
      postList: [],
      //所有部门
      deptList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applier: null,
        applyDate: null,
        beforeDeptId: null,
        beforePostId: null,
        afterDeptId: null,
        afterPostId: null,
        beforeDeptComment: null,
        beforeDeptLeader: null,
        beforeDeptTime: null,
        afterDeptComment: null,
        afterDeptTime: null,
        afterDeptLeader: null,
        zhComment: null,
        zhTime: null,
        zhUserId: null,
        managerComment: null,
        managerTime: null,
        managerUserId: null,
        zjlComment: null,
        zjlTime: null,
        zjlUserId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      taskForm: {
        formId: ''
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询cmc调岗申请列表 */
    getList() {
      this.loading = true;
      listTransfer(this.queryParams).then(response => {
        this.transferList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      getUser(this.$store.state.user.id).then(response => {
        this.postList = response.posts;
      });
      this.deptList = this.$store.state.user.deptList
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        transferId: null,
        userId: null,
        applyDate: null,
        beforeDeptId: null,
        beforePostId: null,
        afterDeptId: null,
        afterPostId: null,
        beforeDeptComment: null,
        beforeDeptLeader: null,
        beforeDeptTime: null,
        afterDeptComment: null,
        afterDeptTime: null,
        afterDeptLeader: null,
        zhComment: null,
        zhTime: null,
        zhUserId: null,
        managerComment: null,
        managerTime: null,
        managerUserId: null,
        zjlComment: null,
        zjlTime: null,
        zjlUserId: null
      };
      this.resetForm("form");
    },
    getPostById(val) {
      let postname = [];
      const postIds = val.split(",");
      postIds.map(id => {
        // 将字符ID转为数字进行比较
        const numericId = Number(id);
        const post = this.postList.find(post => post.postId == numericId);
        if (post) {
          postname.push(post.postName);
        }
      });

      const displayText = postname.join("、");
      return displayText;
    },
    getDeptById(val) {
      const dept = this.deptList.find(dept => dept.deptId == val);
      if (dept) {
        return dept.deptName;
      }
    },
    handleView(row){
      this.taskForm.formId = row.transferId;
      this.open = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.transferId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/transfer/export', {
        ...this.queryParams
      }, `transfer_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
