<template>
  <div class="main-wrapper">

    <div class="row">
      <div class="col" >
        <div class="card">
        <order-data-table title="订单管理" :headers="tableHeaders" :rows="tableData" :data-count="dataCount" @pageChanged="pageChangedHandle"
          />
          <card-loading v-if="!isLoaded"/>
          </div>
      </div>
    </div>
  </div>
</template>

<script>
import OrderDataTable from '@/components/OrderDataTable.vue';
import { mapGetters } from 'vuex'
import CardLoading from '../../components/CardLoading.vue';

export default {
  components: {
    OrderDataTable,
    CardLoading
  },
  data() {
    return {
      dataCount: 0,
      isLoaded: false,
      tableHeaders: [
        { text: "编号", value: "id", width: "155px" },
        { text: "用户", value: "user", width: "214px",type:"obj",property:"username"},
        { text: "订单号", value: "orderNumber", width: "48px" },
        { text: "商户订单号", value: "thirdPartyOrderId", width: "82px" },
        { text: "金额", value: "amount", width: "103px", type: "money"},
        { text: "订单类型", value: "orderType", width: "103px",type:"orderType"},
         { text: "订单状态", value: "status", width: "103px",type:"orderStatus"},
          { text: "创建时间", value: "createdAt", width: "103px",type:"datetime"},
      ],
      tableData: [],
    };
  },
  methods: {
    async loadOrderList(pageIndex = 1, pageSize = 10, desc = true) {
      this.isLoaded = false
      try {
        let res = null
        if(this.isAdmin) res = await this.$api.getOrderList(pageIndex, pageSize, desc)
        else res = await this.$api.getMyOrderList(pageIndex, pageSize, desc)
        if (res.code != 1000) {
          this.$alert(res.message, 'danger')
          return;
        }
        this.tableData = res.data
      } catch (e) {
        this.$alert('订单列表数据加载失败!', 'danger')
        console.error(e)
      }finally{
        this.isLoaded = true
      }
    },
    async loadOrderCount(){
      let res = null
      try{
      if(this.isAdmin) res = await this.$api.getOrderNum();
      else res = await this.$api.getMyOrderNum();
      if (res.code != 1000) {
          this.$alert(res.message, 'danger')
          return;
        }
        this.dataCount = res.data
      } catch (e) {
        this.$alert('订单列表数据加载失败!', 'danger')
        console.error(e)
      }

    },
    async pageChangedHandle(newval) {
      await this.loadOrderList(newval.pageIndex, newval.pageSize, true)
    }
  },
  computed: {
    ...mapGetters('userinfo',['isAdmin'])
  },
  async mounted() {
    await this.loadOrderList()
    await this.loadOrderCount()
    
  }
};
</script>
