<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <div class="main-right-box">
          <div class="main-search-box">
            <el-form
              :model="queryParams"
              ref="queryForm"
              size="small"
              :inline="true"
              v-show="showSearch"
            >
              <el-form-item label="组织机构">
                <org-tree
                  v-model="queryParams.orgId"
                  @defaultKey="getDefaultKey"
                  @defaultOrg="getDefaultOrg"
                  @checkChange="checkChange"
                  @click="clickTreeNode"
                  ref="orgTree"
                ></org-tree>
              </el-form-item>
              <el-form-item label="设备名称" prop="deviceName">
                <el-input v-model="queryParams.deviceName"
                          clearable
                          maxlength="50"
                          placeholder="请输入设备名称"
                          style="width: 100%"
                />
              </el-form-item>
              <el-form-item label="告警分类" prop="dataType">
                <el-select
                  v-model="queryParams.dataType"
                  clearable
                  label="告警分类"
                  placeholder="请选择告警分类"
                  prop="dataType"
                >
                  <el-option
                    v-for="item in alarmTypes"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="告警类型" prop="deviceType">
                <el-select
                  v-model="queryParams.deviceType"
                  clearable
                  label="设备类型"
                  placeholder="请选择告警类型"
                  prop="deviceType"
                >
                  <el-option
                    v-for="item in deviceTypes"
                    v-if="!queryParams.dataType || item.type.indexOf(queryParams.dataType) > -1"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="处置状态" prop="isDoStr">
                <el-select
                  v-model="isDoStr"
                  clearable
                  prop="isDoStr"
                  @change="isDoChanged"
                >
                  <el-option
                    v-for="item in dict.type.alarm_deal_status"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="告警时间" prop="alarmTime">
                <DataRangePicker
                  type="daterange"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :default-time="['00:00:00', '23:59:59']"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  v-model="queryParams.alarmTime"
                >
                </DataRangePicker>
              </el-form-item>
              <el-form-item label="恢复时间" prop="alarmEndTime">
                <DataRangePicker
                  type="daterange"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :default-time="['00:00:00', '23:59:59']"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  v-model="queryParams.alarmEndTime"
                >
                </DataRangePicker>
              </el-form-item>
              <el-form-item label="处置时间" prop="alarmDealTime">
                <DataRangePicker
                  type="daterange"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :default-time="['00:00:00', '23:59:59']"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  v-model="queryParams.alarmDealTime"
                >
                </DataRangePicker>
              </el-form-item>

            </el-form>

            <el-row :gutter="10">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                  >搜索
                </el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  icon="el-icon-refresh"
                  size="mini"
                  @click="resetQuery"
                  >重置
                </el-button>
              </el-col>

<!--              <el-col :span="1.5">
                <el-button
                  type="primary"
                  icon="el-icon-download"
                  size="mini"
                  @click="handleExport"
                  v-hasPermi="['core:task:export']"
                  >导出</el-button
                >
              </el-col>-->

              <right-toolbar
                :showSearch.sync="showSearch"
                @queryTable="getList"
              ></right-toolbar>
            </el-row>
          </div>

          <el-table
            v-loading="loading"
            :data="alarmList"
            border
            height="600"
            size="small"
          >
            <el-table-column
              label="序号"
              type="index"
              align="center"
              width="100"
            >
              <template slot-scope="scope">
                <span>{{
                  (queryParams.pageNum - 1) * queryParams.pageSize +
                  scope.$index +
                  1
                }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="地区"
              align="center"
              prop="city"
              width="100"
            />
            <el-table-column
              label="行社"
              align="center"
              prop="bank"
              width="130"
            />
            <el-table-column
              label="组织机构"
              align="center"
              prop="orgName"
              width="150"
            />
            <el-table-column
              label="设备名称"
              align="center"
              prop="deviceName"
              width="135"
              :show-overflow-tooltip="showOverflowTooltip"
            />
            <el-table-column
              label="告警类型"
              align="center"
              prop="sourceTypeDes"
              width="135"
            />
            <el-table-column label="告警时间" align="center" width="180" prop="time">
            </el-table-column>
            <el-table-column label="恢复时间" align="center" width="180" prop="endTime">
            </el-table-column>
            <el-table-column
              label="处置时间"
              align="center"
              prop="doTime"
              width="180"
            />
            <el-table-column
              label="告警内容"
              align="center"
              prop="content"
            />

            <!--<el-table-column
              label="处理结果"
              align="center"
              prop="reslut"
              width="160"
            />-->
            <el-table-column
              label="操作"
              fixed="right"
              align="center"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit-outline"
                  v-if="!scope.row.doTime"
                  @click="dealAlarm(scope.row)"
                  v-hasPermi="['core:task:query']"
                  >处置报警</el-button>
                  <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-view"
                  @click="showDetailInfo(scope.row)"
                  v-hasPermi="['core:task:query']"
                  >详情</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"
          />
        </div>
      </el-col>
    </el-row>
    <dialog-info ref="infoDialog"></dialog-info>
    <dialog-deal-alarm ref="dealAlarmDialog" @success="getList()"></dialog-deal-alarm>
  </div>
</template>

<script>
import OrgTree from "@/components/orgTree/orgQuerySelector.vue";
import DataRangePicker from "@/components/dateTime/daterange.picker.vue";
import dayjs from "dayjs";
import DialogInfo from "./dialog.info";
import DialogDealAlarm from "./dialog.dealAlarm.vue";
import {mapGetters} from "vuex";
import {alarmList,deviceTypeList} from "@/api/iot/alarmRule";


export default {
  components: { OrgTree,DataRangePicker,DialogInfo,DialogDealAlarm },
  name: "AlarmData",
  dicts: ["sys_access_cycle",'sys_yes_no','alarm_deal_status'],
  data() {
    return {
      isDoStr:null,
      alarmTypes:[
        {
          value: '0',
          label: '动环主机告警'
        },
        {
          value: '1',
          label: '监控主机告警'
        },
         {
           value: '2',
           label: '报警主机告警'
         }
      ],
      deviceTypes: [],
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个停用
      single: true,
      // 非多个停用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监控调阅任务表格数据
      alarmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      showOverflowTooltip: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        cycle: null,
        dateRange: [],
        alarmTime:null,
        alarmEndTime:null,
        planStartTime: null,
        alarmDealTime:null,
        isDo:null,
        status: null,
        roleId: null,
        checkSub: true,
      },
      selectedOrgName: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      orgName: "",
    };
  },
  created() {
    this.queryParams.orgId = this.$store.getters.orgId;
    this.initDeviceTypeList();
  },
  computed: {
    ...mapGetters(["orgId", "orgName"]),
  },
  methods: {
    isDoChanged()
    {
      if(this.isDoStr)
      {
        this.queryParams.isDo=this.isDoStr=='1'? 1:0;
      }
      else{
        this.queryParams.isDo=null;
      }
    },
    initDeviceTypeList(){
          deviceTypeList("")
            .then((r) => {
              // 使用 map 而不是 filter 进行数据转换
              const transformedList = r.map((item) => ({
                value: item.value,
                label: item.label,
                type: item.type,
              }));

              // 将转换后的数据推送到 this.deviceTypeList
              this.deviceTypes.push(...transformedList);
            })
            .catch((error) => {
              // 处理错误
              console.error('Error fetching device type list:', error);
            });
    },
    /** 查询监控调阅任务列表 */
    getList() {
      alarmList(this.queryParams).then((response) => {
        this.alarmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    getDefaultOrg(org) {
      this.orgName = org.name;
      this.selectedOrgName = org.shortName;
    },

    getDefaultKey(key) {
      this.queryParams.orgId = key;
      this.getList();
    },
    checkChange(state) {
      this.queryParams.checkSub = state;
      this.handleQuery();
    },
    // 节点单击事件
    clickTreeNode(data) {
      this.queryParams.orgId = data.id;
      this.orgName = data.name;
      this.selectedOrgName = data.shortName;
      this.handleQuery();
    },
    /** 下穿状态改变*/
    changeCheckBox() {
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        planId: null,
        taskName: null,
        orgId: null,
        orgPath: null,
        orgName: null,
        ymdDate: null,
        ymdYear: null,
        ymdHalfyear: null,
        ymdQuarter: null,
        ymdMonth: null,
        ymdWeek: null,
        ymdDay: null,
        cycle: null,
        planStartTime: null,
        planEndTime: null,
        startTime: null,
        endTime: null,
        status: null,
        roleId: null,
        retrievalUser: null,
        orderNum: null,
        batchNum: null,
        submitTime: null,
        exceptionCount: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      //this.queryParams.dateRange = [];
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.orgId = this.orgId;
      this.selectedOrgName = this.orgName;
      this.queryParams.checkSub = true;
      this.queryParams.dateRange = ["", ""];
      this.queryParams.planStartTime = null;
      this.$refs["orgTree"].setCheckSub(this.queryParams.checkSub);
      this.isDoStr=null;
      this.queryParams.isDo=null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 处置告警按钮操作 */
    dealAlarm(row) {
      this.$refs["dealAlarmDialog"].show(row.id, {});
    },
    /** 详情操作 */
    showDetailInfo(row) {
      this.$refs["infoDialog"].show(row.id, {});
    },

    /** 导出按钮操作 */
    handleExport() {
      if (this.total==null || this.total===0){
        this.$modal.alert("暂无可用数据导出");
        return;
      }
      if (this.total>50000){
        this.$modal.alert("导出数据超过5万条，请缩小查询范围后重试");
        return;
      }
      this.download(
        "core/retrievalTask/export",
        {
          ...this.queryParams,
        },
        `${
          this.selectedOrgName
        }-${this.$tab.getCurrentTabName()}-${dayjs().format("YYYYMMDD")}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped></style>
