<template>
  <div class="mianContent">
    <div class="titleHeader">
  <div>
    <el-input style="width: 250px; height: 30px; margin-right: 10px; font-size: 14px" suffix-icon="Search"
              placeholder="请输入订单编号" v-model="orderNumber"></el-input>
    <el-button class="ml-5" type="primary" @click="load" style="height: 30px;">搜索</el-button>
    <el-button type="warning" @click="reset" style="height:30px">重置</el-button>
  </div>
      <div>
        <el-popconfirm class="ml-5" confirm-button-text='确定' cancel-button-text='取消' icon="InfoFilled" icon-color="red"
                       title="是否导出本页数据?" @confirm="rentOrderList(tableData)" width=180>
          <template #reference>
            <el-button type="success" size="default" style="height:30px">导出为表格</el-button>
          </template>
        </el-popconfirm>
      </div>
    </div>
  <!-- 数据展示层 -->
  <el-table v-loading="loading" :data="tableData" border stripe header-cell-class-name="headerBg"
            :cell-style="{ 'text-align': 'center', 'font-size': '16px' }" @selection-change="handleSelectionChange"
            :header-cell-style="{ 'text-align': 'center' }" :row-style="{ height: '70px' }">
    <el-table-column prop="id" label="序号" width="50">
      <template #default="{ $index }">
        {{ $index + 1 }}
      </template>
    </el-table-column>
    <el-table-column prop="orderNumber" label="订单编号" width="250"></el-table-column>
    <el-table-column prop="" label="租赁天数">
      <template #default="scope">
        {{ scope.row.clothesSnapshot.period }}
      </template>
    </el-table-column>
    <el-table-column prop="" label="用户名">
      <template #default="scope">
        {{ scope.row.contactsSnapshot.name }}
      </template>
    </el-table-column>
    <el-table-column prop="" label="联系电话" width="150">
      <template #default="scope">
        {{ scope.row.contactsSnapshot.phone }}
      </template>
    </el-table-column>
    <el-table-column prop="createTime" label="下单时间" width="180"></el-table-column>
    <el-table-column prop="totalAmount" label="订单实付金额(元)" width="180"></el-table-column>
    <el-table-column prop="orderStatus" label="订单状态">
      <template #default="scope">
        {{ scope.row.orderStatus === '待发货' ? '待取货' : scope.row.orderStatus }}
      </template>
    </el-table-column>
    <el-table-column label="操作" width="260px" fixed="right">
      <template #default="scope">
        <el-button size="small" @click="showDetail(scope.row)">
          详情
        </el-button>
        <el-popconfirm confirm-button-text='是' cancel-button-text='否' icon="InfoFilled" icon-color="red"
                       title="是否取消订单？" @confirm="cancelOrder(scope.row)" width=180 v-if="['待支付'].includes(scope.row.orderStatus)">
          <template #reference>
            <el-button size="small" type="primary" plain>取消订单</el-button>
          </template>
        </el-popconfirm>
        <el-popconfirm confirm-button-text='是' cancel-button-text='否' icon="InfoFilled" icon-color="red"
                       title="服装是否移交客户？" @confirm="changeOrderStatus(scope.row,'待收货')" width=180 v-if="['待发货'].includes(scope.row.orderStatus)">
          <template #reference>
            <el-button size="small" type="primary" plain>用户已取货</el-button>
          </template>
        </el-popconfirm>
        <el-popconfirm confirm-button-text='是' cancel-button-text='否' icon="InfoFilled" icon-color="red"
                       title="租赁是否完成？" @confirm="changeOrderStatus(scope.row,'交易成功')" width=180 v-if="['待收货'].includes(scope.row.orderStatus)">
          <template #reference>
            <el-button size="small" type="primary" plain>租赁完成</el-button>
          </template>
        </el-popconfirm>
        <el-popconfirm confirm-button-text='确定' cancel-button-text='取消' icon="InfoFilled" icon-color="red"
                       title="确定要退款吗？" @confirm="deleteOrder(scope.row)" width=180>
          <template #reference>
            <el-button v-if="['待发货','待收货'].includes(scope.row.orderStatus)" size="small" type="danger" plain>退款</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页器 -->
  <div style="padding: 10px 0">
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                   :current-page="searchParams.current" :page-size="searchParams.pageSize" :page-sizes="[5, 10, 15, 20]"
                   :small="null" :disabled="null" :background="null" layout="total, sizes, prev, pager, next, jumper"
                   :total="total" />
  </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from "vue";
import myAxios from "@/api/myAxios";
import { ElMessage } from "element-plus";
import {SuccessInfo, ErrorInfo, WarnInfo} from "@/utils/messageInfo";
import { useRouter, useRoute } from "vue-router";
import emitter from "@/utils/emitter";
import {bookOrderList} from "@/utils/excelUtil/bookOrderExcel";
import {rentOrderList} from "@/utils/excelUtil/rentOrderExcel";
const tableData: any = ref([]);  //表单展示数据
const total = ref(0);
const orderNumber = ref('')
const searchParams: any = ref({ //封装分页
  current: 1, //当前页码
  pageSize: 5, //每页显示条数
  sortField: "id",  //根据ID分类
  sortOrder: "descend", //降序
})
const router = useRouter()
const loading = ref(false)
onMounted(() => {
  getOrderList()  //页面加载获取订单列表
})

const handleSizeChange = (newSize: any) => {
  searchParams.value.pageSize = newSize //新的页面数
  getOrderList()
}

const handleCurrentChange = (Current: any) => {
  searchParams.value.current = Current
  getOrderList()
}
const handleSelectionChange = (row: any) => {
  // id.value = JSON.parse(JSON.stringify(row));
}
//获取写真预约订单列表
const getOrderList = async () => {
  try {
    searchParams.value.orderNumber = orderNumber.value
    const res = await myAxios.post('/clothesRent/list', { ...searchParams.value });
    console.log('订单信息--->', res.data.data);
    if (res.data.code === 1) {
      tableData.value = res.data.data.records;
      total.value = parseInt(res.data.data.total)
      // console.log('表单信息--->', tableData.value);
    } else {
      ElMessage({
        message: '获取数据失败',
      });
    }
  } catch (error) {
    ElMessage({
      message: '发生错误',
    });
  }
}
//查看订单详情
const showDetail = (row: any) => {
  router.push({   //不会将参数显示到地址栏中,这么做要修改routes中对应的path
    name: '租赁订单详情',
    params: {
      id: row.id
    }
  })
};
const changeOrderStatus = async (row: any, msg : string) => {   //改变订单状态
  const res = await myAxios.post('/clothesRent/update/orderStatus',{
    id: row.id,
    orderStatus: msg
  })
  if(res.data.code === 1) {
    await getOrderList()
    SuccessInfo(msg)
  } else {
    WarnInfo(res.data.message)
  }
}
const deleteOrder = async (row: any) => {  //微信退款
  loading.value = true
  // console.log('row-->', row)
  const res = await myAxios.post('/wechat/refund/clothesRent/create', { id: row.id })   //传入订单号取消订单并退款
  // console.log(res)
  setTimeout(async () => {
    if (res.data.code === 1) {
      SuccessInfo('退款成功')
      await changeOrderStatus(row, '已退款')
      await getOrderList()
      loading.value = false
    }
  }, 3000)
}
const reset = () => {  //重置搜索框
  orderNumber.value = ''
};
const load = () => {
  getOrderList()
}  //搜索的方法
const cancelOrder = async (row: any) => {  //取消订单的方法(未支付能取消)
  // console.log('点击的这一行--->', row);
  const res = await myAxios.post('/order/cancel/id', { id: row.id })
  if (res.data.code === 1) {
    getOrderList()
    SuccessInfo('取消订单成功')
  } else {
    ErrorInfo('请求错误')
  }
}
</script>

<style lang="scss" scoped>
/* 使用 ::v-deep 来修改表头的对齐方式 */
::v-deep .el-table__header th {
  text-align: center;
  /* 表头居中 */
  font-size: 14px;
  /* 设置字体大小 */
  font-weight: bold;
  /* 设置字体加粗 */
  font-family: 'Arial', sans-serif;
  /* 设置字体 */
}

.mianContent {
  width: 100%;
  min-width: 1000px;
}
.titleHeader {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px
}
</style>