<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="applier">
        <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 label="申请部门" prop="applyDept">
          <el-select v-model="queryParams.applyDept" filterable clearable @change="handleQuery">
            <el-option v-for="item in $store.state.user.deptList" :key="item.deptId" :label="item.deptName"
              :value="item.deptId">
            </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:procureApproval:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="procureApprovalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="采购申请id" align="center" prop="procureApplyId" /> -->
      <el-table-column label="购置说明" align="center" prop="applyReason" />
      <el-table-column label="申请人" align="center" prop="applier">
        <template slot-scope="scope">
          {{ getUserName(scope.row.applier) }}
        </template>
      </el-table-column>
      <el-table-column label="申请部门" align="center" prop="applyDept" >
        <template slot-scope="scope">
          {{ getDeptName(scope.row.applyDept) }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="分管审核人" align="center" prop="managerUserId" /> -->
      <el-table-column label="分管审核意见" align="center" prop="managerComment" />
      <!-- <el-table-column label="分管审核时间" align="center" prop="managerTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.managerTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="总经理审批人" align="center" prop="zjlUserId" /> -->
      <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" prop="planUserId" /> -->
      <el-table-column label="计划编制时间" align="center" prop="planTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="计划编制意见" align="center" prop="planComment" /> -->
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['oa:procureApproval:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['oa:procureApproval:remove']"
          >删除</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['oa:procureApproval:query']"
          >查看</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>
      <procure-form :taskForm="taskForm" :taskName="''" :isFlow="false"></procure-form>
    </el-dialog>
  </div>
</template>

<script>
import { listProcureApproval, getProcureApproval, delProcureApproval, addProcureApproval, updateProcureApproval } from "@/api/oa/procure/procureApproval";
import procureForm from '../../flowable/form/procure/procureForm.vue';

export default {
  components: { procureForm },
  name: "ProcureApproval",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购审批表格数据
      procureApprovalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applyReason: null,
        applier: null,
        applyDept: null,
        managerUserId: null,
        managerComment: null,
        managerTime: null,
        zjlUserId: null,
        zjlComment: null,
        zjlTime: null,
        planUserId: null,
        planTime: null,
        planComment: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      taskForm: {
        formId: ''
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购审批列表 */
    getList() {
      this.loading = true;
      listProcureApproval(this.queryParams).then(response => {
        this.procureApprovalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        procureApplyId: null,
        applyReason: null,
        applier: null,
        applyDept: null,
        managerUserId: null,
        managerComment: null,
        managerTime: null,
        zjlUserId: null,
        zjlComment: null,
        zjlTime: null,
        planUserId: null,
        planTime: null,
        planComment: 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.procureApplyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购审批";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const procureApplyId = row.procureApplyId || this.ids
      getProcureApproval(procureApplyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购审批";
      });
    },    
    handleView(row) {
      this.open = true;
      this.taskForm.formId = row.procureApplyId;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.procureApplyId != null) {
            updateProcureApproval(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProcureApproval(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const procureApplyIds = row.procureApplyId || this.ids;
      this.$modal.confirm('是否确认删除采购审批编号为"' + procureApplyIds + '"的数据项？').then(function() {
        return delProcureApproval(procureApplyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/procureApproval/export', {
        ...this.queryParams
      }, `procureApproval_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
