<!--
 * @Author: wrh
 * @Date: 2024-08-14 14:24:11
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-09-05 13:53:52
-->
<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="userId">
        <el-select v-model="queryParams.userId" 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="submitTime">
        <el-date-picker clearable v-model="queryParams.submitTime" type="month" value-format="yyyy-MM"
          placeholder="请选择填报月份">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="工作月份" prop="workDate">
        <el-date-picker clearable v-model="queryParams.workDate" type="month" value-format="yyyy-MM"
          placeholder="请选择工作月份">
        </el-date-picker>
      </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:declare:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="declareList" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="55" align="center" />
      <el-table-column label="项目编号" align="center" prop="project.projectNumber" :formatter="formatterProjectNumber" />
      <el-table-column label="项目名称" align="center" prop="project.projectName" width="220px"
        :formatter="formatterProjectName" />
      <el-table-column label="填报人" align="center" prop="user.nickName" />
      <el-table-column label="工作类别" align="center" prop="workType" />
      <el-table-column label="工作项目" align="center" prop="workItem" />
      <el-table-column label="工作内容" align="center" prop="workContent" width="250px" />
      <el-table-column label="开始日期" align="center" prop="beginDate" width="92px" />
      <el-table-column label="结束日期" align="center" prop="endDate" width="92px" />
      <el-table-column label="工天" align="center" prop="workLoad" />
      <el-table-column label="工天单价" align="center" prop="price" />
      <el-table-column label="系数" align="center" prop="coefficient" />
      <el-table-column label="预估绩效" align="center">
        <template slot-scope="scope">
          {{ predictMoney(scope.row) }}
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center">
        <template slot-scope="scope">
          <div>
            <el-tag :type="formatFn(scope.row, 'checkStatus') ? 'success' : 'warning'" size="mini">一审</el-tag>
            <i v-if="formatFn(scope.row, 'checkStatus')" class="el-icon-circle-check" style="color:#67C23A"></i>
            <i v-else class="el-icon-warning-outline" style="color:#E6A23C"></i>
          </div>
          <div>
            <el-tag :type="formatFn(scope.row, 'auditStatus') ? 'success' : 'warning'" size="mini">二审</el-tag>
            <i v-if="formatFn(scope.row, 'auditStatus')" class="el-icon-circle-check" style="color:#67C23A"></i>
            <i v-else class="el-icon-warning-outline" style="color:#E6A23C"></i>
          </div>
          <div>
            <el-tag :type="formatFn(scope.row, 'confirmStatus') ? 'success' : 'warning'" size="mini">确认</el-tag>
            <i v-if="formatFn(scope.row, 'confirmStatus')" class="el-icon-circle-check" style="color:#67C23A"></i>
            <i v-else class="el-icon-warning-outline" style="color:#E6A23C"></i>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="填报时间" align="center" prop="submitTime" width="92px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitTime, '{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-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['oa:declare:edit']">修改</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="project.projectNumber" v-if="form.projectId">
          {{ form.project.projectNumber + '-' + form.project.projectName }}
        </el-form-item>
        <el-form-item label="项目名称" v-else>
          {{ form.submitTime ? form.submitTime.substring(0, 4) + '年借调、零星项目' : '借调、零星项目' }}
        </el-form-item>
        <el-form-item label="填报人" prop="userId">
          {{ getUserName(form.userId) }}
        </el-form-item>
        <el-form-item label="填报时间" prop="submitTime">
          <el-date-picker clearable v-model="form.submitTime" type="date" value-format="yyyy-MM-dd" disabled
            placeholder="请选择填报时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工作项目" prop="workItem">
          <el-input v-model="form.workItem" placeholder="请输入工作项目" disabled />
        </el-form-item>
        <el-form-item label="工作内容">
          <el-input type="textarea" v-model="form.workContent" :autosize="{ minRows: 4 }" disabled></el-input>
        </el-form-item>
        <el-form-item label="工天" prop="workLoad">
          <el-input v-model="form.workLoad" placeholder="请输入工天" disabled />
        </el-form-item>
        <el-form-item label="工天单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入工天单价" disabled />
        </el-form-item>
        <el-form-item label="系数" prop="coefficient">
          <el-input v-model="form.coefficient" placeholder="请输入系数" @blur="predictMoney(this.form)" />
        </el-form-item>
        <el-form-item label="预估绩效">
          <el-tag type="warning">{{ predictMoney(this.form) }}</el-tag>
        </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>
  </div>
</template>

<script>
import { listDeclare, getDeclare, delDeclare, addDeclare, updateDeclare } from "@/api/oa/declare/declare";
import { listProject } from '@/api/oa/project/project';

export default {
  name: "Declare",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作填报表格数据
      declareList: [],
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        userId: null,
        workType: null,
        workItem: null,
        workContent: null,
        workLoad: null,
        price: null,
        coefficient: null,
        checkStatus: null,
        auditStatus: null,
        submitTime: null,
        confirmStatus: null
      },
      // 表单参数
      form: {
        submitTime: ''
      },
      // 表单校验
      rules: {
      },
      queryType: '1',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工作填报列表 */
    getList() {
      this.loading = true;
      if (this.$store.getters.deptId > 103) {
        this.queryParams.deptId = this.$store.getters.deptId;
      }
      if (this.$store.getters.roles.includes('projectLeader')) {
        this.queryParams.projectLeader = this.$store.getters.userId
      }
      if (this.$store.getters.roles.includes('dept')) {
        this.queryParams.projectLeader = null
      }
      if (this.$store.getters.userId == 1) {
        this.queryParams.deptId = null
        this.queryParams.projectLeader = null
      }
      listDeclare(this.queryParams).then(response => {
        this.declareList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    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;
      })
    },
    // 表单重置
    reset() {
      this.form = {
        formId: null,
        projectId: null,
        userId: null,
        workType: null,
        workItem: null,
        workContent: null,
        workLoad: null,
        price: null,
        coefficient: null,
        checkStatus: null,
        auditStatus: null,
        submitTime: null,
        confirmStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.workDate) {
        this.queryParams.beginDate = this.queryParams.workDate;
        this.queryParams.endDate = this.queryParams.workDate;
      } else {
        this.queryParams.beginDate = null;
        this.queryParams.endDate = null;
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.formId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工作填报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const formId = row.formId || this.ids
      getDeclare(formId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工作填报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.formId != null) {
            updateDeclare(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeclare(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const formIds = row.formId || this.ids;
      this.$modal.confirm('是否确认删除工作填报编号为"' + formIds + '"的数据项？').then(function () {
        return delDeclare(formIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/declare/export', {
        ...this.queryParams
      }, `declare_${new Date().getTime()}.xlsx`)
    },
    formatFn(row, type) {
      if (row[type] && row[type] == '1') {
        return true
      } else {
        return false
      }
    },
    formartType(val) {
      if (val && val == '1')
        return 'success'
      else
        return 'warning'
    },
    predictMoney(row) {
      let result = parseFloat(row.price * row.workLoad * row.coefficient)
      return result.toFixed(2)
    },
    formatterProjectNumber(row) {
      if (!row.projectId) {
        return '无'
      } else {
        return row.project.projectNumber
      }
    },
    formatterProjectName(row) {
      if (!row.projectId) {
        let year = row.submitTime.substring(0, 4)
        return year + '年借调、零星项目'
      } else {
        return row.project.projectName
      }
    },
  }
};
</script>
<style lang="scss" scoped>
@import "@/assets/styles/element-reset.scss";
</style>
