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

    <el-table v-loading="loading" :data="safeList" @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="disclosurerUser.nickName" />
      <!-- <el-table-column label="环境要求" align="center" prop="environmentComment" />
      <el-table-column label="外业要求" align="center" prop="outsideComment" />
      <el-table-column label="内业要求" align="center" prop="insideComment" /> -->
      <el-table-column label="安全交底意见" align="center" prop="disclosureComment" width="180" show-overflow-tooltip />
      <el-table-column label="安全交底时间" align="center" prop="disclosureTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.disclosureTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接受交底人" align="center" prop="accepterUser.nickName" />
      <el-table-column label="接受交底时间" align="center" prop="acceptTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.acceptTime, '{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:safe: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" />

    <!-- 添加或修改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="disclosureAccepter">
          <el-input v-model="form.disclosureAccepter" placeholder="请输入接受交底人" />
        </el-form-item>
        <el-form-item label="安全交底人" prop="disclosurer">
          <el-input v-model="form.disclosurer" placeholder="请输入安全交底人" />
        </el-form-item>
        <el-form-item label="环境要求" prop="environmentComment">
          <el-input v-model="form.environmentComment" placeholder="请输入环境要求" />
        </el-form-item>
        <el-form-item label="外业要求" prop="outsideComment">
          <el-input v-model="form.outsideComment" placeholder="请输入外业要求" />
        </el-form-item>
        <el-form-item label="内业要求" prop="insideComment">
          <el-input v-model="form.insideComment" placeholder="请输入内业要求" />
        </el-form-item>
        <el-form-item label="安全交底意见" prop="disclosureComment">
          <el-input v-model="form.disclosureComment" placeholder="请输入安全交底意见" />
        </el-form-item>
        <el-form-item label="安全交底时间" prop="disclosureTime">
          <el-date-picker clearable v-model="form.disclosureTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择安全交底时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="接受交底时间" prop="acceptTime">
          <el-date-picker clearable v-model="form.acceptTime" 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>
      <safe-tab :taskForm="taskForm"></safe-tab>
    </el-dialog>
  </div>
</template>

<script>
import { listSafe, getSafe, delSafe, addSafe, updateSafe } from "@/api/oa/safe/safe";
import { listProject } from '@/api/oa/project/project';
import safeTab from '../../flowable/form/projectProcess/safeTab.vue';

export default {
  name: "Safe",
  components: { safeTab, },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc安全交底表格数据
      safeList: [],
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      infoOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        disclosureAccepter: null,
        disclosurer: null,
        environmentComment: null,
        outsideComment: null,
        insideComment: null,
        disclosureComment: null,
        disclosureTime: null,
        acceptTime: null
      },
      // 表单参数
      form: {},
      taskForm: {
        formId:''
      },
      // 表单校验
      rules: {
      },
      queryType: '1',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询cmc安全交底列表 */
    getList() {
      this.loading = true;
      listSafe(this.queryParams).then(response => {
        this.safeList = 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 = {
        safeId: null,
        projectId: null,
        disclosureAccepter: null,
        disclosurer: null,
        environmentComment: null,
        outsideComment: null,
        insideComment: null,
        disclosureComment: null,
        disclosureTime: null,
        acceptTime: 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.safeId)
      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 safeId = row.safeId || this.ids
      getSafe(safeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改cmc安全交底";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.safeId != null) {
            updateSafe(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSafe(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const safeIds = row.safeId || this.ids;
      this.$modal.confirm('是否确认删除cmc安全交底编号为"' + safeIds + '"的数据项？').then(function () {
        return delSafe(safeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/safe/export', {
        ...this.queryParams
      }, `safe_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
