<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" v-hasPermi="['project:project:zgs']">
        <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"
          v-hasPermi="['statistics:project:export']"
          @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"
        fixed="left"
        prop="unitName"
        width="163"
        show-overflow-tooltip
      />
      <el-table-column label="项目详情" align="center">
        <el-table-column label="劳务实名制" align="center">
          <el-table-column
            label="劳务人员"
            align="center"
            prop="lwry"
            width="90"
            show-overflow-tooltip
          />
          <el-table-column label="实名制接入" align="center" prop="lwsm" width="90">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.lwsm == 1" type="success" effect="dark"
                >已接入</el-tag
              >
              <el-tag v-if="scope.row.lwsm == 0" type="danger" effect="dark"
                >未接入</el-tag
              >
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="节点预警" align="center" prop="jdyj" width="90">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.jdyj == 1" type="success" effect="dark">正常</el-tag>
            <el-tag v-if="scope.row.jdyj == 2" type="danger" effect="dark">滞后</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="项目预警" align="center">
          <el-table-column
            label="AI预警"
            align="center"
            prop="aiyjs"
            width="90"
            show-overflow-tooltip
          />
          <el-table-column
            label="安全预警"
            align="center"
            prop="aqyjs"
            width="90"
            show-overflow-tooltip
          />
          <el-table-column
            label="质量预警"
            align="center"
            prop="zlyjs"
            width="90"
            show-overflow-tooltip
          />
          <el-table-column
            label="劳资预警"
            align="center"
            prop="lzyjs"
            width="90"
            show-overflow-tooltip
        />
        </el-table-column>
      </el-table-column>
      <el-table-column label="安全管理" align="center">
        <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"
            prop="aqyhpc_jtgs"
            width="90"
            show-overflow-tooltip
          />
          <el-table-column
            label="子公司"
            align="center"
            prop="aqyhpc_zrzt"
            width="90"
            show-overflow-tooltip
          />
          <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_jtgs"
            width="90"
            show-overflow-tooltip
          />
          <el-table-column
            label="子公司"
            align="center"
            prop="zlyhpc_zrzt"
            width="90"
            show-overflow-tooltip
          />
          <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
          label="工程功能检验"
          align="center"
          prop="gcgnjy"
          width="120"
          show-overflow-tooltip
        />
      </el-table-column>
      <el-table-column label="视频监控" align="center">
        <el-table-column label="实时监控" align="center" prop="ssjk" width="90">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.ssjk != 0" type="success" effect="dark"
              >已开通</el-tag
            >
            <el-tag v-if="scope.row.ssjk == 0" type="danger" effect="dark">未开通</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="AI监控" align="center" prop="aijk" width="90">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.aijk == 1" type="success" effect="dark"
              >已开通</el-tag
            >
            <el-tag v-if="scope.row.aijk == 0" type="danger" effect="dark">未开通</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="项目全景"
          align="center"
          prop="yssys"
          width="120"
          show-overflow-tooltip
        />
      </el-table-column>
      <el-table-column label="工程管理" align="center">
        <el-table-column
          label="项目标准化"
          align="center"
          prop="bzh"
          width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="审批进行中"
          align="center"
          prop="gcsq"
          width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="审批已完成"
          align="center"
          prop="gcsp"
          width="120"
          show-overflow-tooltip
        />
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  selectStatisticsProjectList,
  exportView,
} from "@/api/statistics/statisticsProject";
import axios from "axios";
import { saveAs } from "file-saver";
import { getToken } from "@/utils/auth";

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, 6, 7, 11, 12, 13, 14, 19, 20, 21, 22].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.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];
      }
      let url = process.env.VUE_APP_BASE_API + "/statistics/project/exportView";
      axios({
        method: "post",
        url: url,
        data: this.queryParams,
        responseType: "blob",
        headers: { Authorization: "Bearer " + getToken() },
      }).then((res) => {
        this.loading = false;
        let timeStamp = new Date().getTime();
        const blob = new Blob([res.data], {
          type: "application/vnd.ms-excel;charset=utf-8",
        });
        this.saveAs(blob, decodeURIComponent("项目汇总数据_" + timeStamp + ".xlsx"));
      });
    },
    saveAs(text, name, opts) {
      saveAs(text, name, opts);
    },
  },
};
</script>
