<template>
  <div class="RepairRecord">
    <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">
            <span v-if="scope.row.faultType === 0">结构故障</span>
            <span v-else-if="scope.row.faultType === 1">动力故障</span>
            <span v-else-if="scope.row.faultType === 2">船体故障</span>
            <span v-else>其他问题</span></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>
      <el-table-column label="设备ID" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.shipId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户ID" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.createBy }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.contactWay }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handlePreview(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: {
    // 表头单元格样式
    HeaderCell() {
      return "HeaderCellClass";
    },
    handleData() {
      let data = {
        pageNo: this.obj.page,
        pageSize: this.obj.rows,
      };
      apiServe.GetWarrantRepair("get", data).then((res) => {
        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) {
      console.log(id, row);
      apiServe
        .UpdateWarrantRepair("put", {
          id: row.id,
          status: row.status === 1 ? 0 : 1,
        })
        .then((res) => {
          console.log(res);
          this.handleData();
          this.$message({
            showClose: true,
            message: "已成功处理",
            type: "success",
          });
        });
    },
    handleDelete(id, row) {
      apiServe.DeleteWarrantRepair("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>
.RepairRecord::v-deep {
  margin-top: 50px;
  padding: 10px;
  background-color: #fff;
  box-shadow: 0 0 5px #d0d0d0;
  border-radius: 5px;

  .HeaderCellClass {
    background: #859cb7;
    color: #fff;
  }
  .pagination {
    margin-top: 30px;
    margin-bottom: 20px;
  }
}
</style>
