<template>
  <div class="phone-query">
    <div class="phone-query__container">
      <div class="phone-query__icon">
        <i class="el-icon-search"></i>
      </div>
      <h2 class="phone-query__title">业务办理告知</h2>
      <!-- <p class="phone-query__subtitle">输入手机号查询相关文件</p> -->
      <el-card class="phone-query__card" shadow="hover">
        <el-form :model="form" ref="formRef">
          <el-form-item prop="phone">
            <el-input
              v-model="form.phone"
              placeholder="请输入手机号"
              maxlength="11"
              
              size="large"
              prefix-icon="el-icon-mobile-phone"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              @click="handleQuery"
              class="phone-query__btn"
            >
              <i v-if="!loading" class="el-icon-search"></i>
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { buildPdfUrl } from '@/api/client'
export default {
  name: "PhoneQuery",
  data() {
    return {
      form: {
        phone: "",
      },
      loading: false,
    };
  },
  methods: {
    async handleQuery() {
      const phone = (this.form.phone || "").trim();
      if (!phone) {
        this.$message.warning("请输入手机号");
        return;
      }
      if (!/^1[3-9]\d{9}$/.test(phone)) {
        this.$message.warning("请输入正确的手机号");
        return;
      }
      this.loading = true;
      try {
        const { searchByPhone } = await import("@/api/registrations");
        let  data = await searchByPhone(phone);        
        if (data != null && data.length > 0) {
             data = data[0]
          if (data.status == "待登记") {
            this.$message.warning("暂未查询到相关信息");
          } else if (data.status == "已登记") {
            const { fileName } = data;
            if (fileName == null) {
              this.$message.warning("未查询到相关信息");
            }else{
              window.location.href = buildPdfUrl(fileName)
            }
          }
        }else{
          this.$message.warning("暂未查询到相关信息");
        }

        // TODO: 调用查询接口
        // window.location.href = "/pdf/1.pdf";
      } catch (err) {
        this.$message.error("查询失败");
        // eslint-disable-next-line no-console
        console.error(err);
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style scoped>
.phone-query {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.phone-query__container {
  text-align: center;
}

.phone-query__icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 20px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.phone-query__icon i {
  font-size: 36px;
  color: #fff;
}

.phone-query__title {
  color: #fff;
  font-size: 28px;
  font-weight: 600;
  margin: 0 0 8px;
}

.phone-query__subtitle {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  margin: 0 0 30px;
}

.phone-query__card {
  width: 360px;
  border-radius: 12px;
  border: none;
}

.phone-query__card ::v-deep .el-card__body {
  padding: 30px;
}

.phone-query__card ::v-deep .el-input__inner {
  height: 48px;
  line-height: 48px;
  font-size: 16px;
  border-radius: 8px;
}

.phone-query__card ::v-deep .el-input__prefix {
  left: 12px;
  font-size: 18px;
  color: #909399;
}

.phone-query__card ::v-deep .el-input--prefix .el-input__inner {
  padding-left: 40px;
}

.phone-query__btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  border-radius: 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}

.phone-query__btn:hover {
  opacity: 0.9;
}

.phone-query__btn i {
  margin-right: 6px;
}

@media (max-width: 480px) {
  .phone-query__card {
    width: 100%;
    max-width: 360px;
  }

  .phone-query__title {
    font-size: 24px;
  }

  .phone-query__icon {
    width: 64px;
    height: 64px;
  }

  .phone-query__icon i {
    font-size: 28px;
  }
}
</style>
