<template>
  <div>
    <el-form ref="queryFormRef" :model="state" :inline="true" @keyup.enter.native="queryClick" :disabled="loading"
      style="margin-top: -5px;">
      <el-form-item label="数据日期:" style="margin-bottom: 0px;">
        <el-config-provider :locale="locale">
          <el-date-picker v-model="queryParams.taskDate" type="date" @change="dateChanged" value-format="YYYYMMDD"
            size="small" style="max-width: 130px;" />
        </el-config-provider>
      </el-form-item>

      <el-form-item label="审核状态:" style="margin-left: 10px;width: 180px;margin-bottom: 0px;">
        <el-select v-model="queryParams.finishedFlag" class="filter-item" size="small" style="max-width: 110px;"
          clearable>
          <el-option v-for="item in finishedFlagQuery" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="推送状态" style="margin-left: 10px;width: 190px;margin-bottom: 0px;" v-if="userType==='0'">
        <el-select v-model="queryParams.sendSlsFlag" class="filter-item" size="small" style="max-width: 120px;"
          clearable>
          <el-option v-for="item in sendSlsFlagQuery" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="遥测站编码:" style="margin-left: 10px;width: 240px;margin-bottom: 0px;" v-if="userType === '0'">
        <el-input v-model="queryParams.stationCode" placeholder="站点编码" clearable maxlength="10" size="small"
          show-word-limit />
      </el-form-item>

      <el-form-item label="系统判定:" style="margin-left: 10px;width: 190px;margin-bottom: 0px;" v-if="userType === '0'">
        <el-select v-model="queryParams.systemReviewFlag" class="filter-item" size="small" style="max-width: 120px;"
          clearable>
          <el-option v-for="item in systemReviewFlagQuery" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

      <el-form-item style="margin-bottom: 0px;">
        <el-button type="primary" class="button" @click="queryClick">查询数据</el-button>
      </el-form-item>
      <div style="float: right;">
        <el-badge :value="state.taskReviewTj.taskCount" class="item" type="info" max="999">
          <el-button style="font-size: 16px;" @click="badgeClick(0)">总计</el-button>
        </el-badge>
        <el-badge :value="state.taskReviewTj.taskCount - state.taskReviewTj.finishedFlag0" class="item" type="success"
          max="999">
          <el-button style="font-size: 16px;" @click="badgeClick(1)">已审</el-button>
        </el-badge>
        <el-badge :value="state.taskReviewTj.finishedFlag0" class="item" type="warning" max="999">
          <el-button style="font-size: 16px;" @click="badgeClick(2)">未审</el-button>
        </el-badge>
        <el-badge :value="state.taskReviewTj.finishedFlag1" class="item" type="success" max="999">
          <el-button style="font-size: 16px;" @click="badgeClick(3)">识别正确</el-button>
        </el-badge>
        <el-badge :value="state.taskReviewTj.finishedFlag2" class="item" type="danger" max="999">
          <el-button style="font-size: 16px;" @click="badgeClick(4)">审核修正</el-button>
        </el-badge>
        <el-badge :value="state.taskReviewTj.finishedFlag3" class="item" max="999">
          <el-button style="font-size: 16px;" @click="badgeClick(5)">无法辨别</el-button>
        </el-badge>
      </div>
    </el-form>
    <el-table v-loading="loading" :data="state.tableData" border stripe style="width: 100%" :height="tableHeight"
      highlight-current-row @current-change="handleCurrentRowChange" ref="my_table"
      :header-cell-style="{ background: '#606266', color: '#FFFFFF' }">
      <el-table-column fixed align="center" width="50" label="序号">
        <template #default="scope">
          <span style="font-weight: bold;font-size: 15px;">{{ (state.queryParams.page - 1) * state.queryParams.rows +
              (scope.$index + 1)
          }}</span>
          <el-divider v-if="scope.row.finishedFlag === '0'"
            style="margin: 1px 9px;border-top: 1px dashed red;width: 13px;" />
        </template>
      </el-table-column>
      <!-- <el-table-column prop="taskDate" label="数据批次" header-align="center" width="90"></el-table-column> -->
      <el-table-column label="遥测站信息" header-align="center" width="110">
        <template #default="scope">
          <span style="font-weight: bold;cursor:pointer;color: blue;" @click="loadHistoryData(scope.row)">{{
              scope.row.stationCode
          }}</span>
          <el-divider border-style="border" />
          <span style="font-size: 13px;" title="双击复制[SN]到剪切板" @dblclick="copySnToShearPlate">{{
              scope.row.deviceSn
          }}</span>
          <el-divider border-style="border" />
          <span style="font-size: 13px;">{{ scope.row.stationName }}</span>
        </template>
      </el-table-column>

      <el-table-column label="版本信息" header-align="center" width="145">
        <template #default="scope">
          <span style="font-size: 10px;">MCU：{{ scope.row.softVer }}</span>
          <el-divider border-style="border" />
          <span style="font-size: 10px;">AI&nbsp&nbsp&nbsp&nbsp&nbsp：{{ scope.row.aiVersion }}</span>
          <el-divider border-style="border" />
          <span style="font-size: 10px;">CPU：{{ scope.row.hardVersion }} |
            NB:{{ scope.row.nbVersion }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="sampleTime" label="抄表时间" header-align="center" width="100">
        <template #default="scope">
          <span v-if="checkToday(scope.row)">{{ scope.row.sampleTime }}</span>
          <span v-else style="color:red;font-weight: bold;">{{
              scope.row.sampleTime
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备侧图片" header-align="center" width="170">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-image :src="scope.row.mcuImageUrl" :hide-on-click-modal="true" :preview-src-list="scope.row.srcList"
              :close-on-press-escape="true">
              <template #error>
                <div slot="error" class="image-slot">
                  <!-- <el-icon><icon-picture /></el-icon> -->
                  <i class="el-icon-picture-outline"
                    style="font-size: 30px;text-align: center;display: block;width: 120px;"></i>
                  <span style="color: red;text-align: center;display: block;"
                    v-if="scope.row.funCode === '52'">补发报(无图)</span>
                  <span style="color: red;display:inline-block;width:120px;text-align:center;"
                    v-if="scope.row.funCode !== '52'">图片异常</span>
                </div>
              </template>
            </el-image>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="读数信息" header-align="center" width="140">
        <template #default="scope">
          <span>设备侧：</span>
          <span v-if="scope.row.idenValidFlag === '1'" style="color:blue;font-weight: bold;"
            @click="lastValueClick(scope.row, 1)">{{ scope.row.identValue
            }}</span>
          <span v-if="scope.row.idenValidFlag !== '1'" style="color:red;font-weight: bold;"
            @click="lastValueClick(scope.row, 1)">{{ scope.row.identValue
            }}</span>
          <el-divider border-style="border" />
          <span>服务侧：</span>
          <span v-if="goodFlag(scope.row.serverValidFlag)" style="color:blue;" @click="lastValueClick(scope.row, 2)">{{
              scope.row.serverValue
          }}</span>
          <span v-if="!goodFlag(scope.row.serverValidFlag)" style="color:red;" @click="lastValueClick(scope.row, 2)">{{
              scope.row.serverValue
          }}</span>
          <el-divider border-style="border" />
          <span>比&nbsp&nbsp&nbsp&nbsp对：</span>
          <span v-if="scope.row.valueMatched !== '1'" style="color:red;">✖</span>
          <span v-if="scope.row.valueMatched === '1'" style="color:green;">✔</span>
        </template>
      </el-table-column>

      <el-table-column prop="reviewValue" label="人工审核值" header-align="center" width="130">
        <template #default="scope">
          <el-input type="number" maxlength="15" v-model="scope.row.reviewValue" placeholder="审核值" class="input"
            :disabled="scope.row.finishedFlag > 0 || userType !== '0'" />
        </template>
      </el-table-column>

      <el-table-column align="center" width="110" label="审核处理" v-if="userType === '0' || createUnitFalg === '1'">
        <template #default="scope">
          <el-space direction="vertical">
            <el-tag size="larger">
              <span v-if="scope.row.finishedFlag > 0" style="color: dimgrey;font-size:15px;"
                @click="updateData(scope.row, '1')">识别正确</span>
              <span v-if="scope.row.finishedFlag == 0 || scope.row.finishedFlag == ''"
                style="color: blue;cursor:pointer;text-decoration: underline;font-size:15px;text-underline-offset: 3px;"
                @click="updateData(scope.row, '1')">识别正确</span>
            </el-tag>
            <el-tag size="larger">
              <span v-if="scope.row.finishedFlag > 0" style="color:dimgrey;font-size:15px;"
                @click="updateData(scope.row, '2')">审核修正</span>
              <span v-if="scope.row.finishedFlag == 0 || scope.row.finishedFlag == ''"
                style="color: blue;cursor:pointer;text-decoration: underline;font-size: 15px;text-underline-offset: 3px;"
                @click="updateData(scope.row, '2')">审核修正</span>
            </el-tag>
            <el-tag size="larger">
              <span v-if="scope.row.finishedFlag > 0" style="color:dimgrey;font-size:15px;"
                @click="updateData(scope.row, '3')">无法辨别</span>
              <span v-if="scope.row.finishedFlag == 0 || scope.row.finishedFlag == ''"
                style="color: blue;cursor:pointer;text-decoration: underline;font-size: 15px;text-underline-offset: 3px;"
                @click="updateData(scope.row, '3')">无法辨别</span>
            </el-tag>
          </el-space>
        </template>
      </el-table-column>
      <el-table-column prop="finishedFlag" label="审核状态" header-align="center" width="85" align="center">
        <template #default="scope">
          <span v-if="scope.row.finishedFlag === '0'" style="color:red;font-weight: bold;">未审</span>
          <span v-if="scope.row.finishedFlag === '1'"
            style="color:green;font-weight: bold;cursor:pointer;text-decoration: underline;"
            @click="revokeReview(scope.row)">识别正确</span>
          <span v-if="scope.row.finishedFlag === '2'"
            style="color:chocolate;font-weight: bold;cursor:pointer;text-decoration: underline;"
            @click="revokeReview(scope.row)">审核修正</span>
          <span v-if="scope.row.finishedFlag === '3'"
            style="color:chocolate;font-weight: bold;cursor:pointer;text-decoration: underline;"
            @click="revokeReview(scope.row)">无法辨别</span>
        </template>
      </el-table-column>
      <el-table-column prop="sendSlsFlag" label="推送状态" header-align="center" width="85" align="center">
        <template #default="scope">
          <span v-if="scope.row.sendSlsFlag === '0'" style="color:black;">-</span>
          <span v-if="scope.row.sendSlsFlag === '1'" style="color:black;">无需推送</span>
          <span v-if="scope.row.sendSlsFlag === '2'"
            style="color:blue;font-weight: bold;cursor:pointer;text-decoration: underline;text-underline-offset: 3px;"
            @click="sendReviewToSls(scope.row)">需要推送</span>
          <span v-if="scope.row.sendSlsFlag === '3'" style="color:green;">推送完成</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="历史数据" header-align="center" width="85" align="center">
        <template #default="scope">
          <span style="color:blue;cursor:pointer;text-decoration: underline;text-underline-offset: 3px;font-size:15px;"
            @click="loadHistoryData(scope.row)">查看历史</span>
        </template>
      </el-table-column> -->

      <el-table-column label="上次审核图" header-align="center" width="170"
        v-if="userType === '0' || createUnitFalg === '1'">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-image :src="scope.row.lastMcuImageUrl" :hide-on-click-modal="true" :close-on-press-escape="true">
              <template #error>
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"
                    style="font-size: 30px;text-align: center;display: block;width: 120px;"></i>
                  <span style="color: red;text-align: center;display: block;"
                    v-if="scope.row.funCode === '52'">补发报(无图)</span>
                  <span style="color: red;display:inline-block;width:120px;text-align:center;"
                    v-if="scope.row.funCode !== '52'">图片异常</span>
                </div>
              </template>
            </el-image>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="上次审核数据" header-align="center" width="155"
        v-if="userType === '0' || createUnitFalg === '1'">
        <template #default="scope">
          <span>抄表时间:{{ scope.row.lastSampleTime }}</span>
          <el-divider border-style="border" />
          <span v-if="scope.row.finishedFlag > 0">审核值：{{ scope.row.lastReviewValue }}</span>
          <span v-if="scope.row.finishedFlag == 0 || scope.row.finishedFlag == ''" title="点击自动填入到人工审核值"
            @click="lastValueClick(scope.row, 3)"
            style="color:blue;font-weight: bold;cursor:pointer;text-decoration: underline;">审核值：{{
                scope.row.lastReviewValue
            }}</span>
        </template>
      </el-table-column>

      <el-table-column label="配置信息" header-align="center" width="120">
        <template #default="scope">
          <span style="font-size: 10px;">数字个数：{{ scope.row.digitNum }}</span>
          <el-divider border-style="border" />
          <span style="font-size: 10px;">尾数单位：{{ global.getUnitType(scope.row.uintType) }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="reviewMan" label="审核人" header-align="center" width="90"></el-table-column>
      <el-table-column prop="reviewTime" label="审核时间" header-align="center" width="100"></el-table-column>
    </el-table>
    <div>
      <span
        style="color:blue;font-weight: bold;cursor:pointer;text-decoration: underline;text-align: center;margin-left:10px;font-size: 15px;"
        @click="showCompareView">校对结果</span>
      <el-config-provider :locale="locale">
        <el-pagination v-model:currentPage="queryParams.page" v-model:page-size="queryParams.rows"
          :page-sizes="[10, 15, 20, 50, 100]" :small="state.small" :disabled="state.disabled"
          :background="state.background" layout="total, sizes, prev, pager, next, jumper" :total="state.total"
          style="text-align: center; width: 90%;float:center;display:inline-block;line-height:30px;margin-top: 5px;"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </el-config-provider>
    </div>

    <el-dialog :title="formdialog.title" v-model="formdialog.visible" width="1150px" draggable center
      @close="dialogClose">
      <el-form ref="formRef" :model="formData" label-width="100px" style="margin-top: -20px;">
        <el-table v-loading="loading" :data="state.historyData" border stripe style="width: 100%" height="530px"
          highlight-current-row @current-change="handleCurrentRowChange"
          :header-cell-style="{ background: '#606266', color: '#FFFFFF' }">
          <el-table-column fixed type="index" width="50" align="center" />
          <el-table-column fixed prop="deviceSn" label="设备" header-align="center" width="140">
            <template #default="scope">
              <el-space direction="vertical" alignment="start">
                <el-tag size="small" style="width: 110px">SN:<span style="color: blue;">{{ scope.row.deviceSn }}</span>
                </el-tag>
                <el-tag size="small" style="width: 110px">ID:<span style="color: green;">{{ scope.row.stationCode
                }}</span></el-tag>
                <el-tag size="small" style="width: 110px">MC:<span :title="scope.row.stationName"
                    style="color: green;width: 63px;display: inline-block;">{{
                        scope.row.stationName != null && scope.row.stationName.length > 5 ?
                          scope.row.stationName.substring(0, 5) + '...' : scope.row.stationName
                    }}</span>
                </el-tag>
              </el-space>
            </template>
          </el-table-column>

          <el-table-column label="图片" header-align="center" width="150">
            <template #default="scope">
              <div style="display: flex; align-items: center">
                <el-image :src="scope.row.src" :preview-src-list="scope.row.srcList" :hide-on-click-modal="true"
                  :close-on-press-escape="true" :title="global.getImageNameTip(scope.row.smallJpgUrl)">
                  <template #error>
                    <div slot="error" class="image-slot">
                      <!-- <el-icon><icon-picture /></el-icon> -->
                      <i class="el-icon-picture-outline"
                        style="font-size: 30px;text-align: center;display: block;width: 120px;"></i>
                      <span style="color: red;text-align: center;display: block;"
                        v-if="scope.row.funCode === '52'">补发报(无图)</span>
                      <span style="color: red;display:inline-block;width:120px;text-align:center;"
                        v-if="scope.row.funCode !== '52'">图片异常</span>
                    </div>
                  </template>
                </el-image>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="读数信息" header-align="center" width="130">
            <template #default="scope">
              <el-space direction="vertical" alignment="start">
                <el-tag size="small">结果：<span style="color: blue">{{ scope.row.identValue }}</span>
                </el-tag>
                <el-tag size="small">原始：<span style="color: green">{{ scope.row.rawValue }}</span>
                </el-tag>
                <el-tag size="small">审核：<span style="color: green">{{ scope.row.reviewValue }}</span>
                </el-tag>
              </el-space>
            </template>
          </el-table-column>
          <el-table-column label="时间" header-align="center" width="180">
            <template #default="scope">
              <el-space direction="vertical" alignment="start">
                <el-tag size="small">启动时间：<span style="color: blue">{{
                    tool.dateFormat(scope.row.sampleTime)
                }}</span></el-tag>
                <el-tag size="small">发送时间：<span style="color: green">{{
                    tool.dateFormat(scope.row.rptTime)
                }}</span>
                </el-tag>
                <el-tag size="small">接收时间：<span style="color: red">{{
                    tool.dateFormat(scope.row.revTime)
                }}</span>
                </el-tag>
              </el-space>
            </template>
          </el-table-column>

          <el-table-column fit label="调试信息" header-align="center" width="410">
            <template #default="scope">
              <el-row>
                <el-col>
                  <span style="font-size:12px;" v-for="(item, i) in scope.row.alist">{{
                      global.debugInfoFormat(item, i)
                  }}</span>
                </el-col>
              </el-row>

              <el-row>
                <el-col>
                  <span style="font-size:12px;" v-for="(item, i) in scope.row.blist">{{
                      global.debugInfoFormat(item, i)
                  }}</span>
                </el-col>
              </el-row>
              <el-row v-if="scope.row.serverAList">
                    <el-divider style="margin-top: -2px;margin-bottom: -5px;height: 1px;" />
                  </el-row>
                  <el-row v-if="scope.row.serverAList">
                    <el-col>
                      <span style="font-size:12px;" v-for="(item, i) in scope.row.serverAList">{{
                        global.debugInfoFormat(item, i)
                      }}</span>
                    </el-col>
                  </el-row>

                  <el-row v-if="scope.row.serverAList">
                    <el-col>
                      <span style="font-size:12px;" v-for="(item, i) in scope.row.serverBList">{{
                        global.debugInfoFormat(item, i)
                      }}</span>
                    </el-col>
                  </el-row>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel()">关闭</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog :title="compareDialog.title" v-model="compareDialog.visible" width="1010px" draggable center>
      <el-form :model="reviewCompareTable" label-width="100px" style="margin-top: -20px;">
        <el-table v-loading="loading" :data="state.reviewCompareTable" border stripe style="width: 100%" height="530px"
          highlight-current-row @current-change="handleCurrentRowChange"
          :header-cell-style="{ background: '#606266', color: '#FFFFFF' }">
          <el-table-column type="index" width="50" align="center" />
          <el-table-column prop="deviceSn" label="设备SN" header-align="center" width="110" />
          <el-table-column prop="stationCode" label="遥测站编码" header-align="center" width="100" />
          <el-table-column prop="stationName" label="遥测站名称" header-align="center" width="190" show-overflow-tooltip />
          <el-table-column prop="reviewValue" label="审核值" header-align="center" width="110" align="right" />
          <el-table-column prop="iotDataValue" label="感知平台读数" header-align="center" width="120" align="right" />

          <el-table-column prop="compareType" label="对比结果" header-align="center" width="130">
            <template #default="scope">
              <span v-if="scope.row.compareType === '1'">数值不等</span>
              <span v-if="scope.row.compareType === '2'">感知平台无数据</span>
              <span v-if="scope.row.compareType === '3'">感知平台值无效</span>
              <span v-if="scope.row.compareType === '4'">审核值异常</span>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" header-align="center" width="120">
            <template #default="scope">
              {{ tool.dateFormat(scope.row.createTime, "YYYY-MM-DD HH:mm:ss") }}
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, toRefs, toRef, ref, onMounted } from "vue";
import { ElForm, ElMessage, ElMessageBox } from 'element-plus';
import TaskReviewAction from "@/api/taskreview";
import { TaskReviewQueryParam, TaskReviewModel, Dialog, TAireadQueryParam, TAireadModel, TaskReviewTjModel, TReviewCompareModel, TReviewCompareQueryParam } from '@/types';
import { PageResult } from "@/types/api/base";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import Moment from "moment";
import TAiReadAction from "@/api/tairead"
import tool from "@/until/tool"
import global from '@/until/global';
import TReviewCompareAction from '@/api/treviewcompare';

let locale = ref(zhCn);
const queryFormRef = ref(ElForm); // 属性名必须和元素的ref属性值一致
const my_table = ref();

const loading = ref(false);
const userType = localStorage.getItem("UserType")?.toString();
const unitCode = localStorage.getItem("UnitCode") + "";
const createUnitFalg = localStorage.getItem("CreateUnitFalg") + "";
const state = reactive({
  currentType: 0,
  queryParams: { page: 1, rows: 10 } as TaskReviewQueryParam,
  formdialog: {} as Dialog,
  formData: {} as TaskReviewModel,
  tableData: [] as TaskReviewModel[],
  defaultTime: new Date(),
  total: 0,
  small: false,
  background: true,
  disabled: false,
  historyData: [] as TAireadModel[],
  taskReviewTj: {} as TaskReviewTjModel,
  sameDay: '',
  reviewCompareTable: [] as TReviewCompareModel[],
  compareDialog: {} as Dialog,
});

//审核状态
const finishedFlagQuery = [
  {
    value: '0',
    label: '全部',
  },
  {
    value: '1',
    label: '已审数据',
  },
  {
    value: '2',
    label: '未审数据',
  },
  {
    value: '3',
    label: '识别正确',
  },
  {
    value: '4',
    label: '审核修正',
  },
  {
    value: '5',
    label: '无法辨别',
  },
]

//推送水利所标记
const sendSlsFlagQuery = [
  {
    value: 0,
    label: '全部数据',
  },
  {
    value: 1,
    label: '需要推送',
  },
  {
    value: 2,
    label: '推送完成',
  },
]

//系统审核标记
const systemReviewFlagQuery = [
  {
    value: 0,
    label: '需要审核',
  },
  {
    value: 1,
    label: '一级免审',
  },
  {
    value: 2,
    label: '二级免审',
  },
  {
    value: 3,
    label: '三级免审',
  },
  {
    value: 4,
    label: '四级免审',
  },
  {
    value: 5,
    label: '五级免审',
  }
]

const {
  queryParams,
  formdialog,
  formData,
  tableData,
  total,
  defaultTime,
  historyData,
  reviewCompareTable,
  compareDialog
} = toRefs(state);

const indexMethod = (index: number) => {
  return index * 2
}
const tableHeight = ref(document.documentElement.clientHeight - 170);

const currentRow = ref();

const handleCurrentRowChange = (val: TaskReviewModel | undefined) => {
  currentRow.value = val;
};

const handleSizeChange = (val: number) => {
  queryParams.value.rows = val;
  queryList();
};

const handleCurrentChange = (val: number) => {
  queryParams.value.page = val;
  queryList();
  if (Object.keys(my_table).length > 0) { if (my_table.value != null) { (my_table.value as any).$refs.bodyWrapper.scrollTop = 0; } }
};

const showCompareView = () => {
  state.compareDialog = {
    title: '感知平台审核同步情况[' + state.queryParams.taskDate + ']',
    visible: true,
    disabled: false,
  };
  queryCompareList();
}

const queryCompareList = () => {
  let params = {} as TReviewCompareQueryParam;
  params.taskDate = state.queryParams.taskDate;
  params.page = 1;
  params.rows = 999;
  if (userType === '1') {
    params.unitCode = unitCode;
  }
  TReviewCompareAction.QueryReviewCompareByQueryParam(params).then(result => {
    if (result.status === 200) {
      const { res, flag, error } = result.data;
      if (flag != undefined && flag == 1) {
        const pageData = res as PageResult<TReviewCompareModel[]>;
        state.reviewCompareTable = pageData.list;
      } else {
        ElMessage.error(error);
      }
    }
  }).catch(error => {
    //请求失败时的操作     
    console.log(error);
  })
}

const queryList = () => {
  if (tool.stringIsNullOrEmpty(state.queryParams.taskDate)) {
    state.tableData = [] as TaskReviewModel[];
    state.taskReviewTj = {} as TaskReviewTjModel;
    state.queryParams.page = 1;
    state.queryParams.rows = 10;
    state.total = 0;
    return;
  }

  if (state.queryParams.stationCode) {
        state.queryParams.stationCode = state.queryParams.stationCode.padStart(10, '0');     //不足10位前面自动补0
      }

  loading.value = true;

  if (userType === '1') {
    state.queryParams.unitCode = unitCode;
  }
  TaskReviewAction.QueryTaskReviewListByTaskDate(state.queryParams).then(result => {
    loading.value = false;
    if (result.status === 200) {
      const { res, flag, error } = result.data;
      if (flag != undefined && flag == 1) {
        const pageData = res as PageResult<TaskReviewModel[]>;
        pageData.list.forEach(element => {
          element.srcList = [];
          if (element.mcuImageUrl) {
            element.srcList.push(element.mcuImageUrl);
          }
          if (element.serverImageUrl) {
            element.srcList.push(element.serverImageUrl);
          }

          //未审核的给一个推荐值
          if (element.finishedFlag == '0') {
            if (element.idenValidFlag == "1") {
              element.reviewValue = element.identValue;
            }
            else {
              element.reviewValue = element.serverValue;
            }
          }
        })
        state.tableData = pageData.list;
        state.total = pageData.total;

        getReviewCount();
      } else {
        ElMessage.error(error);
      }
    }
  }).catch(error => {
    //请求失败时的操作     
    console.log(error);
  })
  //loading.value = false;
};

const dateChanged = () => {
  state.sameDay = state.queryParams.taskDate + '16';
  state.queryParams.page = 1;
  queryList();
}

const updateData = (row: any, type: string) => {
  state.formData = row;
  if (state.formData.finishedFlag != "0") {
    ElMessage.warning("已经审核过");
    return;
  }

  state.formData.finishedFlag = type;
  state.formData.reviewMan = localStorage.getItem("UserName") + "";
  if (type == "3") {
    state.formData.reviewValue = 0;
  }
  else if (type == "1") {
    state.formData.reviewValue = state.formData.identValue;         //防止点的是识别正确,但输入框的值被修改了
  }
  TaskReviewAction.EditTaskReview(state.formData).then(result => {
    if (result.status === 200) {
      const { res, flag, error } = result.data;
      if (flag === "1") {
        queryList();
      } else {
        ElMessage.error(error);
      }
    }
  });
}

const sendReviewToSls = (row: any) => {
  let model = row as TaskReviewModel;
  TaskReviewAction.SendReviewToSls(model).then(result => {
    if (result.status === 200) {
      const { res, flag, error } = result.data;
      if (flag === "1") {
        model.sendSlsFlag = (res.data as TaskReviewModel).sendSlsFlag;
      } else {
        ElMessage.error(error);
      }
    }
  });
}

onMounted(() => {
  //当前未到16点,显示昨日数据,过16点显示当天 2022-09-21 guozhao
  let date = new Date();
  state.queryParams.taskDate = Moment(date).format("YYYYMMDD");
  if (date.getHours() < 16) {
    state.queryParams.taskDate = Moment(date).subtract(1, 'days').format("YYYYMMDD");
  }
  state.sameDay = state.queryParams.taskDate + '16';
  window.onresize = function () {
    tableHeight.value = document.documentElement.clientHeight - 180
  }
  queryList();
})

const loadHistoryData = (row: any) => {
  let model = row as TaskReviewModel;
  let queryParams = {} as TAireadQueryParam;
  queryParams.stationCode = model.stationCode;
  queryParams.page = 1;
  queryParams.rows = 100;
  queryParams.startTime = Moment((new Date()).getTime() - 1000 * 3600 * 24 * 30).format("YYYYMMDDHHmmss");
  queryParams.endTime = Moment((new Date())).format("YYYYMMDDHHmmss");
  TAiReadAction.QueryTAireadListByStation(queryParams)
    .then((result) => {
      if (result.status === 200) {
        const { res, flag, error } = result.data;
        if (flag === "1") {
          const pageData = res as PageResult<TAireadModel[]>;

          pageData.list.forEach(element => {
            element.srcList = [];
            element.alist = [];
            element.blist = [];
            if (element.smallJpgUrl) {
              element.src = element.smallJpgUrl;
              element.srcList.push(element.smallJpgUrl);
            }

            if (element.bigJpgUrl) {
              element.src = element.bigJpgUrl;
              element.srcList.push(element.bigJpgUrl);
            }

            if (element.szsbz) {
              let alist = element.szsbz.split(",");
              element.alist = alist;
            }

            if (element.szsbgl) {
              let blist = element.szsbgl.split(",");
              element.blist = blist;
            }
            if (element.serverReadModel) {
              if (element.serverReadModel.szsbz) {
                element.serverAList = element.serverReadModel.szsbz.split(",");
              }
              if (element.serverReadModel.szsbgl) {
                element.serverBList = element.serverReadModel.szsbgl.split(",");
              }
            }
          });

          state.historyData = pageData.list;
        } else {
          ElMessage.warning(error);
          loading.value = false;
        }
      }
      else {
        loading.value = false;
      }
    })
    .catch((error) => {
      //请求失败时的操作
      ElMessage.error(error || "未知异常...");
      loading.value = false;
    });
  state.formdialog = {
    title: '[' + model.stationName + ']历史数据查看',
    visible: true,
    disabled: false,
  };
}

const cancel = () => {
  state.historyData = [] as TAireadModel[];
  state.formdialog = {
    title: '',
    visible: false,
    disabled: true,
  };
}

const dialogClose = () => {
  state.historyData = [] as TAireadModel[];
}

const revokeReview = (row: any) => {
  if (userType === '1') {
    return;
  }
  let model = row as TaskReviewModel;
  ElMessageBox.confirm(
    ' 是否确认撤销[' + model.stationName + ']站点的审核数据? ',
    '提示',
    {
      cancelButtonText: '取消',
      confirmButtonText: '确认',
      type: 'warning',
      center: true,
    }
  )
    .then(() => {
      TaskReviewAction.RevokeReview(model.id).then(result => {
        if (result.status === 200) {
          const { res, flag, error } = result.data;
          if (flag === "1") {
            queryList();
          } else {
            ElMessage.error(error);
          }
        }
      });
    })
}

//获取统计数量
const getReviewCount = () => {
  let ax = null;
  if (userType === '0') {
    ax = TaskReviewAction.GetTaskReviewCount(state.queryParams.taskDate);
  } else {
    ax = TaskReviewAction.GetTaskReviewCountByUnitCode(state.queryParams.taskDate, unitCode);
  }

  ax.then(result => {
    if (result.status === 200) {
      const { res, flag, error } = result.data;
      if (flag != undefined && flag == 1) {
        state.taskReviewTj = res as TaskReviewTjModel;
      } else {
        ElMessage.error(error);
      }
    }
  }).catch(error => {
    //请求失败时的操作     
    console.log(error);
  })
}

//点击角标触发更改查询条件
const badgeClick = (type: number) => {
  state.queryParams.page = 1;
  state.queryParams.finishedFlag = type + "";
  queryList();
}

const lastValueClick = (row: any, type: number) => {
  let model = row as TaskReviewModel;
  if (model.finishedFlag !== '0') {
    return;
  }
  if (type === 1) {
    model.reviewValue = model.identValue;
  } else if (type === 2) {
    model.reviewValue = model.serverValue;
  } else {
    model.reviewValue = model.lastReviewValue;
  }
}

const queryClick = () => {
  state.queryParams.page = 1;
  state.sameDay = state.queryParams.taskDate + '16';
  queryList();
}

/**
 * 
 * @param val 判断是否当日16点的数据
 */
const checkToday = (val: TaskReviewModel) => {
  return tool.dateFormat(val.sampleTime, 'YYYY-MM-DD HH:mm:ss', 'YYYYMMDDHH') === state.sameDay;
}

/**
 * 复制SN到剪切板
 */
const copySnToShearPlate = () => {
  var save = function (e: any) {
    let row = currentRow.value as TaskReviewModel;
    e.clipboardData.setData('text/plain', row.deviceSn);
    //e.clipboardData.setData('text/plain',event.target.innerText); 
    e.preventDefault();//阻止默认行为
  }
  document.addEventListener('copy', save);
  document.execCommand("copy");
  document.removeEventListener('copy', save);
  ElMessage.success("SN已经复制到剪切板中,可直接粘贴使用");
}

const goodFlag=(val:string)=>{ 
  return "1278".includes(val);
}
</script>

<style lang="scss" scoped>
.button {
  margin: 10px 0;
}

/* el-divider 修改高度&虚线效果 */
.el-divider--horizontal {
  margin: 2px 0;
  background: 0 0;
  border-top: 1px dashed rgb(162, 159, 159);
}

/* 去掉input-number的上下箭头*/
::v-deep.input input::-webkit-outer-spin-button,
::v-deep.input input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}

::v-deep.input input[type="number"] {
  -moz-appearance: textfield;
}

.item {
  margin-top: 8px;
  margin-right: 25px;
}
</style>