<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="useDept">
        <el-select v-model="queryParams.useDept" 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 label="项目编号" prop="projectId">
        <el-select v-model="queryParams.projectId" clearable filterable remote reserve-keyword placeholder="请输入项目编号"
          :remote-method="remoteMethod" :loading="loading" style="width: 400px;">
          <el-option v-for="project in projectList" :key="project.projectId"
            :label="project.projectNumber + '-' + project.projectName" :value="project.projectId">
          </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="['device:record:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceApprovalList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <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="useDept" >
        <template slot-scope="scope">
          {{ getDeptName(scope.row.useDept) }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="修改后设备id" align="center" prop="modifyDevices" /> -->
      <el-table-column label="项目编号" align="center" prop="project.projectNumber" />
      <el-table-column label="项目名称" align="center" prop="project.projectName" />
      <el-table-column label="申领事由" align="center" prop="applyReason" show-overflow-tooltip />
      <el-table-column label="开始日期" align="center" prop="beginDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="天数" align="center" prop="days" />
      <!-- <el-table-column label="分管审批人" align="center" prop="managerUserId" />
      <el-table-column label="分管审批意见" align="center" prop="managerComment" />
      <el-table-column label="调度员" align="center" prop="dispatcher" />
      <el-table-column label="调度审批意见" align="center" prop="dispatchComment" />
      <el-table-column label="预计设备成本" align="center" prop="estimateCost" /> -->
      <el-table-column label="申请日期" align="center" prop="applyDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="确认日期" align="center" prop="confirmDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.confirmDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="归还日期" align="center" prop="returnDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="需维修设备id" align="center" prop="repairDevices" />
      <el-table-column label="备注说明" align="center" prop="remark" />
      <el-table-column label="部门审批时间" align="center" prop="deptTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deptTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <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="dispatchTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dispatchTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['oa:deviceApproval:list']">查看</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" />

    <!-- 添加或修改cmc设备审批对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="65%" append-to-body>
      <device-form :taskForm="taskForm" :taskName="''" :flowDisabled="false"></device-form>
    </el-dialog>
  </div>
</template>

<script>
import { listDeviceApproval, getDeviceApproval, delDeviceApproval, addDeviceApproval, updateDeviceApproval } from "@/api/oa/device/deviceApproval";
import { listProject } from '@/api/oa/project/project';
import DeviceForm from '../../flowable/form/oa/deviceForm.vue';

export default {
  name: "DeviceApproval",
  components: {
    DeviceForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc设备审批表格数据
      deviceApprovalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        applier: null,
        useDept: null,
        devices: null,
        modifyDevices: null,
        projectId: null,
        applyReason: null,
        beginDate: null,
        endDate: null,
        days: null,
        managerUserId: null,
        managerComment: null,
        dispatcher: null,
        dispatchComment: null,
        estimateCost: null,
        applyDate: null,
        confirmDate: null,
        returnDate: null,
        repairDevices: null,
        deptTime: null,
        managerTime: null,
        dispatchTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      projectList: [],
      taskForm: {
        formId: ''
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询cmc设备审批列表 */
    getList() {
      this.loading = true;
      listDeviceApproval(this.queryParams).then(response => {
        this.deviceApprovalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    remoteMethod(val) {
      listProject({
        pageNum: 1,
        pageSize: 20,
        projectNumber: val
      }).then(res => {
        this.projectList = res.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceApplyId: null,
        applier: null,
        useDept: null,
        devices: null,
        modifyDevices: null,
        projectId: null,
        applyReason: null,
        beginDate: null,
        endDate: null,
        days: null,
        managerUserId: null,
        managerComment: null,
        dispatcher: null,
        dispatchComment: null,
        estimateCost: null,
        applyDate: null,
        confirmDate: null,
        returnDate: null,
        repairDevices: null,
        remark: null,
        deptTime: null,
        managerTime: null,
        dispatchTime: 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.deviceApplyId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加cmc设备审批";
    },
    handleView(row) {
      this.taskForm.formId = row.deviceApplyId;
      this.open = true;
      this.title = "查看设备审批";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deviceApplyId = row.deviceApplyId || this.ids
      getDeviceApproval(deviceApplyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改cmc设备审批";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deviceApplyId != null) {
            updateDeviceApproval(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeviceApproval(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceApplyIds = row.deviceApplyId || this.ids;
      this.$modal.confirm('是否确认删除cmc设备审批编号为"' + deviceApplyIds + '"的数据项？').then(function () {
        return delDeviceApproval(deviceApplyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/deviceApproval/export', {
        ...this.queryParams
      }, `deviceApproval_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
