<script setup lang="ts">
import { ref } from "vue";
import { useOrder } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Refresh from "~icons/ep/refresh";
import View from "~icons/ep/view";
import Refund from "~icons/ep/money";
import Cancel from "~icons/ep/delete";
import Search from "~icons/ep/search";
import Setting from "~icons/ep/setting";

defineOptions({
  name: "OrderManage"
});

const formRef = ref();
const tableRef = ref();

const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  onSearch,
  resetForm,
  handleDetail,
  handleRefund,
  handlePayScoreCancel,
  handleQueryFromHaha,
  handleRepairPayScore,
  quickRepairPayScore,
  handleSizeChange,
  handleCurrentChange
} = useOrder(tableRef);

const payscoreCancelVisible = ref(false);
const payscoreCancelForm = ref({ outOrderNo: "", reason: "" });
const payscoreCancelLoading = ref(false);

function openPayScoreCancel() {
  payscoreCancelForm.value = { outOrderNo: "", reason: "" };
  payscoreCancelVisible.value = true;
}

async function doPayScoreCancel() {
  if (!payscoreCancelForm.value.outOrderNo.trim()) {
    return;
  }
  payscoreCancelLoading.value = true;
  try {
    await handlePayScoreCancel(payscoreCancelForm.value);
    payscoreCancelVisible.value = false;
    payscoreCancelForm.value = { outOrderNo: "", reason: "" };
  } finally {
    payscoreCancelLoading.value = false;
  }
}
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-full pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="订单编号：" prop="orderNo">
        <el-input
          v-model="form.orderNo"
          placeholder="请输入订单编号"
          clearable
          class="w-[150px]!"
        />
      </el-form-item>
      <el-form-item label="手机号：" prop="phone">
        <el-input
          v-model="form.phone"
          placeholder="请输入手机号"
          clearable
          class="w-[150px]!"
        />
      </el-form-item>
      <el-form-item label="设备ID：" prop="deviceId">
        <el-input
          v-model="form.deviceId"
          placeholder="请输入设备ID"
          clearable
          class="w-[150px]!"
        />
      </el-form-item>
      <el-form-item label="支付状态：" prop="payStatus">
        <el-select
          v-model="form.payStatus"
          placeholder="请选择"
          clearable
          class="w-[140px]!"
        >
          <el-option label="已支付" value="paid" />
          <el-option label="待支付" value="unpaid" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态：" prop="status">
        <el-select
          v-model="form.status"
          placeholder="请选择"
          clearable
          class="w-[140px]!"
        >
          <el-option label="已取消" :value="0" />
          <el-option label="待支付" :value="1" />
          <el-option label="已完成" :value="2" />
          <el-option label="已关闭" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="下单时间：" prop="dateRange">
        <el-date-picker
          v-model="form.dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
          class="w-[210px]!"
          @change="(val) => {
            form.startDate = val ? val[0] : '';
            form.endDate = val ? val[1] : '';
          }"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri/search-line')"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          @click="handleQueryFromHaha()"
        >
          平台查询
        </el-button>
        <el-button
          type="warning"
          :icon="useRenderIcon(Cancel)"
          @click="openPayScoreCancel"
        >
          支付分取消
        </el-button>
        <el-button
          type="primary"
          :icon="useRenderIcon(Setting)"
          @click="handleRepairPayScore"
        >
          支付分补扣
        </el-button>
      </el-form-item>
    </el-form>

    <el-dialog
      v-model="payscoreCancelVisible"
      title="手动取消支付分订单"
      width="480px"
      :close-on-click-modal="false"
    >
      <el-form :model="payscoreCancelForm" label-width="100px">
        <el-form-item label="订单号" required>
          <el-input
            v-model="payscoreCancelForm.outOrderNo"
            placeholder="输入 outOrderNo，如 PS1782351340B150534"
          />
        </el-form-item>
        <el-form-item label="取消原因">
          <el-input
            v-model="payscoreCancelForm.reason"
            placeholder="管理员手动取消"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="payscoreCancelVisible = false">取消</el-button>
        <el-button
          type="primary"
          :loading="payscoreCancelLoading"
          @click="doPayScoreCancel"
        >
          确认取消
        </el-button>
      </template>
    </el-dialog>

    <PureTableBar
      title="订单管理"
      :columns="columns"
      @refresh="onSearch"
    >
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          row-key="id"
          adaptive
          :adaptiveConfig="{ offsetBottom: 108 }"
          align-whole="center"
          table-layout="auto"
          :loading="loading"
          :size="size"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="{ ...pagination, size }"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(View)"
              @click="handleDetail(row)"
            >
              详情
            </el-button>
            <el-button
              v-if="row.status === 1 && (row.refundAmount || 0) < (row.paidAmount || row.totalAmount || 0)"
              class="reset-margin"
              link
              type="danger"
              :size="size"
              :icon="useRenderIcon(Refund)"
              @click="handleRefund(row)"
            >
              退款
            </el-button>
            <el-button
              v-if="row.payStatus === 'UNPAID' && (row.totalAmount || row.paidAmount) > 0 && row.payScoreState !== 'REVOKED' && row.payScoreState !== 'USER_CANCEL'"
              class="reset-margin"
              link
              type="warning"
              :size="size"
              @click="quickRepairPayScore(row)"
            >
              补扣
            </el-button>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style lang="scss" scoped>
.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
