<template>
  <view class="container">
    <!-- 顶部装饰条 -->
    <view class="top-decoration"></view>

    <view class="header">
      <image src="/static/logo.png" class="logo" mode="aspectFit"></image>
      <text class="title">访客登记</text>
      <text class="subtitle">请填写以下信息进行登记</text>
      <view class="header-buttons">
        <view class="user-center-btn" @click="handleGoHome">
          <text class="user-icon">🏠</text>
          <text class="user-text">首页</text>
        </view>
        <view class="user-center-btn" @click="handleUserCenter">
          <text class="user-icon">👤</text>
          <text class="user-text">我的</text>
        </view>
      </view>
    </view>

    <view class="form-card">
      <!-- 访客姓名 -->
      <VisitorInput
        v-model="form.visitorName"
        label="姓名"
        required
        icon="👤"
        placeholder="请输入您的姓名"
      />

      <!-- 访客手机号 -->
      <VisitorInput
        v-model="form.visitorPhone"
        label="手机号"
        required
        icon="📱"
        type="number"
        placeholder="请输入您的手机号"
      />

      <!-- 车牌号码 -->
      <VisitorInput
        v-model="form.licensePlateNumber"
        label="车牌号码"
        required
        icon="🚙"
        placeholder="请输入车牌号"
      />

      <!-- 受访人所在部门 -->
      <VisitorInput
        v-model="form.memberOrg"
        label="受访人所在部门"
        required
        icon="🏢"
        placeholder="请输入受访人所在部门"
      />

      <!-- 受访人姓名 -->
      <VisitorInput
        v-model="form.memberName"
        label="被访人姓名"
        required
        icon="👥"
        placeholder="请输入受访人姓名"
      />

      <!-- 受访人手机号 -->
      <VisitorInput
        v-model="form.memberPhone"
        label="被访人手机号"
        required
        icon="📱"
        type="number"
        placeholder="请输入受访人手机号"
      />

      <!-- 来访事由 -->
      <VisitorInput
        v-model="form.reason"
        label="来访事由"
        required
        icon="📝"
        textarea
        rows="2"
        placeholder="请输入来访事由"
      />

      <!-- 预约日期 -->
      <view class="form-item">
        <text class="form-label required">预约日期</text>
        <picker mode="date" :value="form.visitDate" :start="minDate" :end="maxDate" @change="handleDateChange">
          <view class="date-picker-btn">
            <text>{{ form.visitDate || '请选择预约日期' }}</text>
          </view>
        </picker>
      </view>

      <!-- 预约时间区间 -->
      <view class="form-item">
        <text class="form-label required">选择区间</text>
        <view class="time-range">
          <picker mode="time" :value="form.startTime" @change="handleStartTimeChange">
            <view class="date-picker-btn time-btn">
              <text>{{ form.startTime || '开始时间' }}</text>
            </view>
          </picker>
          <text class="time-separator">至</text>
          <picker mode="time" :value="form.endTime" @change="handleEndTimeChange">
            <view class="date-picker-btn time-btn">
              <text>{{ form.endTime || '结束时间' }}</text>
            </view>
          </picker>
        </view>
      </view>

      <button class="submit-btn" @click="handleSubmit" :disabled="!isFormValid">
        <text class="btn-text">提交登记</text>
      </button>

      <!-- 查看访问记录按钮 -->
      <button class="records-btn" @click="handleViewRecords">
        <text class="btn-text">查看访问记录</text>
      </button>
    </view>

    <!-- 底部装饰 -->
    <view class="bottom-decoration"></view>
  </view>
</template>

<script>
import { api_addVisit } from '@/api/property.js';
import VisitorInput from './components/VisitorInput.vue';

export default {
  components: { VisitorInput },
  data() {
    const today = new Date();
    const minDate = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`;
    return {
      minDate,
      maxDate: '2050-12-31',
      form: {
        visitorName: '',
        visitorPhone: '',
        memberName: '',
        memberPhone: '',
        memberOrg: '',
        reason: '',
        visitDate: '',
        startTime: '',
        endTime: '',
        visitStartTime: null,
        visitEndTime: null,
        licensePlateNumber: '',
        openId: ''
      }
    };
  },
  computed: {
    isFormValid() {
      return this.form.visitorName &&
        /^1[3-9]\d{9}$/.test(this.form.visitorPhone) &&
        this.form.memberName &&
        /^1[3-9]\d{9}$/.test(this.form.memberPhone) &&
        this.form.memberOrg &&
        this.form.reason &&
        this.form.visitStartTime &&
        this.form.visitEndTime &&
        this.form.visitStartTime < this.form.visitEndTime &&
        this.form.licensePlateNumber;
    }
  },
  methods: {
    handleDateChange(e) {
      this.form.visitDate = e.detail.value;
      this.updateVisitTime();
    },
    handleStartTimeChange(e) {
      this.form.startTime = e.detail.value;
      this.updateVisitTime();
    },
    handleEndTimeChange(e) {
      this.form.endTime = e.detail.value;
      this.updateVisitTime();
    },
    updateVisitTime() {
      if (!this.form.visitDate || !this.form.startTime || !this.form.endTime) return;
      this.form.visitStartTime = `${this.form.visitDate} ${this.form.startTime}:00`;
      this.form.visitEndTime = `${this.form.visitDate} ${this.form.endTime}:59`;
    },
    async getWechatAuth() {
      try {
        uni.showLoading({ title: '正在获取授权...' });
        const { code } = await uni.login();
        // 实际项目中应将 code 发送到后端换取 openId
        this.form.openId = 'mock_open_id_' + Date.now();
        uni.hideLoading();
        return true;
      } catch (error) {
        uni.hideLoading();
        uni.showToast({ title: '获取授权失败，请重试', icon: 'none' });
        console.error('微信授权失败:', error);
        return false;
      }
    },
    async handleSubmit() {
      const authSuccess = await this.getWechatAuth();
      if (!authSuccess) return;

      try {
        uni.showLoading({ title: '正在提交...' });
        await api_addVisit({ ...this.form });
        uni.hideLoading();
        uni.showToast({ title: '登记成功', icon: 'success' });

        // 重置表单
        const openId = this.form.openId;
        this.form = {
          visitorName: '',
          visitorPhone: '',
          memberName: '',
          memberPhone: '',
          memberOrg: '',
          reason: '',
          visitDate: '',
          startTime: '',
          endTime: '',
          visitStartTime: null,
          visitEndTime: null,
          licensePlateNumber: '',
          openId
        };
      } catch (error) {
        uni.hideLoading();
        uni.showToast({ title: '提交失败，请重试', icon: 'none' });
        console.error('提交失败:', error);
      }
    },
    handleViewRecords() {
      uni.navigateTo({
        url: `/pages/supply_chain/visitor_records/index?openid=${this.form.openId}`
      });
    },
    handleUserCenter() {
      uni.navigateTo({
        url: `/pages/supply_chain/visitor_user/index?openid=${this.form.openId}`
      });
    },
    handleGoHome() {
      uni.reLaunch({
        url: '/pages/index/index'
      });
    }
  }
};
</script>

<style lang="scss">
.container {
  min-height: 100vh;
  background: linear-gradient(135deg, #3a5da6 0%, #4a72c2 100%);
  padding: 20rpx;
  position: relative;
  overflow: hidden;
}

.top-decoration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 10rpx;
  background: linear-gradient(90deg, #ff6b6b, #4ecdc4, #45b7d1, #96ceb4);
}

.header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 60rpx;
  margin-bottom: 60rpx;
  position: relative;
  z-index: 1;
}

.header-buttons {
  position: absolute;
  bottom: 18rpx;
  left: 20rpx;
  right: 20rpx;
  display: flex;
  justify-content: space-between;
}

.user-center-btn {
  width: 98rpx;
  height: 98rpx;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(10rpx);
  box-shadow: 0 4rpx 15rpx rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.user-center-btn:active {
  transform: scale(0.9);
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.15);
}

.user-icon {
  font-size: 40rpx;
}

.user-text {
  font-size: 24rpx;
  font-weight: 300;
  color: rgba(255, 255, 255, 0.6);
  line-height: 24rpx;
}

.logo {
  width: 180rpx;
  height: 180rpx;
  margin-bottom: 30rpx;
  border-radius: 50%;
  box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.15);
  background-color: rgba(255, 255, 255, 0.9);
  padding: 20rpx;
}

.title {
  font-size: 52rpx;
  font-weight: bold;
  color: #fff;
  margin-bottom: 16rpx;
  text-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.2);
}

.subtitle {
  font-size: 28rpx;
  color: rgba(255, 255, 255, 0.9);
  font-weight: 300;
}

.form-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 24rpx;
  padding: 50rpx;
  box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10rpx);
  position: relative;
  z-index: 1;
}

.form-item {
  margin-bottom: 40rpx;
  display: flex;
  flex-direction: column;
}

.form-label {
  font-size: 32rpx;
  color: #333;
  margin-bottom: 16rpx;
  font-weight: 500;
}

.form-label.required::after {
  content: '*';
  color: #ff6b6b;
  margin-left: 8rpx;
}

.date-picker-btn {
  width: 100%;
  height: 96rpx;
  background-color: #f5f5f5;
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  padding: 0 32rpx;
  box-sizing: border-box;
  font-size: 32rpx;
  color: #666;
  border: 2rpx solid #e0e0e0;
  transition: all 0.3s ease;
}

.date-picker-btn:active {
  background-color: #e0e0e0;
  border-color: #ccc;
}

.time-range {
  display: flex;
  align-items: center;
  gap: 16rpx;
}

.time-btn {
  flex: 1;
}

.time-separator {
  font-size: 28rpx;
  color: #999;
}

.submit-btn {
  width: 100%;
  height: 104rpx;
  background: linear-gradient(135deg, #3a5da6 0%, #4a72c2 100%);
  color: #fff;
  font-size: 38rpx;
  font-weight: 600;
  border-radius: 52rpx;
  margin-top: 30rpx;
  border: none;
  box-shadow: 0 12rpx 30rpx rgba(42, 82, 152, 0.4);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.submit-btn:active {
  transform: translateY(4rpx);
  box-shadow: 0 8rpx 20rpx rgba(102, 126, 234, 0.3);
}

.submit-btn:disabled {
  background: linear-gradient(135deg, #c0c4cc 0%, #a0a0a0 100%);
  box-shadow: 0 8rpx 20rpx rgba(192, 196, 204, 0.3);
  transform: none;
}

.records-btn {
  width: 100%;
  height: 104rpx;
  background: linear-gradient(135deg, #3a5da6 0%, #4a72c2 100%);
  color: #fff;
  font-size: 38rpx;
  font-weight: 600;
  border-radius: 52rpx;
  margin-top: 30rpx;
  border: 2rpx solid #3a5da6;
  box-shadow: 0 6rpx 20rpx rgba(58, 93, 166, 0.2);
  transition: all 0.3s ease;
}

.records-btn:active {
  transform: translateY(4rpx);
  box-shadow: 0 4rpx 15rpx rgba(42, 82, 152, 0.3);
}

.btn-text {
  position: relative;
  display: flex;
	align-items: center;
	justify-content: center;
  z-index: 1;
  color: #fff;
  height: 104rpx;
}

.bottom-decoration {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 200rpx;
  background: linear-gradient(180deg, transparent, rgba(42, 82, 152, 0.2));
  opacity: 0.6;
}

.container::before {
  content: '';
  position: absolute;
  top: 20%;
  right: 10%;
  width: 200rpx;
  height: 200rpx;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.3) 0%, transparent 70%);
  border-radius: 50%;
  animation: float 6s ease-in-out infinite;
}

.container::after {
  content: '';
  position: absolute;
  bottom: 30%;
  left: 5%;
  width: 150rpx;
  height: 150rpx;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.2) 0%, transparent 70%);
  border-radius: 50%;
  animation: float 8s ease-in-out infinite reverse;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0rpx) scale(1);
  }
  50% {
    transform: translateY(-30rpx) scale(1.1);
  }
}
</style>
