<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">      
      <el-form-item prop="projectId">
        <el-select v-model="queryType" style="width: 100px;">
          <el-option label="项目编号" value="1"></el-option>
          <el-option label="项目名称" value="2"></el-option>
        </el-select>
        <el-select v-model="queryParams.projectId" clearable filterable remote reserve-keyword placeholder="请输入关键字"
          :remote-method="remoteMethod" :loading="loading" style="width: 300px;">
          <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 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:outsource:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="outsourceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="委外id" align="center" prop="outsourceId" /> -->
      <el-table-column label="委外说明" align="center" prop="applyReason" />      
      <el-table-column label="项目编号" align="center" prop="project.projectNumber" />
      <el-table-column label="项目名称" align="center" prop="project.projectName" width="200px" />
      <el-table-column label="合作单位" align="center" prop="partnerName" />
      <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="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </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" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['oa:outsource: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>
      <outsource-form :taskForm="taskForm" :taskName="''" :isFlow="false"></outsource-form>
    </el-dialog>
  </div>
</template>

<script>
import { listOutsource, getOutsource, delOutsource, addOutsource, updateOutsource } from "@/api/oa/outsource/outsource";
import outsourceForm from '../../flowable/form/outsource/outsourceForm.vue';
import { listProject } from '@/api/oa/project/project';

export default {
  components: { outsourceForm },
  name: "Outsource",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目委外表格数据
      outsourceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applyReason: null,
        projectId: null,
        partnerId: null,
        applier: null,
        applyDept: null,
        applyTime: null,
        managerUserId: null,
        managerComment: null,
        managerTime: null,
        zjlUserId: null,
        zjlComment: null,
        zjlTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      taskForm: {
        formId: ''
      },      
      projectList: [],
      queryType: '1',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目委外列表 */
    getList() {
      this.loading = true;
      listOutsource(this.queryParams).then(response => {
        this.outsourceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        outsourceId: null,
        applyReason: null,
        projectId: null,
        partnerId: null,
        applier: null,
        applyDept: null,
        applyTime: null,
        managerUserId: null,
        managerComment: null,
        managerTime: null,
        zjlUserId: null,
        zjlComment: null,
        zjlTime: 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.outsourceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目委外";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const outsourceId = row.outsourceId || this.ids
      getOutsource(outsourceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目委外";
      });
    },
    handleView(row) {
      this.open = true;
      this.taskForm.formId = row.outsourceId;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.outsourceId != null) {
            updateOutsource(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOutsource(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const outsourceIds = row.outsourceId || this.ids;
      this.$modal.confirm('是否确认删除项目委外编号为"' + outsourceIds + '"的数据项？').then(function() {
        return delOutsource(outsourceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/outsource/export', {
        ...this.queryParams
      }, `outsource_${new Date().getTime()}.xlsx`)
    }, 
    remoteMethod(val) {
      let params1 = {
        pageNum: 1,
        pageSize: 20,
        projectNumber: val
      }
      let params2 = {
        pageNum: 1,
        pageSize: 20,
        projectName: val
      }
      let params = {};
      if (this.queryType == '1') {
        params = params1
      } else {
        params = params2
      }
      listProject(params).then(res => {
        this.projectList = res.rows;
      })
    },
  }
};
</script>
