<!--
 * @Author: SunTao 328867980@qq.com
 * @Date: 2024-10-12 14:52:23
 * @LastEditors: SunTao 328867980@qq.com
 * @LastEditTime: 2024-11-05 09:23:30
 * @FilePath: \znxjxt-ui\src\views\xj\inspection\traffic-management\index.vue
 * @Description: 巡检管理-交安事件
-->

<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="道路名称" prop="segmentName">
        <el-select
          v-model="queryParams.segmentName"
          placeholder="请选择道路名称"
          clearable
        >
          <el-option
            v-for="item in segmentList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="采集时间" prop="dateTime">
        <el-date-picker
          v-model="dateTime"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          clearable
        />
      </el-form-item>
      <el-form-item label="事件类型" prop="defectType">
        <el-select
          v-model="queryParams.defectType"
          placeholder="请选择事件类型"
          clearable
        >
          <el-option
            v-for="item in eventType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="校验状态" prop="dataStatus">
        <el-select
          v-model="queryParams.dataStatus"
          placeholder="请选择校验状态"
          clearable
        >
          <el-option
            v-for="item in verifyStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上下行" prop="inspectDirection">
        <el-select
          v-model="queryParams.inspectDirection"
          placeholder="请选择"
          clearable
        >
          <el-option label="上行" value="0" />
          <el-option label="下行" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="公里桩" prop="stakeRange">
        <el-input
          v-model="queryParams.stakeStart"
          placeholder="起始公里桩"
          style="width: 10rem"
          clearable
        />
        <span style="margin: 0 5px">-</span>
        <el-input
          v-model="queryParams.stakeEnd"
          placeholder="终止公里桩"
          style="width: 10rem"
          clearable
        />
        <el-tooltip
          class="item"
          effect="dark"
          content="格式为K0000+000"
          placement="top"
        >
          <i class="el-icon-info"></i>
        </el-tooltip>
      </el-form-item>
      <el-form-item label="巡检任务" prop="defectType3">
        <el-select
          v-model="queryParams.defectType3"
          placeholder="请选择巡检任务"
          clearable
        >
          <el-option
            v-for="item in tableDefect"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="事件状态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder="请选择事件状态"
          clearable
        >
          <el-option
            v-for="item in defectStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="事件id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请填写事件id"
          clearable=""
        ></el-input>
      </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="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除
        </el-button>
      </el-col>
      <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            >导出
          </el-button>
        </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit-outline"
          size="mini"
          :disabled="single"
          @click="handleUpdateBatch"
          >批量确认
        </el-button>
      </el-col> -->
    </el-row>
    <!-- 列表 -->
    <el-table
      ref="roadTable"
      v-loading="loading"
      :data="roadList"
      @selection-change="handleSelectionChange"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="道路名称" align="center" prop="segmentName" />
      <el-table-column label="采集时间" align="center" prop="createdTime">
        <template slot-scope="scope">
          <span>{{ new Date(scope.row.createdTime).toLocaleString() }}</span>
        </template>
      </el-table-column>
      <el-table-column label="事件类型" align="center" prop="defectType">
        <template slot-scope="scope">
          <span>{{ filterDefect(scope.row.defectType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="事件状态" align="center" prop="state">
        <template slot-scope="scope">
          <span>{{ filterState(scope.row.state) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始桩号" align="center" prop="stakeStart" />
      <el-table-column label="结束桩号" align="center" prop="stakeEnd" />
      <el-table-column label="事件图片" align="center" prop="media">
        <template slot-scope="scope">
          <img
            @click="showScreenImg(scope.row)"
            height="50"
            :src="scope.row.mediaUrl"
          />
        </template>
      </el-table-column>
      <el-table-column label="巡检任务" align="center" prop="stakeEnd" />
      <el-table-column label="道路方向" align="center" prop="inspectDirection">
        <template slot-scope="scope">
          <span>{{
            scope.row.inspectDirection === "0" ? "上行" : "下行"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="事件id" align="center" prop="id" />

      <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="viewDefect(scope.row)"
            >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <div class="pagination-part">
      <el-pagination
        background
        :current-page.sync="pagination.page"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 40]"
        :page-size.sync="pagination.size"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableTotal"
      >
      </el-pagination>
    </div>
    <!-- 新增/编辑弹窗 -->
    <el-dialog
      :title="addTitle"
      :visible.sync="addVisible"
      width="60rem"
      append-to-body
      :close-on-click-modal="false"
      destroy-on-close
      @close="addCancel"
    >
      <event-add
        @cancel="addCancel"
        :dialogItem="dialogItem"
        :segmentList="segmentList"
        :eventType="eventType"
        :roadType="roadType"
        :tableDefect="tableDefect"
        :defectStatus="defectStatus"
      ></event-add>
    </el-dialog>
    <!-- 查看弹窗 -->
    <el-dialog
      title="查看事件"
      :visible.sync="viewVisible"
      width="55rem"
      append-to-body
      :close-on-click-modal="false"
      destroy-on-close
      @close="viewCancel"
    >
      <event-view
        @cancel="viewCancel"
        :dialogItem="dialogItem"
        :eventType="eventType"
      ></event-view>
    </el-dialog>

    <!-- 查看图片大图 -->
    <el-dialog
      title="查看图片"
      :visible.sync="showImageDialog"
      width="75rem"
      append-to-body
      destroy-on-close
      @close="imgCancel"
    >
      <div class="image-container" ref="imageContainer">
        <img
          :src="currentImageItem.mediaUrl"
          alt="Main Image"
          ref="mainImage"
          @load="updateRects"
        />
        <div
          v-for="(rect, index) in rects"
          :key="index"
          class="rect-overlay"
          :style="getRectStyle(rect)"
        ></div>
      </div>
    </el-dialog>
  </div>
</template>
    
    <script>
import {
  getEventTable,
  getSegment,
  getEventType,
  getDefectStatus,
  deleteType,
  changeEvent,
  getRoadType,
} from "@/api/xj/traffic";
import EventAdd from "./components/event-add.vue";
import EventView from "./components/event-view.vue";

export default {
  components: { EventAdd, EventView },
  data() {
    return {
      // 查询表单
      queryParams: {
        // 道路名称
        segmentName: "",
        // 事件类型
        defectType: "",
        // 校验状态
        dataStatus: "",
        // 巡检任务
        defectType3: "",
        // 公里桩
        stakeStart: "",
        stakeEnd: "",
        // 上下行
        inspectDirection: "",
        // 事件状态
        state: "",
        // 事件id
        id: "",
      },
      // 采集时间
      dateTime: [],
      // 传查询接口表单
      searchForm: {},
      // 道路名称下拉
      segmentList: [],
      // 事件类型下拉
      eventType: [],
      // 路产类型下拉
      roadType: [],
      // 事件状态下拉
      defectStatus: [],
      // 校验状态下拉
      verifyStatus: [
        { label: "未校验", value: "1" },
        { label: "已校验", value: "2" },
      ],
      // 列表数据绑定
      roadList: [],
      tableDefect: [],
      // 列表加载状态
      loading: false,
      // 列表已选数组
      checkIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 分页-列表总数
      tableTotal: 0,
      // 分页-页数页码
      pagination: {
        page: 1,
        size: 10,
      },
      // 传弹窗的数据
      dialogItem: {},
      // 弹窗标题
      addTitle: "添加事件",
      // 新增/编辑弹窗显隐控制
      addVisible: false,
      // 查看弹窗显隐控制
      viewVisible: false,
      // 查看图片大图弹窗显隐控制
      showImageDialog: false,
      // 查看大图片路径
      currentImageItem: "",
      // 图片病害位置信息
      rects: [],
    };
  },
  created() {
    this.getRoadList();
    this.getSegmentList();
    this.getEventTypeList();
    this.getDefectStatusList();
    this.getRoadTypeList();
  },
  methods: {
    /* 获取道路名称下拉数据 */
    getSegmentList() {
      getSegment().then(({ code, data }) => {
        if (code === 200) {
          this.segmentList = data;
        }
      });
    },

    /* 获取事件类型下拉数据 */
    getEventTypeList() {
      getEventType().then(({ code, data }) => {
        if (code === 200) {
          this.eventType = data;
        }
      });
    },

    /* 事件状态下拉数据 */
    getDefectStatusList() {
      getDefectStatus().then(({ data, code }) => {
        if (code === 200) {
          this.defectStatus = data;
        }
      });
    },

    /* 获取路产类型下拉数据 */
    getRoadTypeList() {
      getRoadType().then(({ code, data }) => {
        if (code === 200) {
          this.roadType = data;
        }
      });
    },

    /* 点击搜索事件 */
    handleQuery() {
      const phonereg = /^K\d{4}\+\d{3}$/;
      if (this.queryParams.stakeStart) {
        if (phonereg.test(this.queryParams.stakeStart)) {
          this.pagination.page = 1;
          this.searchForm = JSON.parse(JSON.stringify(this.queryParams));
          this.getRoadList();
        } else {
          this.$modal.msgWarning("请按照格式填写公里桩进行修改");
        }
      } else if (this.queryParams.stakeEnd) {
        if (phonereg.test(this.queryParams.stakeEnd)) {
          this.pagination.page = 1;
          this.searchForm = JSON.parse(JSON.stringify(this.queryParams));
          this.getRoadList();
        } else {
          this.$modal.msgWarning("请按照格式填写公里桩进行修改");
        }
      } else {
        this.pagination.page = 1;
        this.searchForm = JSON.parse(JSON.stringify(this.queryParams));
        this.getRoadList();
      }
    },

    /* 点击重置事件 */
    resetQuery() {
      this.searchForm = {};
      this.queryParams = {
        segmentName: "",
        defectType: "",
        dataStatus: "",
        inspectDirection: "",
        defectType3: "",
        stakeStart: "",
        stakeEnd: "",
        state: "",
        id: "",
      };
      this.dateTime = [];
      this.getRoadList();
    },

    /* 获取列表数据 */
    getRoadList() {
      const params = {
        ...this.pagination,
        ...this.searchForm,
        startTime: this.dateTime ? this.dateTime[0] : "",
        endTime: this.dateTime ? this.dateTime[1] : "",
      };
      getEventTable(params).then(({ code, rows, total }) => {
        if (code === 200) {
          this.roadList = rows;
          this.tableTotal = total;
        }
      });
    },

    /* 过滤列表事件类型 */
    filterDefect(value) {
      let a = null;
      [a] = this.eventType.filter((item) => {
        return item.value === value;
      });
      if (a) {
        return a.label;
      }
    },

    /* 过滤事件状态 */
    filterState(value) {
      let a = null;
      [a] = this.defectStatus.filter((item) => {
        return item.value === value;
      });
      if (a) {
        return a.label;
      }
    },

    /* 点击新增事件 */
    handleAdd() {
      this.addVisible = true;
      this.addTitle = "添加事件";
      this.dialogItem = {
        title: "添加事件",
      };
    },

    /* 点击删除事件 */
    handleDelete(row) {
      const checkIds = row ? [row.id] : this.checkIds;
      if (!checkIds.length) {
        this.$modal.msgWarning("请选择要删除的记录");
        return;
      }
      this.$modal
        .confirm(`是否确认删除选中的${checkIds.length}条记录？`)
        .then(() => {
          return deleteType(checkIds);
        })
        .then(() => {
          this.getRoadList();
          this.$refs.defectTable.clearSelection();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    /* 点击导出事件 */
    handleExport() {},

    /* 批量确认事件 */
    handleUpdateBatch() {
      if (this.checkIds.length > 0) {
        changeEvent(this.checkIds).then(({ code, data }) => {
          if (code === 200) {
            this.$modal.msgSuccess(`修改${data}`);
            this.$refs.defectTable.clearSelection();
            this.checkIds = [];
          }
        });
      } else {
        this.$modal.msgWarning("请选择一条记录进行修改");
      }
    },

    /* 列表选择改变事件 */
    handleSelectionChange(selection) {
      this.checkIds = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /* 列表-点击修改事件 */
    handleUpdate(item) {
      this.addVisible = true;
      this.addTitle = "修改事件";
      this.dialogItem = {
        title: "修改事件",
        ...item,
      };
    },

    /* 列表-点击查看事件 */
    viewDefect(item) {
      this.viewVisible = true;
      this.dialogItem = {
        title: "查看事件",
        ...item,
      };
    },

    /* 切换分页 */
    handleCurrentChange(arg) {
      this.pagination.page = arg;
      this.getRoadList();
    },

    /* 切换每条/页 */
    handleSizeChange(arg) {
      this.pagination.size = arg;
      this.getRoadList();
    },

    /* 关闭新增/编辑弹窗 */
    addCancel() {
      this.addVisible = false;
      this.dialogItem = { title: "添加事件" };
      this.addTitle = "";
      this.getRoadList();
    },

    /* 关闭查看弹窗 */
    viewCancel() {
      this.viewVisible = false;
      this.dialogItem = { title: "添加事件" };
    },

    /* 打开查看图片弹窗 */
    showScreenImg(item) {
      this.currentImageItem = item;
      this.showImageDialog = true;
    },

    /* 图片位置信息获取 */
    updateRects() {
      this.rects = [];
      this.rectsItem = {};
      const image = this.$refs.mainImage;
      const rects = this.currentImageItem?.rect?.split(",").map(Number) || [];
      this.rects = [
        {
          left: rects[0],
          top: rects[1],
          width: rects[2],
          height: rects[3],
        },
      ];
      // this.rectsItem = this.defectData[this.currentIndex];
    },

    /* 图片红框位置 */
    getRectStyle({ left, top, width, height }) {
      const image = this.$refs.mainImage;
      const container = this.$refs.imageContainer;
      if (!image || !container) return {};

      const scaleX = container.clientWidth / image.naturalWidth;
      const scaleY = container.clientHeight / image.naturalHeight;
      const scale = Math.min(scaleX, scaleY);

      const renderedWidth = image.naturalWidth * scale;
      const renderedHeight = image.naturalHeight * scale;

      const offsetX = (container.clientWidth - renderedWidth) / 2;
      const offsetY = (container.clientHeight - renderedHeight) / 2;

      return {
        position: "absolute",
        left: `${left * scale + offsetX}px`,
        top: `${top * scale + offsetY}px`,
        width: `${width * scale}px`,
        height: `${height * scale}px`,
        border: "2px solid #FF0000",
        boxSizing: "border-box",
      };
    },

    /* 关闭查看图片弹窗 */
    imgCancel() {
      this.showImageDialog = false;
      this.currentImageItem = {};
      this.rects = [];
    },
  },
};
</script>
    
<style lang="scss" scoped>
.app-container {
  padding: 20px;
}

.mb8 {
  margin-bottom: 8px;
}

.small-padding {
  padding: 0 5px;
}

.fixed-width {
  width: 120px;
}

/* 分页样式 */
.pagination-part {
  width: 100%;
  display: flex;
  padding-top: 1rem;
  justify-content: flex-end;
}

/* 新增弹窗/编辑弹窗 */
.addForm {
  .el-select,
  .el-cascader {
    width: 100%;
  }
}
/* 新增地图框样式 */
.no-hand-cursor {
  cursor: default !important;
  width: 100%;
  height: 20rem;
}

/* 上传文件列表样式 */
::v-deep .el-upload-list {
  img {
    width: auto;
  }
}

/* 查看弹窗-图片框样式 */
.view-carousel {
  height: 15rem;
  // .el-carousel__item{
  //   height: 12rem;

  img {
    max-height: 15rem;
    max-width: 25rem;
  }
}

/* 查看大图弹窗 */
.image-container {
  position: relative;
  width: 100%;
  height: 80%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.image-container img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.rect-overlay {
  position: absolute;
  pointer-events: none;
  border: 2px solid red;
}
</style>
  