<template>
  <div class="contents">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true" label-width="80px">
      <el-form-item label="申请人" prop="employeeName">
        <el-input v-model="queryParams.employeeName" placeholder="请输入申请人" clearable style="width: 250px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <treeselect style="width: 250px" :default-expand-level="1" v-model="queryParams.deptId" :options="deptOptions"
          :normalizer="normalizer" placeholder="选择部门" />
      </el-form-item>
      <el-form-item label="职位" prop="postId">
        <el-select v-model="queryParams.postId" placeholder="请选择" style="width: 250px">
          <el-option v-for="item in postOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="employeePhone">
        <el-input v-model="queryParams.employeePhone" placeholder="请输入手机号" clearable style="width: 250px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审批状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="审批状态" clearable style="width: 250px">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.BPM_TASK_AUDIT_STATUS)" :key="parseInt(dict.value)"
            :label="dict.label" :value="parseInt(dict.value)" />
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 250px" value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="">
        <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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" @click="handleExport" :loading="exportLoading"
          v-hasPermi="['system:role:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @row-click="handdle">
      <el-table-column label="申请人" align="center" prop="employeeName" min-width="120" />
      <el-table-column label="部门" align="center" prop="deptName" min-width="150" />
      <el-table-column label="职位" align="center" prop="position" min-width="150" />
      <el-table-column label="手机号" align="center" prop="employeePhone" min-width="150" />
      <el-table-column label="申请事项标题" align="center" prop="title" min-width="150" />
      <el-table-column label="申请时间" align="center" prop="createTime" min-width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批状态" align="center" prop="auditStatus" min-width="100">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.BPM_TASK_AUDIT_STATUS" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>
      <el-table-column label="当前处理人" align="center" prop="currentAuditEmployeeName" min-width="100" />
    </el-table>

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

    <!-- 修改 -->
    <el-dialog title="通用审批(修改)" :visible.sync="openEdit" width="800px" v-loading="detailLoading">
      <edit :id="id"></edit>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog title="通用审批(详情)" :visible.sync="openDetail" width="800px" v-loading="detailLoading">
      <detail :id="id"></detail>
    </el-dialog>
  </div>
</template>

<script>
import { listSimplePosts } from "@/api/system/post";
import { getListData, exportList } from "@/api/oa/universal"
import detail from "./detail.vue";
import edit from "./edit.vue";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listDept } from "@/api/system/dept";

export default {
  name: "SystemRole",
  components: {
    detail,
    edit,
    Treeselect,
  },
  data() {
    return {
      detailLoading: false,
      postOptions: [],
      id: '',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      dataList: [],
      openEdit: false,
      openDetail: false,
      // 部门列表
      deptOptions: [], // 部门属性结构
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        employeeName: undefined,
        deptId: undefined,
        postId: undefined,
        employeePhone: undefined,
        auditStatus: undefined,
        createTime: []
      },
    };
  },
  created() {
    this.getList();
    this.getListDept();
  },
  methods: {
    /** 查询部门列表 */
    getListDept() {
      listDept().then(response => {
        this.deptOptions = this.handleTree(response.data, "id");
      });
      listSimplePosts().then(response => {
        // 处理 postOptions 参数
        this.postOptions = [];
        this.postOptions.push(...response.data);
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    handdle(row) {
      this.id = row.id;
      if (row.auditStatus == 0) {
        this.openEdit = true;
      } else {
        this.openDetail = true;
      }
    },
    closeEdit() {
      this.openEdit = false;
      this.getList();
    },

    /** 查询列表 */
    getList() {
      this.loading = true;
      getListData(this.queryParams).then(
        response => {
          this.dataList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },







    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有数据项?').then(function () {
        this.exportLoading = true;
        return exportList(queryParams);
      }).then(response => {
        this.$download.excel(response, '通用审批数据.xls');
        this.exportLoading = false;
      }).catch(() => { });
    }
  }
};
</script>
