<template>
  <view class="cv-container">

    <!-- 可滚动内容区 -->
    <view class="cv-scroll-content">
      <!-- 访客信息 -->
      <view class="info">
        <view class="cv-section-title">访客信息</view>
        <view class="cv-section-tag">姓名</view>
        <input class="cv-input" placeholder="请输入姓名" v-model="form.visitorName"/>
        <view class="cv-section-tag">电话</view>
        <input class="cv-input" placeholder="请输入联系电话" v-model="form.visitorPhone"/>
        <view class="cv-section-tag">所属公司</view>
        <input class="cv-input" placeholder="请输入所属公司" v-model="form.visitorUnit"/>
      </view>
      <!-- 选择来访目的 -->
      <view class="cv-section">
        <view class="cv-section-title">来访目的</view>
        <view class="cv-form-group">
          <view class="cv-select">
            <view class="cv-select-header" @click="togglePurposeDropdown"
                  :class="{'dropdown-open': purposeDropdownOpen}">
              <span>{{ getReasonLabel(form.visitingReason) || '请选择拜访事由' }}</span>
              <view class="cv-select-arrow" :class="{open: purposeDropdownOpen}"></view>
            </view>
            <view class="cv-select-options" :class="{open: purposeDropdownOpen}">
              <view v-for="(purpose, dictValue) in purposes"
                    :key="dictValue"
                    class="cv-option"
                    :class="{selected: form.visitingReason === purpose}"
                    @click="selectPurpose(purpose)">
                {{ purpose.dictLabel }}
              </view>
            </view>
          </view>
        </view>
      </view>
      <!-- 来访时间 -->
      <view class="cv-section">
        <view class="cv-section-title">来访时间</view>
        <view class="datetime-row">
          <uni-datetime-picker class="datetime-picker" type="datetime" :hide-second="true" v-model="form.visitingBeginTime"/>
          <view class="separator-line"></view>
          <uni-datetime-picker class="datetime-picker" type="datetime" :hide-second="true" v-model="form.visitingEndTime"/>
        </view>

      </view>
      <!-- 提交按钮 -->
      <button class="cv-submit-btn" @click="submit">提交</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      header: '',
      form: {
        visitorName: '',
        visitorPhone: '',
        visitingReason: '',
        visitingBeginTime: '',
        visitingEndTime: '',
        interviewedPerson:"sdada",
        InterviewedUnit:'dasdas',
        visitorUnit:''
      },
      purposes: [],
      purposeDropdownOpen: false,
    }
  },
  onLoad() {
    // 获取业务类型
    this.getTypes();
  },
  onShow() {
    uni.$once('selectPlate', plate => {
      this.form.licensePlate = plate;
    });
  },
  methods: {
    togglePurposeDropdown() {
      this.purposeDropdownOpen = !this.purposeDropdownOpen;
    },
    selectPurpose(purpose) {
      this.form.visitingReason = purpose.dictValue;
      this.purposeDropdownOpen = false;
    },
    //提交
    async submit() {
      let res = await this.$u.api.visitor.addVisitor(this.form);
      if (res.code == 200) {
        uni.navigateTo({
          url: '/pages/sys/user/myVisitor/myVisitor'
        });
      }
    },
    async getTypes() {
      let res = await this.$u.api.getType("reason_for_visit");
      if (res.code == '200') {
        this.purposes = res.data;
      }
    },
    getReasonLabel(value) {
      const item = this.purposes.find(item => item.dictValue === value);
      return item ? item.dictLabel : '';
    },
    chooseRoom() {
      // 这里可弹出选择房间号
      uni.navigateTo({
        url: '/pages/sys/user/myVisitor/selectRoom'
      });
    },
    chooseCarNumber() {
      // 这里可弹出选择车牌号
      uni.navigateTo({
        url: '/pages/sys/user/myPayment/myCarCode'
      });
    }
  }
}
</script>

<style scoped>
.cv-container {
  background: #fff;
  height: calc(100vh - (44px + env(safe-area-inset-top)));
  display: flex;
  flex-direction: column;
}

.cv-scroll-content {
  flex: 1;
  overflow-y: auto;
  padding-bottom: 120rpx;
}

.cv-section {
  margin: 36rpx 56rpx 0 56rpx;
}

.info {
  position: relative;
  margin: 36rpx 56rpx 0 56rpx;
}

.cv-section-title {
  font-size: 32rpx;
  color: #000;
  font-weight: 600;
}

.cv-section-tag {
  font-size: 28rpx;
  color: #000;
  font-weight: 500;
  margin: 36rpx 0 16rpx 0;
}

/* 下拉框样式 */
.cv-select {
  position: relative;
  width: 100%;
}

.cv-form-group {
  margin: 36rpx 0 16rpx 0;
}

.cv-select-header {
  padding: 14px 16px;
  border: 1px solid #ddd;
  border-radius: 10px;
  font-size: 28rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
  cursor: pointer;
  z-index: 2; /* 确保选择框在下拉框上方 */
  position: relative; /* 添加定位 */
}

.cv-select-arrow {
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 8px solid #777;
  transition: transform 0.3s;
}

.cv-select-arrow.open {
  transform: rotate(180deg);
}

.cv-select-options {
  position: absolute;
  top: calc(100% - 1px);
  left: 0;
  width: 100%;
  background: white;
  border: 1px solid #ddd;
  border-top: none;
  border-radius: 0 0 10px 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  z-index: 1;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease-out;
  /* 修复隐藏时的黑线问题 */
  opacity: 0;
  visibility: hidden;
}

.cv-select-options.open {
  max-height: 300px;
  overflow-y: auto;
  /* 显示时恢复可见性 */
  opacity: 1;
  visibility: visible;
}

.cv-option {
  padding: 14px 16px;
  font-size: 28rpx;
  cursor: pointer;
  transition: background 0.2s;
}

.cv-option:hover {
  background: #f0f7ff;
}

/* 下拉框打开时持续显示阴影 */
.cv-select-header.dropdown-open {
  border-color: #007cff;
  border-radius: 10px 10px 0 0; /* 打开时上圆角下直角 */
  border-bottom: 1px solid #007cff; /* 添加底部边框与下拉框衔接 */
}

.cv-option.selected {
  background: #007cff;
  color: white;
}

.datetime-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 15rpx;
  margin: 36rpx 0 0 0;
}

.datetime-picker {
  flex: 1;
  max-width: 500rpx;
  position: relative;
}

::v-deep .uni-date__x-input {
  font-size: 11px;
  font-weight: 600;
}

.separator-line {
  width: 10rpx;
  height: 2px;
  background: #bdc3c7;
  border-radius: 1px;
}

.cv-input {
  height: 73rpx;
  background: #F7F7F7;
  border-radius: 10rpx;
  font-size: 24rpx;
  color: #222;
  padding: 0 24rpx;
  margin-bottom: 29rpx;
  border: none;
  outline: none;
}

.cv-submit-btn {
  width: 90vw;
  height: 90rpx;
  background: #0090FF;
  color: #fff;
  font-size: 36rpx;
  border: none;
  border-radius: 45rpx;
  margin: 80rpx auto 0 auto;
  display: block;
  font-weight: bold;
  box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.18);
}
</style>