<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="SN" prop="sn">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入SN"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择车辆状态" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.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 @change="handleQuery">
          <el-option
            v-for="dict in dict.type.device_lock_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="电门状态" prop="quality">
        <el-select v-model="queryParams.quality" placeholder="请选择电门状态" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.device_quality"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="电动车状态" prop="iotStatus" label-width="6em">
        <el-select v-model="queryParams.iotStatus" placeholder="请选择电动车状态" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.device_iot_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="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-has-permi="['bst:locationLog:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-has-permi="['bst:locationLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="locationLogList" @selection-change="handleSelectionChange" :default-sort="defaultSort"  @sort-change="onSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="column of showColumns">
        <el-table-column
            :key="column.key"
            :label="column.label"
            :prop="column.key"
            :align="column.align"
            :min-width="column.minWidth"
            :sort-orders="orderSorts"
            :sortable="column.sortable"
            :show-overflow-tooltip="column.overflow"
            :width="column.width"
        >
          <template slot-scope="d">
            <template v-if="column.key === 'id'">
              {{d.row[column.key]}}
            </template>
            <template v-else-if="column.key === 'status'">
              <dict-tag :options="dict.type.device_status" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'lockStatus'">
              <dict-tag :options="dict.type.device_lock_status" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'quality'">
              <dict-tag :options="dict.type.device_quality" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'iotStatus'">
              <dict-tag :options="dict.type.device_iot_status" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'voltage'">
              {{d.row[column.key] | fix2 | dv}} V
            </template>
            <template v-else-if="column.key === 'location'">
              <el-link type="primary" size="mini" @click="handleLocation(d.row)">
                {{d.row.longitude}}<br/>{{d.row.latitude}}
              </el-link>
            </template>
            <template v-else>
              {{d.row[column.key]}}
            </template>
          </template>
        </el-table-column>
      </template>
      <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)"
            v-has-permi="['bst:locationLog:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-has-permi="['bst:locationLog:remove']"
          >删除</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"
    />

    <!-- 添加或修改定位日志对话框 -->
    <location-log-view-dialog
      :visible.sync="open"
      :id="selectedId"
      @success="getList"
    />
  </div>
</template>

<script>
import { listLocationLog, delLocationLog} from "@/api/bst/locationLog";
import { $showColumns } from '@/utils/mixins';
import FormCol from "@/components/FormCol/index.vue";
import LocationLogViewDialog from '@/views/bst/locationLog/components/LocationLogViewDialog.vue';

  // 默认排序字段
const defaultSort = {
  prop: "at",
  order: "descending"
}

export default {
  name: "LocationLog",
  mixins: [$showColumns],
  dicts: ['device_status', 'device_lock_status', 'device_iot_status', 'device_quality'],
  components: {FormCol, LocationLogViewDialog},
  data() {
    return {
      span: 24,
      // 字段列表
      columns: [
        {key: 'id', visible: false, label: 'ID', minWidth: null, sortable: true, overflow: false, align: 'center', width: "80"},
        {key: 'mac', visible: true, label: 'MAC', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'sn', visible: true, label: 'SN', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'location', visible: true, label: '定位', minWidth: null, sortable: false, overflow: false, align: 'center', width: null},
        {key: 'at', visible: true, label: '消息时间', minWidth: null, sortable: true, overflow: false, align: 'center', width: "100"},
        {key: 'status', visible: true, label: '状态', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'iotStatus', visible: true, label: '电动车', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'lockStatus', visible: true, label: '锁', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'quality', visible: true, label: '电门', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'voltage', visible: true, label: '电压', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'signal', visible: true, label: '信号强度', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'satellites', visible: true, label: '卫星数量', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 定位日志表格数据
      locationLogList: [],
      // 是否显示弹出层
      open: false,
      // 选中的ID
      selectedId: null,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        id: null,
        mac: null,
        sn: null,
        status: null,
        lockStatus: null,
        quality: null,
        iotStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mac: [
          { required: true, message: "mac不能为空", trigger: "blur" }
        ],
        at: [
          { required: true, message: "onenet消息时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleLocation(row) {
      window.open(`https://ditu.amap.com/regeo?lng=${row.longitude}&lat=${row.latitude}`, '_blank');
    },
    /** 当排序按钮被点击时触发 **/
    onSortChange(column) {
      if (column.order == null) {
        this.queryParams.orderByColumn = defaultSort.prop;
        this.queryParams.isAsc = defaultSort.order;
      } else {
        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;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.selectedId = null;
      this.open = true;
    },
    handleView(row) {
      this.selectedId = row.id || this.ids;
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.selectedId = row.id || this.ids;
      this.open = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除定位日志编号为"' + ids + '"的数据项？').then(function() {
        return delLocationLog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bst/locationLog/export', {
        ...this.queryParams
      }, `locationLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
