<template>
  <div class="app-container scroll-auto">
    <el-form
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <!--el-form-item label="区县公司" prop="countyOrgNm">
        <el-input
          v-model="queryParams.countyOrgNm"
          placeholder="请输入区县公司"
		  clearable
        />
      </el-form-item>
      <el-form-item label="项目负责人" prop="curator">
        <el-input
          v-model="queryParams.curator"
          placeholder="请输入项目负责人"
		  clearable
        /-->
      </el-form-item>
      <el-form-item label="车牌号" prop="vehicleNo">
        <el-input v-model="queryParams.vehicleNo" clearable placeholder="请输入车牌号" />
      </el-form-item>
      <el-form-item label="车组" prop="vehicleGroup">
        <el-input v-model="queryParams.vehicleGroup" clearable placeholder="请输入车组" />
      </el-form-item>
	  <el-form-item label="状态" prop="alarmCd">
	  <el-select
	    size="mini"
	    v-model="queryParams.alarmCd"
	    placeholder="请选择"
	    clearable
	  >
	    <el-option label="全部" value=""> </el-option>
	    <el-option
	      v-for="item in alarmCdOptions"
			:key="item.value"
			:label="item.label"
			:value="item.value"
	    >
	    </el-option>
	  </el-select>
	  </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-upload
          :action="uploadUrl"
          :headers="headers"
          :data="uploadData"
          name="file"
          :show-file-list="false"
          :before-upload="beforeUpload"
          :on-success="handleSuccess"
          :on-error="handleError"
        >
          <el-button type="primary" icon="el-icon-upload" size="mini"
            >导入行驶记录</el-button
          >
        </el-upload>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <div class="table">
      <el-table ref="renewalTable" v-loading="loading" :data="dataList">
        <<el-table-column prop="" label="序号" width="80" align="center">
          <template slot-scope="scope">
            {{
              scope.$index +
              (queryParams.pageNum - 1) * queryParams.pageSize +
              1
            }}
          </template>
        </el-table-column>
		<el-table-column
				  label="所属车组"
				  prop="vehicleGroup"
				  align="center"
				>  </el-table-column>
       
        <!--el-table-column
          label="项目负责人"
          align="center"
          prop="curator"
          :show-overflow-tooltip="true"
        /-->
        <el-table-column
          label="车牌号"
          prop="vehicleNo"
          align="center"
        >
        </el-table-column>
       
      
        <!--el-table-column
          label="设备号"
          prop="equipNo"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="设备名称"
          prop="equipNm"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column-->
		<!--el-table-column
		  label="所属县区公司"
		  align="center"
		  prop="countyOrgNm"
		  :show-overflow-tooltip="true"
		>
		</el-table-column-->
        <el-table-column
          label="最新状态"
          prop="alarmDesc"
          align="center"
        >
		<template slot-scope="scope" >
				<el-tag type="danger"  v-if="scope.row.alarmCd!='0'" effect="text">{{scope.row.alarmDesc}}</el-tag>
				<el-tag type="success" v-else>{{scope.row.alarmDesc}}</el-tag>
		</template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          align="center"
        >
        </el-table-column>
		<el-table-column
		  label="最后更新时间"
		  prop="updateTime"
		  align="center"
		>
		</el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="250"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
			<el-button
			    size="mini"
			    type="text"
			    icon="el-icon-view"
			    @click="handleAlarmHist(scope.row)"
			    >监控历史</el-button
			  >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-map-location"
              @click="handleTrack(scope.row)"
              >作业轨迹</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-document"
              @click="handleRecord(scope.row)"
              >行驶记录</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        class="page-box"
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 使用封装的组件 -->
    <work-track
      :visible.sync="trackDialogVisible"
      :vehicle-info="currentVehicle"
    />

    <driving-record
      :visible.sync="recordDialogVisible"
      :vehicle-info="currentVehicle"
    />
	<alarm-hist
	  :visible.sync="alarmHistDialogVisible"
	  :vehicle-info="currentVehicle"
	/>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import {
  pdmVehicleAlarmList,
  vehicleDrivingRecordList,
  selectWorkPlanByVchicleNo,
} from "@/api/powerdistribution/vehicle-monitoring";
import WorkTrack from "./components/WorkTrack.vue";
import DrivingRecord from "./components/DrivingRecord.vue";
import AlarmHist from "./components/AlarmHist.vue";
export default {
  components: {
    WorkTrack,
    DrivingRecord,
	AlarmHist
  },
  data() {
    return {
		// 状态选项
	alarmCdOptions: [
		  { value: "0", label: "车辆状态正常" },
		  { value: "1", label: "车辆GPS离线" },
		  { value: "2", label: "施工车辆长期未动" },
		  { value: "3", label: "有作业信息，没有车辆行驶里程" },
		  { value: "4", label: "有车辆行驶里程，没有作业信息" },
		],
      dataList: [],
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      total: 0,
      stateList: [
        {
          value: "0",
          label: "正常",
        },
        {
          value: "1",
          label: "车辆GPS离线",
        },
        {
          value: "2",
          label: "施工车辆长期未动",
        },
        {
          value: "3",
          label: "有作业信息，没有车辆行驶里程",
        },
        {
          value: "4",
          label: "有车辆行驶里程，没有作业信息",
        },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        countyOrgNm: "", // 区县公司
        curator: "", // 项目负责人
        vehicleNo: "", // 车牌号
        vehicleGroup: "", // 车组
      },
      // 上传相关配置
      uploadUrl:
        process.env.VUE_APP_BASE_API +
        "/powerdistribution/vehicleDrivingRecord/import",
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      uploadData: {
        // 如果需要额外的参数可以在这里添加
      },
      // 作业轨迹相关
      trackDialogVisible: false,
      // 行驶记录相关
      recordDialogVisible: false,
	  
	  alarmHistDialogVisible :false,
      // 当前选中的车辆信息
      currentVehicle: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      pdmVehicleAlarmList({ ...this.queryParams }).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        countyOrgNm: "",
        curator: "",
        vehicleNo: "",
        vehicleGroup: "",
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 文件上传前的处理 */
    beforeUpload(file) {
      const isExcel =
        file.type ===
          "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
        file.type === "application/vnd.ms-excel" ||
        file.type === "text/csv";
      if (!isExcel) {
        this.$message.error("只能上传CSV文件!");
        return false;
      }
      const isLt5M = file.size / 1024 / 1024 < 5;
      if (!isLt5M) {
        this.$message.error("文件大小不能超过 5MB!");
        return false;
      }
      return true;
    },
    /** 文件上传成功的处理 */
    handleSuccess(response, file) {
      // if (response.code === 200) {
      this.$modal.msgSuccess("导入成功");
      this.getList();
      // } else {
      //   this.$modal.msgError(response.msg || "导入失败");
      // }
    },
    /** 文件上传失败的处理 */
    handleError(error) {
      this.$modal.msgError("导入失败，请重试");
      console.error("上传失败:", error);
    },
    /** 查看作业轨迹 */
    handleTrack(row) {
      this.currentVehicle = row;
      this.trackDialogVisible = true;
    },
    /** 查看行驶记录 */
    handleRecord(row) {
      this.currentVehicle = row;
      this.recordDialogVisible = true;
    },
	/** 查看监控记录 */
	handleAlarmHist(row) {
	  this.currentVehicle = row;
	  this.alarmHistDialogVisible = true;
	},
  },
};
</script>
<style lang="scss" scoped>
.table {
  height: calc(100% - 150px);
}
::v-deep {
  .el-table {
    height: 100%;
    width: 100%;
    .el-table__body-wrapper {
      width: 100% !important;
      height: calc(100% - 50px) !important; // 表格高度减去表头的高度
      overflow-y: scroll;
    }
  }
  .el-form--inline .el-form-item {
    margin-right: 10px;
    margin-bottom: 10px;
  }
  // 上传按钮样式调整
  .el-upload {
    display: inline-block;
  }
  .el-button--text {
    padding: 0 8px;
  }
}

.track-container {
  min-height: 400px;
  // 添加地图容器的样式
}

.record-container {
  min-height: 400px;
  padding: 20px;

  .mb20 {
    margin-bottom: 20px;
  }

  ::v-deep {
    .el-table {
      margin-bottom: 20px;
    }
  }
}
</style>
