<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="98px"
      size="mini">
      <el-form-item class="treeselectForm" label="审批人部门" prop="orgIds">
        <tree @treeselectFn="treeselectFn" :treeval="orgIds"></tree>
      </el-form-item>
      <!-- <el-form-item label="审批人姓名" prop="userId">
        <user :userId="queryParams.userId" @userIdFn="userIdsFn" @input="onInput"></user>
      </el-form-item> -->
      <el-form-item label="审批人身份" prop="approvalPostName">
        <el-input
          v-model="queryParams.approvalPostName"
          placeholder="请输入审批人身份"
          clearable
          size="mini"
          clearable />
      </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="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sfa:approvalPost:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column label="审批人账号" prop="approvalUserName" width="120" />
      <el-table-column label="审批人姓名" prop="approvalNickName" :show-overflow-tooltip="true" />
      <el-table-column label="审批人工号" prop="approvalWorkCode" width="120" />
      <el-table-column label="审批人身份" prop="approvalPostName" :show-overflow-tooltip="true" />
      <el-table-column label="部门级别" prop="deptLevel" width="150">
        <template slot-scope="scope">
          {{ storeCategoryFormatter(scope.row, scope.column) }}
        </template>
      </el-table-column>
      <!--      <el-table-column label="部门编号"  prop="deptCode" width="120"/>-->
      <el-table-column label="部门名称" prop="deptName" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</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
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sfa:approvalPost:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sfa:approvalPost:remove']"
            class="text-danger"
            >删除</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="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="98px">
        <el-form-item label="审批人姓名" prop="approvalUserId">
          <user
            :userId="form.approvalUserId"
            :approvalNickName="form.approvalNickName"
            @userIdFn="userIdFn"
            @input="onInput"></user>
        </el-form-item>
        <el-form-item label="审批人身份" prop="approvalPostName">
          <el-input v-model="form.approvalPostName" placeholder="请输入审批人身份" />
        </el-form-item>
        <el-form-item label="归属部门" prop="deptId">
          <treeselect
            v-model="form.deptId"
            :options="deptOptions"
            :show-count="true"
            placeholder="请选择归属部门"
            style="width: 100%" />
        </el-form-item>
      </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>
  </div>
</template>

<script>
import { treeselect } from '@/api/system/dept';
import user from '@/views/components/sfa/userz';
import tree from '@/views/components/sfa/treeselectParent2';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
import {
  addApprovalPost,
  approvalPostlist,
  delAprovalPost,
  updateApprovalPost,
} from '@/api/sfa/approver';
export default {
  name: 'Post',
  dicts: ['sys_normal_disable'],
  components: { Treeselect, tree, user },
  data() {
    return {
      // 部门树选项
      deptOptions: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审批人表格数据
      postList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postCode: undefined,
        approvalUserId: undefined,
        status: undefined,
        approvalPostName: undefined,
      },
      tcfxOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        approvalUserId: [{ required: true, message: '审批人名称不能为空', trigger: 'change' }],
        approvalPostName: [{ required: true, message: '审批人身份不能为空', trigger: 'blur' }],
        deptId: [{ required: true, message: '审批人部门不能为空', trigger: 'change' }],
      },
      orgIds: [],
    };
  },
  created() {
    this.getTreeselect();
    this.getList();
    this.getDicts('dept_level').then((response) => {
      this.tcfxOptions = response.data;
    });
  },
  methods: {
    storeCategoryFormatter(row, column) {
      return this.selectDictLabel(this.tcfxOptions, row.deptLevel);
    },
    treeselectFn(val) {
      this.orgIds = val;
    },
    onInput() {
      this.$forceUpdate();
    },
    userIdsFn(val) {
      this.queryParams.queryParams = val;
    },
    userIdFn(val) {
      this.form.approvalUserId = val;
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** 查询审批人列表 */
    getList() {
      this.loading = true;
      this.queryParams.deptIds = this.orgIds;
      approvalPostlist(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        approvalUserId: undefined,
        deptId: undefined,
        approvalPostName: undefined,
        approvalNickName: '',
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        postCode: undefined,
        approvalUserId: undefined,
        status: undefined,
        approvalPostName: undefined,
      };
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.postId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加审批人';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.$nextTick(() => {
        this.form.deptId = row.deptId;
        this.form.approvalPostId = row.approvalPostId;
        this.form.approvalNickName = row.approvalNickName;
        this.form.approvalPostName = row.approvalPostName;
        this.form.approvalUserId = row.approvalUserId;
      });
      this.open = true;
      this.title = '修改审批人';
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.approvalPostId != undefined) {
            updateApprovalPost(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addApprovalPost(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.approvalPostId || this.ids;
      this.$modal
        .confirm('是否确认删除审批人为"' + row.approvalNickName + '"的数据项？')
        .then(function () {
          return delAprovalPost(postIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/post/export',
        {
          ...this.queryParams,
        },
        `post_${new Date().getTime()}.xlsx`
      );
    },
    resetForm() {
      this.$nextTick(() => {
        this.$refs.form.resetFields();
      });
    },
  },
};
</script>
