<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="128px">
      <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="technicalDirector">
        <el-select v-model="queryParams.technicalDirector" 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="technicalDesigner">
        <el-select v-model="queryParams.technicalDesigner" 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>
        <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:plan:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="technicalList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <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="technicalDirector">
        <template slot-scope="scope">
          {{ getUserName(scope.row.technicalDirector) }}
        </template>
      </el-table-column>
      <el-table-column label="技术设计人" align="center" prop="technicalDesigner">
        <template slot-scope="scope">
          {{ getUserName(scope.row.technicalDesigner) }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="技术安排人" align="center" prop="technicalPlanner">
        <template slot-scope="scope">
          {{ getUserName(scope.row.technicalPlanner) }}
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="技术安排意见" align="center" prop="planComment" /> -->
      <el-table-column label="质量检查员" align="center" prop="qualityInspector">
        <template slot-scope="scope">
          {{ getUserNames(scope.row.qualityInspector) }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="技术安排时间" align="center" prop="technicalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.technicalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="技术方案" align="center" prop="technicalDocument" />
      <el-table-column label="方案说明" align="center" prop="designDescription" />
      <!-- <el-table-column label="技术质量与安全部审核意见" align="center" prop="technicalComment" />
      <el-table-column label="总工审核意见" align="center" prop="manageComment" /> -->
      <el-table-column label="方案上传时间" align="center" prop="technicalUploadTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.technicalUploadTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="技术质量与安全部审核时间" align="center" prop="techApprovalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.techApprovalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总工审核时间" align="center" prop="manageApprovalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.manageApprovalTime, '{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:plan:query']">查看</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['oa:plan:remove']">删除</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目id" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目id" />
        </el-form-item>
        <el-form-item label="技术负责人" prop="technicalDirector">
          <el-input v-model="form.technicalDirector" placeholder="请输入技术负责人" />
        </el-form-item>
        <el-form-item label="技术设计人" prop="technicalDesigner">
          <el-input v-model="form.technicalDesigner" placeholder="请输入技术设计人" />
        </el-form-item>
        <el-form-item label="技术安排人" prop="technicalPlanner">
          <el-input v-model="form.technicalPlanner" placeholder="请输入技术安排人" />
        </el-form-item>
        <el-form-item label="技术安排意见" prop="planComment">
          <el-input v-model="form.planComment" placeholder="请输入技术安排意见" />
        </el-form-item>
        <el-form-item label="质量检查员" prop="qualityInspector">
          <el-input v-model="form.qualityInspector" placeholder="请输入质量检查员" />
        </el-form-item>
        <el-form-item label="技术安排时间" prop="technicalTime">
          <el-date-picker clearable v-model="form.technicalTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择技术安排时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="技术方案" prop="technicalDocument">
          <el-input v-model="form.technicalDocument" placeholder="请输入技术方案" />
        </el-form-item>
        <el-form-item label="方案说明" prop="designDescription">
          <el-input v-model="form.designDescription" placeholder="请输入方案说明" />
        </el-form-item>
        <el-form-item label="技术质量与安全部审核意见" prop="technicalComment">
          <el-input v-model="form.technicalComment" placeholder="请输入技术质量与安全部审核意见" />
        </el-form-item>
        <el-form-item label="总工审核意见" prop="manageComment">
          <el-input v-model="form.manageComment" placeholder="请输入总工审核意见" />
        </el-form-item>
        <el-form-item label="方案上传时间" prop="technicalUploadTime">
          <el-date-picker clearable v-model="form.technicalUploadTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择方案上传时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="技术质量与安全部审核时间" prop="techApprovalTime">
          <el-date-picker clearable v-model="form.techApprovalTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择技术质量与安全部审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总工审核时间" prop="manageApprovalTime">
          <el-date-picker clearable v-model="form.manageApprovalTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择总工审核时间">
          </el-date-picker>
        </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>
    <el-dialog :title="title" :visible.sync="infoOpen" width="50%" append-to-body>
      <plan-tab :taskForm="taskForm"></plan-tab>
    </el-dialog>
  </div>
</template>

<script>
import { listTechnicalPlan, getTechnicalPlan, delTechnicalPlan, addTechnicalPlan, updateTechnicalPlan } from "@/api/oa/technical/technicalPlan";
import planTab from '../../flowable/form/projectProcess/planTab.vue';
import { listProject } from '@/api/oa/project/project';

export default {
  name: "TechnicalPlan",
  components: { planTab, },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc技术方案表格数据
      technicalList: [],
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      infoOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        technicalDirector: null,
        technicalDesigner: null,
        technicalPlanner: null,
        planComment: null,
        qualityInspector: null,
        technicalTime: null,
        technicalDocument: null,
        designDescription: null,
        technicalComment: null,
        manageComment: null,
        technicalUploadTime: null,
        techApprovalTime: null,
        manageApprovalTime: null
      },
      // 表单参数
      form: {},
      taskForm: {
        formId:''
      },
      // 表单校验
      rules: {
      },
      queryType: '1',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询cmc技术方案列表 */
    getList() {
      this.loading = true;
      listTechnicalPlan(this.queryParams).then(response => {
        this.technicalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    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;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        technicalPlanId: null,
        projectId: null,
        technicalDirector: null,
        technicalDesigner: null,
        technicalPlanner: null,
        planComment: null,
        qualityInspector: null,
        technicalTime: null,
        technicalDocument: null,
        designDescription: null,
        technicalComment: null,
        manageComment: null,
        technicalUploadTime: null,
        techApprovalTime: null,
        manageApprovalTime: 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.technicalPlanId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加cmc技术方案";
    },
    handleView(row){
      this.taskForm.formId = row.projectId;
      this.infoOpen = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const technicalPlanId = row.technicalPlanId || this.ids
      getTechnicalPlan(technicalPlanId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改cmc技术方案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.technicalPlanId != null) {
            updateTechnicalPlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTechnicalPlan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const technicalPlanIds = row.technicalPlanId || this.ids;
      this.$modal.confirm('是否确认删除cmc技术方案编号为"' + technicalPlanIds + '"的数据项？').then(function () {
        return delTechnicalPlan(technicalPlanIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/technical/export', {
        ...this.queryParams
      }, `technical_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
