<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="reportDate">
        <el-date-picker clearable v-model="queryParams.reportDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择报告日期">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="公司人员" prop="member">
        <el-input
          v-model="queryParams.member"
          placeholder="请输入公司人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="临时人员" prop="employee">
        <el-input
          v-model="queryParams.employee"
          placeholder="请输入临时人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="填报人" prop="reporter">
        <el-select v-model="queryParams.reporter" filterable clearable style="width:220px">
            <el-option v-for="item in userList" :key="item.userId" :label="item.nickName" :value="item.userId"
              v-if="item.nickName != 'admin'">
            </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="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['oa:report:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['oa:report:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['oa:report:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="safeReportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="安全报告id" align="center" prop="safeReportId" /> -->
      <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="reportDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公司人员" align="center" prop="userNames" />
      <el-table-column label="临时人员" align="center" prop="employee" />
      <el-table-column label="安全报告" align="center" prop="content" />
      <el-table-column label="填报人" align="center" prop="reportUser.nickName" />
      <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:report:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" :disabled="isDisabled(scope.row.reporter)"
            v-hasPermi="['oa:report: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" />

    <!-- 添加或修改安全汇报对话框 -->
    <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="项目编号" prop="projectId">
          <el-button type="primary" size="mini" @click="openProject = true">选择项目</el-button>
          <el-descriptions border v-if="isSelect" style="margin-top: 10px;" :column="1">
            <el-descriptions-item label="项目编号" label-class-name="my-label">{{ chooseProject.projectNumber
            }}</el-descriptions-item>
            <el-descriptions-item label="项目名称" label-class-name="my-label">{{ chooseProject.projectName
            }}</el-descriptions-item>
          </el-descriptions>
        </el-form-item>
        <el-form-item label="报告日期" prop="reportDate">
          <el-date-picker clearable v-model="form.reportDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择报告日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="公司人员" prop="member">
          <el-select v-model="form.member" filterable multiple clearable style="width:380px">
            <el-option v-for="item in userList" :key="item.userId" :label="item.nickName" :value="item.userId"
              v-if="item.nickName != 'admin'">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="临时人员" prop="employee">
          <el-input v-model="form.employee" placeholder="请输入临时人员" />
        </el-form-item>
        <el-form-item label="安全报告" prop="content">
          <el-input type="textarea" :rows="4" v-model="form.content" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="填报人" prop="reporter">
          <el-input v-model="reporter" placeholder="请输入填报人" :disabled="true" />
        </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="选择项目" :visible.sync="openProject" width="70%" append-to-body>
      <project-choose @chooseProject="confirmProject"></project-choose>
    </el-dialog>
  </div>
</template>

<script>
import { listSafeReport, getSafeReport, delSafeReport, addSafeReport, updateSafeReport } from "@/api/oa/safe/safeReport";
import { listProject } from '@/api/oa/project/project';
import projectChoose from '@/views/flowable/form/components/chooseProject.vue';
import { listUser } from '@/api/system/user';

export default {
  name: "SafeReport",
  components: {
    projectChoose
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      canDelete: true,
      // 总条数
      total: 0,
      // 安全汇报表格数据
      safeReportList: [],
      userList: [],
      projectList: [],
      chooseProject: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openProject: false,
      isSelect: false,
      reporter: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        reportDate: null,
        member: null,
        employee: null,
        content: null,
        reporter: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [{ required: true, message: "项目编号不能为空", trigger: "blur" }],
        member: [{ required: true, message: "公司人员不能为空", trigger: "blur" }],
        reportDate: [{ required: true, message: "报告日期不能为空", trigger: "blur" }],
        content: [{ required: true, message: "安全汇报内容不能为空", trigger: "blur" }],
      },
      queryType: '1',
    };
  },
  created() {
    this.reporter = this.$store.getters.name;
    this.getList();
    this.getUserList();
  },
  methods: {
    /** 查询安全汇报列表 */
    getList() {
      this.loading = true;
      this.queryParams.member = this.$store.state.user.id
      listSafeReport(this.queryParams).then(response => {
        let rows = response.rows;
        for (let i = 0; i < rows.length; i++) {
          if (rows[i].member == null)
            rows[i].member = []
          else {
            let arr = []
            rows[i].member = rows[i].member.split(',');
            for (let member of rows[i].member) {
              arr.push(parseInt(member))
            }
            rows[i].member = arr
          }
        }
        this.safeReportList = rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询用户列表
    getUserList() {
      listUser({ pageSize: 9999, pageNum: 1 }).then(res => {
        this.userList = res.rows
      })
    },
    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;
      })
    },
    confirmProject(val) {
      if (val.length > 1) {
        this.$message.error('请选择一个项目');
        return
      } else if (val.length == 1) {
        this.chooseProject = val[0]
        this.isSelect = true
        this.form.projectId = val[0].projectId
      }
      this.openProject = false
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        safeReportId: null,
        projectId: null,
        reportDate: null,
        member: null,
        employee: null,
        content: null,
        reporter: 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.safeReportId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加安全汇报";
      this.isSelect = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const safeReportId = row.safeReportId || this.ids
      getSafeReport(safeReportId).then(response => {
        if (response.data.member != null && response.data.member != '') {
          let arr = []
          response.data.member = response.data.member.split(',');
          for (let member of response.data.member) {
            arr.push(parseInt(member))
          }
          response.data.member = arr
        }
        this.form = response.data;
        this.open = true;
        this.title = "修改安全汇报";
      });
    },
    isDisabled(reporter){
      if(reporter == this.$store.state.user.id){
        return false
      }else{
        return true
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.reporter = this.$store.getters.userId;
          this.form.member = this.form.member.join(',');
          if (this.form.safeReportId != null) {
            updateSafeReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSafeReport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const safeReportIds = row.safeReportId || this.ids;
      this.$modal.confirm('是否确认删除安全汇报编号为"' + safeReportIds + '"的数据项？').then(function () {
        return delSafeReport(safeReportIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/safeReport/export', {
        ...this.queryParams
      }, `safeReport_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
