<template>
    <div class="app-container">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="项目名称" prop="projectName">
          <el-input
            v-model="queryParams.projectName"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="项目分类" prop="projectType">
          <el-select
            v-model="queryParams.projectType"
            placeholder="请选择项目分类"
            clearable
          >
            <el-option
              v-for="dict in dict.type.project_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="建设类型" prop="projectNature">
          <el-select
            v-model="queryParams.projectNature"
            placeholder="请选择项目建设类型"
            clearable
          >
            <el-option
              v-for="dict in dict.type.project_build_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="责任主体" prop="deptId">
          <el-select v-model="queryParams.deptId" placeholder="请选择责任主体" clearable>
            <el-option
              v-for="dict in depts"
              :key="dict.deptId"
              :label="dict.deptName"
              :value="dict.deptId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="汇总时间">
            <el-date-picker
            v-model="daterangeTime"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-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"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table
        v-loading="loading"
        :data="dataList"
        :span-method="objectSpanMethod"
        height="700"
        stripe border
      >
        <el-table-column
          label="项目名称"
          align="left"
          prop="projectName"
          width="180"
          class-name="small-padding fixed-width"
          fixed="left"
          show-overflow-tooltip
        />
        <el-table-column label="参建单位" align="left" prop="unitName" width="163" show-overflow-tooltip/>
        <el-table-column label="安责险" align="center" prop="azx" width="80" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag v-if="scope.row.azx==1" type="success" effect="dark">已办理</el-tag>
            <el-tag v-if="scope.row.azx==0" type="danger" effect="dark">未办理</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="一切险" align="center" prop="yqx" width="80" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag v-if="scope.row.yqx==1" type="success" effect="dark">已办理</el-tag>
            <el-tag v-if="scope.row.yqx==0" type="danger" effect="dark">未办理</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="安全" align="center">
          <el-table-column label="安全隐患排查" align="center">
            <el-table-column label="甲方代表" align="center" prop="aqyhpc_jfdb" width="90" show-overflow-tooltip/>
            <el-table-column label="监理单位" align="center" prop="aqyhpc_jldw" width="90" show-overflow-tooltip/>
            <el-table-column label="总包单位" align="center" prop="aqyhpc_zbdw" width="90" show-overflow-tooltip/>
          </el-table-column>
          <el-table-column label="特种人员" align="center" prop="tzry" width="90" show-overflow-tooltip/>
          <el-table-column label="应急演练" align="center" prop="yjyl" width="90" show-overflow-tooltip/>
          <el-table-column label="专项培训" align="center" prop="zxpx" width="90" show-overflow-tooltip/>
        </el-table-column>
        <el-table-column label="质量" align="center">
          <el-table-column label="质量隐患排查" align="center">
            <el-table-column label="甲方代表" align="center" prop="zlyhpc_jfdb" width="90" show-overflow-tooltip/>
            <el-table-column label="监理单位" align="center" prop="zlyhpc_jldw" width="90" show-overflow-tooltip/>
            <el-table-column label="总包单位" align="center" prop="zlyhpc_zbdw" width="90" show-overflow-tooltip/>
          </el-table-column>
          <el-table-column label="取样复试" align="center" prop="clqyfs" width="90" show-overflow-tooltip/>
          <el-table-column label="材料封样" align="center" prop="clfy" width="90" show-overflow-tooltip/>
          <el-table-column label="实测实量" align="center" prop="cscl" width="90" show-overflow-tooltip/>
          <el-table-column label="举牌验收" align="center" prop="jpys" width="90" show-overflow-tooltip/>
        </el-table-column>
      </el-table>
    </div>
  </template>
  
  <script>
  import {
    selectStatisticsProjectList
  } from "@/api/statistics/statisticsProject";
  
  export default {
    name: "statisticsProject",
    components: {},
    dicts: [
      "project_build_type",
      "project_category",
    ],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 查询参数
        queryParams: {
          projectName: null,
          projectType: "1",
          projectNature: null,
          deptId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        depts: [],
        dataList:[],
        daterangeTime:[],
      };
    },
    created() {
      this.getList();
      this.$api.publics.getZgsDeptList().then((d) => {
        this.depts = d?.data || [];
      });
    },
    methods: {
      /** 查询项目管理列表 */
      getList() {
        this.loading = true;
        this.queryParams.params = {};
        if (null != this.daterangeTime && "" != this.daterangeTime) {
          this.queryParams.params["beginTime"] = this.daterangeTime[0];
          this.queryParams.params["endTime"] = this.daterangeTime[1];
        }
        selectStatisticsProjectList(this.queryParams).then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      objectSpanMethod({ row, column, rowIndex, columnIndex }) {
        // column.property是表头的key
        let _row = 1;
        if([0,4,5,10,11].includes(columnIndex)){
          var spanArr = this.getSpanArr(column.property);
          _row = spanArr[rowIndex];
        }
        const _col = _row > 0 ? 1 : 0;
        return {
          rowspan: _row,
          colspan: _col,
        };
      },
      // 处理合并行的数据
      getSpanArr(spanKey) {
        let data = this.dataList;
        var spanArr = [];
        var pos = "";
        for (var i = 0; i < data.length; i++) {
          if (i === 0) {
            spanArr.push(1);
            pos = 0;
          } else {
            // 判断当前元素与上一个元素是否相同
            if (data[i][spanKey] === data[i - 1][spanKey] && data[i].projectId === data[i - 1].projectId) {
              spanArr[pos] += 1;
              spanArr.push(0);
            } else {
              spanArr.push(1);
              pos = i;
            }
          }
        }
        return spanArr;
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download(
          "statistics/project/exportView",
          {
            ...this.queryParams,
          },
          `项目数据汇总_${new Date().getTime()}.xlsx`
        );
      },
    },
  };
  </script>
  