<template>
  <div class="errorRecord">
    <el-button
      style="position: absolute; top: 40px; left: 66px"
      size="medium"
      round
      icon="el-icon-back"
      @click="handleBack()"
      >返回</el-button
    >
    <el-table
      :data="tableData"
      height="350px"
      style="width: 100%"
      :header-cell-class-name="HeaderCell"
      stripe
      border
    >
      <el-table-column label="序号" type="index" width="80" align="center">
      </el-table-column>
      <el-table-column label="路线名称" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.routeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保存时间" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <!-- 0是路线规划模式，1是多点导航模式 -->
      <el-table-column label="路线类型" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">
            {{
              scope.row.routeType === 1 ? "多点导航模式" : "路线记录模式"
            }}</span
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            style="margin-left: 50px"
            size="mini"
            type="primary"
            @click="handlePreview(scope.$index, scope.row)"
            >预览</el-button
          >
          <el-button
            size="mini "
            type="success"
            @click="handleOpen(scope.$index, scope.row)"
            >开启</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 第四步绑定自定义属性 -->
    <pagination
      :total="obj.total"
      @handleSize="handleSizeChange"
      @handleCurrent="handleCurrentChange"
    >
    </pagination>
  </div>
</template>
<script>
import apiServe from "@/utils/api";
// 第一步引入组件
import pagination from "../pagination/pagination.vue";
export default {
  // 第二步 注册组件
  components: {
    pagination,
  },
  data() {
    return {
      data: {
        name: "",
      },
      token: "",
      tableData: [],
      obj: {
        // 页码
        page: 1,
        // 每页多少条
        rows: 10,
        // 默认选中哪一页
        currentPage: 1,
        // 定义总条数
        total: null,
      },
    };
  },
  methods: {
    handleBack() {
      this.$router.push({
        name: "deviceDetails",
      });
    },
    // 表头单元格样式
    HeaderCell() {
      return "HeaderCellClass";
    },
    handleData() {
      let data = {
        pageNo: this.obj.page,
        pageSize: this.obj.rows,
      };
      apiServe.HistoryRoute("get", data).then((res) => {
        console.log(res.data);
        this.tableData = res.data.list;
        this.obj.total = Number(res.data.total);
      });
    },
    handleEdit(id, row) {
      console.log(id, row);
    },
    // 分页开始
    handleSizeChange(val) {
      // console.log(val, "接收");
      // 当我点击每页多少条的时候赋值并重新渲染页面
      this.obj.rows = val;
      this.handleData();
    },
    handleCurrentChange(val) {
      // console.log(val, "接收");
      // 当我点击页码的时候将页码赋值给变量并重新渲染页面
      this.obj.page = val;
      this.handleData();
    },
    // 分页结束

    // 预览详情
    handlePreview(id, row) {
      apiServe.HistoryRouteDetail("get", { id: row.id }).then((res) => {
        // 跳转到另一个页面并且传递数据
        this.$router.push({
          path: "/index/PointNavigation",
          query: {
            id: row.id,
            data: res.data.route,
          },
        });
      });
    },
    handleOpen(id, row) {
      console.log(row);
      apiServe
        .SelectRoutePoints("post", {
          routeId: row.id,
          shipId: 123456789,
          routeMode: 1,
        })
        .then(() => {
          // 跳转到另一个页面并且传递数据
          this.$router.push({
            path: "/index/PointNavigation",
            query: { routeId: row.id },
          });
        });
    },
    handleDelete(id, row) {
      apiServe.DeleteHistoryRoute("delete", { id: row.id }).then((res) => {
        console.log(res);
        if (res.code == 200) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.handleData();
        }
      });
    },
  },
  mounted() {
    this.handleData();
  },
  created() {},
  computed: {},
};
</script>
<style lang="scss" scoped>
.errorRecord::v-deep {
  margin-top: 100px;
  padding: 10px;
  background-color: #fff;
  box-shadow: 0 0 5px #d0d0d0;
  border-radius: 5px;

  .HeaderCellClass {
    background: #859cb7;
    color: #fff;
  }
}
</style>
