<template>
  <div class="task-message-page">
    <!-- 顶部导航 -->
    <s-header :name="$t('taskMessage.refundRemind')" :noback="false"></s-header>

    <div class="task-container">
      <van-list
        v-model:loading="loading"
        v-model:error="error"
        :error-text="$t('common.reqFailClkReload')"
        :finished="finished"
        :finished-text="$t('common.noMoreTxt')"
        offset="300"
        :immediate-check="false"
        @load="onLoad"
      >
        <!-- 顶部信息卡片 -->
        <div class="info-card">
          <div class="stats-area">
            <div class="icon-box">
              <van-icon name="todo-list" class="icon" />
            </div>
            <div class="stats-text">
              {{ $t("taskMessage.total") }}
              <span class="highlight">{{ alarmHistoryTotal }}</span>
              {{ $t("taskMessage.recordsInTotal") }}
            </div>
          </div>

          <van-button
            round
            size="small"
            plain
            class="search-btn"
            @click="noticeClk"
          >
            <van-icon name="search" size="20" class="action-icon" />
          </van-button>
        </div>

        <!-- 列表容器 -->
        <div class="list-container">
          <div
            v-for="(item, index) in alarmHistoryList"
            :key="index"
            class="list-card"
            :class="{
              pending: item.status === 0,
              success: item.status === 1,
              failed: item.checkType === 2,
            }"
          >
            <div class="card-header">
              <div class="status-tag">
                <van-icon name="label-o" class="tag-icon" />
                {{ getStatusText(item) }}
              </div>
              <div class="ref-id">#{{ index + 1 }}</div>
            </div>

            <div class="card-body">
              <div class="info-row">
                <van-icon name="todo-list-o" class="row-icon" />
                <div class="row-content">
                  <span class="label">{{ $t("taskMessage.sn") }}:</span>
                  <span class="value">{{ item.sn || "-" }}</span>
                </div>
              </div>

              <div class="info-row">
                <van-icon name="idcard" class="row-icon" />
                <div class="row-content">
                  <span class="label">{{ $t("taskMessage.clientId") }}:</span>
                  <span class="value">{{ item.clientId || "-" }}</span>
                </div>
              </div>

              <div class="info-row">
                <van-icon name="phone-o" class="row-icon" />
                <div class="row-content">
                  <span class="label">{{ $t("taskMessage.phone") }}:</span>
                  <span class="value">{{ item.phone || "-" }}</span>
                </div>
              </div>

              <div class="info-row">
                <van-icon name="clock-o" class="row-icon" />
                <div class="row-content">
                  <span class="label">{{ $t("taskMessage.sendTime") }}:</span>
                  <span class="value">{{ showDateTime(item.createDate) }}</span>
                </div>
              </div>

              <div v-if="item.remark" class="info-row">
                <van-icon name="comment-o" class="row-icon" />
                <div class="row-content">
                  <span class="label">{{ $t("taskMessage.remark") }}:</span>
                  <span class="value">{{ item.remark }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </van-list>
    </div>

    <!-- 筛选弹窗 -->
    <k-dialog
      :dialogTitle="$t('taskMessage.equipmentInit.searchPop.title')"
      :confirmBtnTxt="$t('taskMessage.equipmentInit.searchPop.filterBtn')"
      ref="kDialogRef"
      @confirmclk="confirmClk"
    >
      <template #content>
        <div class="dialog-form">
          <van-field
            clearable
            v-model="searchParams.sn"
            :placeholder="$t('taskMessage.snPlaceholder')"
            :label="$t('taskMessage.sn')"
            label-align="top"
            class="custom-field"
          >
            <template #left-icon>
              <van-icon name="notes-o" class="field-icon" />
            </template>
          </van-field>

          <van-field
            clearable
            v-model="searchParams.clientId"
            :placeholder="$t('taskMessage.clientIdPlaceholder')"
            :label="$t('taskMessage.clientId')"
            label-align="top"
            class="custom-field"
          >
            <template #left-icon>
              <van-icon name="idcard" class="field-icon" />
            </template>
          </van-field>

          <van-field
            v-model="searchDate"
            is-link
            readonly
            :label="$t('taskMessage.searchDate')"
            :placeholder="$t('taskMessage.searchDatePlaceholder')"
            @click="showPicker = true"
            label-align="top"
            class="custom-field"
          >
            <template #left-icon>
              <van-icon name="calendar-o" class="field-icon" />
            </template>
          </van-field>
        </div>
      </template>
    </k-dialog>

    <!-- 日期选择器 -->
    <van-popup v-model:show="showPicker" position="bottom" round>
      <van-date-picker
        :title="$t('taskMessage.chooseTitle')"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="onConfirm"
        @cancel="showPicker = false"
        :columns-type="columnsType"
        class="date-picker-popup"
      />
    </van-popup>
  </div>
</template>

<script>
import { onMounted, reactive, ref } from "vue";
import sHeader from "@/components/SimpleHeader";
import { refundRecord } from "@/service/taskMessage";
import { showFailToast } from "vant";
import { getLoginUser } from "@/common/js/utils";
import dateUtil from "@/utils/dateUtil";
import kDialog from "@/components/commom/kDialog/index.vue";
import { useI18n } from "vue-i18n";

export default {
  components: { sHeader, kDialog },
  setup() {
    const { t } = useI18n();
    const kDialogRef = ref(null);
    const showPicker = ref(false);
    const searchDate = ref("");
    const minDate = new Date(2024, 9, 1);
    const maxDate = new Date();
    const columnsType = ["year", "month"];
    let startTime = null;
    let endTime = null;
    const onConfirm = ({ selectedValues }) => {
      searchDate.value = selectedValues[0] + "年" + selectedValues[1] + "月";
      startTime = new Date(selectedValues[0] + "-" + selectedValues[1] + "-01");
      endTime = new Date(selectedValues[0], selectedValues[1], 0);
      searchParams.startTime =
        dateUtil.formateDate(startTime, "yyyy-MM-dd") + " 00:00:00";
      searchParams.endTime =
        dateUtil.formateDate(endTime, "yyyy-MM-dd") + " 23:59:59";
      showPicker.value = false;
    };
    // 点击筛选
    const noticeClk = () => {
      kDialogRef.value.openDialog();
    };
    // 点击查询按钮
    const confirmClk = () => {
      searchRecordList();
    };
    const user = getLoginUser();
    const loading = ref(false); // 加载状态
    const error = ref(false); // 错误状态
    const finished = ref(false); // 结束翻页状态
    const alarmHistoryList = ref([]); // 列表集合
    const alarmHistoryTotal = ref(0); // 列表总数
    const searchParams = reactive({
      sendAdminId: "", // 当前登录账户的id
      clientId: "", // 设备编号
      current: 1, // 当前页
      size: 20, // 页大小
      username: "", // 商家名称
      startTime: "", // 开始时间
      endTime: "", // 结束时间
      sn: "", // 订单编号
    });
    // 滚动加载
    const onLoad = () => {
      if (!finished.value) {
        searchParams.current = searchParams.current + 1;
        getList();
      }
    };
    // 查询退款提醒记录
    const searchRecordList = () => {
      alarmHistoryList.value = [];
      searchParams.current = 1;
      getList();
    };
    // 获取设备初始化审批列表数据
    const getList = async () => {
      const { data } = await refundRecord(Object.assign({}, searchParams));
      if (data.code === "00000") {
        // 列表值叠加
        alarmHistoryList.value = alarmHistoryList.value.concat(
          data.data.records
        );
        alarmHistoryTotal.value = data.data.total;
        if (alarmHistoryList.value.length === data.data.total) {
          finished.value = true;
        }
        loading.value = false;
        console.log(alarmHistoryList);
      } else {
        showFailToast(data.message);
      }
    };

    // 初始化页面获取列表
    onMounted(async () => {
      if (user) {
        searchParams.sendAdminId = user.id;
        searchRecordList();
      }
    });
    const showDateTime = (date) => {
      return date
        ? dateUtil.formateDate(new Date(date), "yyyy/MM/dd hh:mm:ss")
        : "";
    };

    const getStatusText = (item) => {
      if (item.status === 0) return t('taskMessage.sendWait');
      if (item.status === 1) return t('taskMessage.sendSucess');
      if (item.checkType === 2) return t('taskMessage.sendFail');
      return '-';
    };
    return {
      loading,
      error,
      finished,
      onLoad,
      alarmHistoryList,
      alarmHistoryTotal,
      showDateTime,
      kDialogRef,
      noticeClk,
      confirmClk,
      user,
      searchParams,
      searchDate,
      showPicker,
      onConfirm,
      columnsType,
      minDate,
      maxDate,
      getStatusText,
    };
  },
};
</script>

<style lang="less" scoped>
@theme-color: #4d6add;
@success-color: #4caf50;
@pending-color: #ff9800;
@failed-color: #f44336;
@light-bg: #f5f8ff;
@card-bg: #ffffff;
@text-dark: #333;
@text-light: #666;
@text-lighter: #999;
@border-radius: 12px;
@card-shadow: 0 4px 12px rgba(77, 106, 221, 0.15);
@hover-shadow: 0 8px 20px rgba(77, 106, 221, 0.25);
@transition: all 0.3s ease;

.task-message-page {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: @light-bg;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    sans-serif;
}

.task-container {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
}

.info-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  margin-bottom: 20px;
  background: @card-bg;
  border-radius: @border-radius;
  box-shadow: @card-shadow;
}

.stats-area {
  display: flex;
  align-items: center;
}

.icon-box {
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: fade(@theme-color, 8%);
  border-radius: 10px;
  margin-right: 12px;

  .icon {
    color: @theme-color;
    font-size: 24px;
  }
}

.stats-text {
  font-size: 15px;
  color: @text-dark;

  .highlight {
    font-size: 18px;
    color: @theme-color;
    font-weight: bold;
    margin: 0 4px;
  }
}

.search-btn {
  height: 32px;
  padding: 0 12px;
  border-color: fade(@theme-color, 50%);
  color: @theme-color;
  font-weight: 500;

  &:active {
    background-color: fade(@theme-color, 8%);
  }
}

.list-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.list-card {
  background: @card-bg;
  border-radius: @border-radius;
  box-shadow: @card-shadow;
  padding: 18px 16px 16px;
  transition: @transition;
  position: relative;
  overflow: hidden;

  &.pending {
    border-left: 3px solid @pending-color;
  }

  &.success {
    border-left: 3px solid @success-color;
  }

  &.failed {
    border-left: 3px solid @failed-color;
  }

  &:hover {
    transform: translateY(-2px);
    box-shadow: @hover-shadow;
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid fade(@theme-color, 10%);
}

.status-tag {
  display: flex;
  align-items: center;
  padding: 5px 10px;
  border-radius: 18px;
  font-size: 13px;
  font-weight: 500;
  min-width: 80px;

  .tag-icon {
    margin-right: 5px;
    font-size: 14px;
  }

  .pending & {
    background-color: fade(@pending-color, 12%);
    color: darken(@pending-color, 15%);
  }

  .success & {
    background-color: fade(@success-color, 12%);
    color: darken(@success-color, 15%);
  }

  .failed & {
    background-color: fade(@failed-color, 12%);
    color: darken(@failed-color, 15%);
  }
}

.ref-id {
  font-size: 12px;
  color: @text-lighter;
  letter-spacing: 0.5px;
}

.card-body {
  display: flex;
  flex-direction: column;
  gap: 13px;
}

.info-row {
  display: flex;
  align-items: flex-start;

  .row-icon {
    flex-shrink: 0;
    font-size: 18px;
    color: @theme-color;
    margin-right: 10px;
  }

  .row-content {
    flex: 1;
    display: flex;
    // flex-wrap: wrap;
  }

  .label {
    color: @text-light;
    min-width: 90px;
    display: inline-block;
    font-size: 14px;
    padding-right: 5px;
  }

  .value {
    color: @text-dark;
    word-break: break-all;
    flex: 1;
    display: flex;
    align-items: center;
    min-width: 150px;
    font-size: 13px;
    font-weight: 500;
  }
}

.dialog-form {
  padding: 10px 0 15px;
}

.custom-field {
  background-color: fade(@theme-color, 4%);
  border-radius: 8px;
  margin-bottom: 15px;
  padding: 12px 16px;

  :deep(.van-cell) {
    padding: 0;
    background: transparent;
  }

  :deep(.van-field__label) {
    margin-bottom: 6px;
    font-weight: 500;
  }

  .field-icon {
    font-size: 20px;
    color: @theme-color;
    margin-right: 8px;
  }
}

.date-picker-popup {
  :deep(.van-picker__toolbar) {
    background-color: @light-bg;
  }

  :deep(.van-picker__confirm) {
    color: @theme-color;
  }
}

// 动画效果
@keyframes cardEntry {
  0% {
    transform: translateY(15px);
    opacity: 0;
  }
  100% {
    transform: translateY(0);
    opacity: 1;
  }
}

.list-card {
  animation: cardEntry 0.4s ease-out forwards;
  opacity: 0;

  &:nth-child(1) {
    animation-delay: 0.05s;
  }
  &:nth-child(2) {
    animation-delay: 0.1s;
  }
  &:nth-child(3) {
    animation-delay: 0.15s;
  }
  &:nth-child(4) {
    animation-delay: 0.2s;
  }
  &:nth-child(5) {
    animation-delay: 0.25s;
  }
}

// 响应式调整
@media (max-width: 360px) {
  .task-container {
    padding: 12px;
  }

  .info-card {
    padding: 12px;
  }

  .icon-box {
    width: 40px;
    height: 40px;
  }

  .search-btn {
    padding: 0 14px;
    font-size: 13px;
  }

  .info-row .label {
    min-width: 80px;
  }
}
</style>
