<template>
  <div>
    <el-table v-loading="loading" :data="orderList">
      <el-table-column align="center" type="index" label="#"></el-table-column>
      <el-table-column align="center" label="订单编号" width="200" prop="orderNo"></el-table-column>
      <el-table-column align="center" label="运维用户" prop="adminName"></el-table-column>
      <el-table-column align="center" label="手机号" prop="phone"></el-table-column>
      <el-table-column align="center" label="换电前电量" prop="beforeElectric"></el-table-column>
      <el-table-column align="center" label="换电后电量" prop="afterElectric"></el-table-column>
      <el-table-column align="center" label="换电用时" prop="swapDuration"></el-table-column>
      <el-table-column align="center" label="状态" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.et_admin_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="完成时间" prop="createTime"></el-table-column>
    </el-table>

    <pagination
      :auto-scroll="false"
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getOrderList"
    />
  </div>
</template>

<script>
import { listOrder } from "@/api/system/adminOrder";

export default {
  name: 'replacementRecord',
  dicts: ['et_admin_order_status'],
  props: {
    // 设备id
    sn: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      orderList: [], // 充值记录列表
      loading: false,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: 2,
        sn: null,
      }
    }
  },
  watch: {
    sn(nv, ov) {
      this.getOrderList(nv);
    }
  },
  created() {
    this.getOrderList(this.sn);
  },
  methods: {
    // 获取充值记录
    getOrderList(sn) {
      if(sn == null) {
        this.recordList = [];
        this.total = 0;
        return;
      }
      this.loading = true;
      this.queryParams.sn = sn | this.sn;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.orderList.forEach(item => {
          if(item.completeTime){
            const createTime = new Date(item.createTime);
            const completeTime = new Date(item.completeTime);
            const diffTime = completeTime.getTime() - createTime.getTime();
            const hours = Math.floor(diffTime / (1000 * 60 * 60));
            const minutes = Math.floor((diffTime % (1000 * 60 * 60)) / (1000 * 60));
            const seconds = Math.floor((diffTime % (1000 * 60)) / 1000);
            item.swapDuration = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
          }
        });
        this.total = response.total;
      }).finally(() => {
        this.loading = false;
      });
    }
  }
}
</script>
