<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="mac" prop="mac">
        <el-input
          v-model="queryParams.mac"
          placeholder="请输入mac"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="车辆状态" prop="status">-->
<!--        <el-select v-model="queryParams.status" placeholder="请选择车辆状态" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.rl_device_status"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="锁状态" prop="lockStatus">-->
<!--        <el-select v-model="queryParams.lockStatus" placeholder="请选择锁状态" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.rl_device_lock_status"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </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-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:locationLog:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="locationLogList"  :default-sort="defaultSort" @sort-change="handleSortChange" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="定位id" align="center" prop="locationId" />
      <el-table-column label="mac" align="center" prop="mac" />
<!--      <el-table-column label="onenet接收到的消息" align="center" prop="onenetMsg" />-->
<!--      <el-table-column label="经度" align="center" prop="longitude" />-->
<!--      <el-table-column label="纬度" align="center" prop="latitude" />-->
      <el-table-column label="位置" align="center">
        <template slot-scope="scope">
          <a
            :href="`https://uri.amap.com/marker?position=${scope.row.longitude},${scope.row.latitude}`"
            target="_blank"
            class="link-type">
            {{ scope.row.longitude }}, {{ scope.row.latitude }}
          </a>
        </template>
      </el-table-column>

      <el-table-column label="onenet消息时间" align="center" prop="at" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.at, '{y}-{m}-{d} {h}:{i}:{s}' ) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}' ) }}</span>
        </template>
      </el-table-column>

      <!-- 新增列：电动车状态、电池电压、信号强度、卫星数量、钥匙 -->
      <el-table-column label="电动车状态" align="center" prop="status2">
        <template slot-scope="scope">
          <span>{{ formatStatus(scope.row.status2) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="电池电压" align="center" prop="bat">
        <template slot-scope="scope">
          <span>{{ formatVoltage(scope.row.bat) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="信号强度" align="center" prop="csq">
        <template slot-scope="scope">
          <span>{{ scope.row.csq }}</span>
        </template>
      </el-table-column>

      <el-table-column label="卫星数量" align="center" prop="s">
        <template slot-scope="scope">
          <span>{{ scope.row.s }}</span>
        </template>
      </el-table-column>

      <el-table-column label="钥匙状态" align="center" prop="q">
        <template slot-scope="scope">
          <span>{{ scope.row.q }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="车辆状态" align="center" prop="status" >-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag-->
<!--            :options="dict.type.rl_device_status"-->
<!--            :value="scope.row.status"-->
<!--          />-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="锁状态" align="center" prop="lockStatus" >-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag-->
<!--            :options="dict.type.rl_device_lock_status"-->
<!--            :value="scope.row.lockStatus"-->
<!--          />-->
<!--        </template>-->
<!--      </el-table-column>-->
      <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-view"
            @click="handleView(scope.row,scope.index)"
            v-hasPermi="['system:locationLog: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"
    />

    <!-- 添加或修改定位日志对话框 -->
    <el-dialog title="命令日志详细" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-col :span="12">
          <el-form-item label="MAC">{{ form.mac }}</el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="SN">{{ form.sn }}</el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="经度">{{ form.longitude }}</el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="纬度">{{ form.latitude }}</el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="消息">{{ form.onenetMsg }}</el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="消息时间">{{ form.at }}</el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="创建时间">{{ form.createTime }}</el-form-item>
        </el-col>
        <!-- 新增字段展示 -->
        <el-col :span="12">
          <el-form-item label="电动车状态">{{ formatStatus(form.status2) }}</el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电池电压">{{ formatVoltage(form.bat) }}</el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="信号强度">{{ form.csq }}</el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="卫星数量">{{ form.s }}</el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="钥匙状态">{{ form.q }}</el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆状态">
            <dict-tag :options="dict.type.rl_device_status" :value="form.status"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="锁状态">
            <dict-tag :options="dict.type.rl_device_lock_status" :value="form.lockStatus"/>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLocationLog, getLocationLog, delLocationLog, addLocationLog, updateLocationLog } from "@/api/system/locationLog";

export default {
  name: "LocationLog",
  dicts: ["rl_device_lock_status","rl_device_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 定位日志表格数据
      locationLogList: [],
      defaultSort: {prop: 'createTime', order: 'descending'},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        mac: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    const mac = this.$route.params && this.$route.params.mac;
    if (mac != null) {
      this.queryParams.mac = mac;
    }
    this.getList();
  },
  methods: {
    // 格式化电动车状态
    formatStatus(status) {
      const statusMap = {
        0: '断电',
        1: '上电运行',
        2: '轮动抱死',
        3: '超出区域断电'
      };
      return statusMap[status] || '未知状态';
    },
    // 格式化电池电压为 '50.1V' 的样式
    formatVoltage(bat) {
      if (bat === null || bat === undefined) return '0.0V';
      return (bat / 10).toFixed(1) + 'V';
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.open = true;
      this.form = row;
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 查询定位日志列表 */
    getList() {
      this.loading = true;
      listLocationLog(this.queryParams).then(response => {
        this.locationLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        locationId: null,
        mac: null,
        onenetMsg: null,
        longitude: null,
        latitude: null,
        createTime: null,
        at: null,
        status: null,
        lockStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.locationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加定位日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const locationId = row.locationId || this.ids
      getLocationLog(locationId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改定位日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.locationId != null) {
            updateLocationLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLocationLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const locationIds = row.locationId || this.ids;
      this.$modal.confirm('是否确认删除定位日志编号为"' + locationIds + '"的数据项？').then(function() {
        return delLocationLog(locationIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/locationLog/export', {
        ...this.queryParams
      }, `locationLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
