<template>
  <view class="switch-roles">
    <nav-bar title="选择公司规模" color="#000"></nav-bar>
    <view class="roles-content">
      <view class="content">
        <view class="progress-num"> <text>1</text>/8 </view>
        <view class="title">公司标准工作时间</view>
        <view class="desc">
          开始完善公司主页吧，让求职者了解公司、信任职位
          添加工作时间，标准时间仅为求职者提供参考；不代表公司下所有职位工作时间
        </view>

        <view class="check-title-big">工作时间</view>
        <view class="time-picker-container">
          <view class="time-item-box" @click="showStartTime = true">
            <view class="time-label">开始时间</view>
            <view class="time-value time-item">{{ startTime || "请选择开始时间" }}</view>
          </view>
          <text class="time-separator">至</text>
          <view class="time-item-box" @click="showEndTime = true">
            <view class="time-label">结束时间</view>
            <view class="time-value time-item">{{ endTime || "请选择结束时间" }}</view>
          </view>
        </view>

        <view class="selected-time" v-if="startTime && endTime">
          已选择：{{ startTime }} - {{ endTime }}
        </view>

        <view class="check-title-big">休息时间（可选）</view>
        <view class="check-box">
          <view
            class="check-item"
            :class="{ 'is-check': check == index }"
            v-for="(item, index) in peopleList"
            :key="index"
            @click="checkPeople(index)"
          >
            {{ item }}
          </view>
        </view>
        <view class="check-title-big">加班情况（可选）</view>
        <view class="check-box grid-three">
          <view
            class="check-item"
            :class="{ 'is-check': work == index }"
            v-for="(item, index) in workList"
            :key="index"
            @click="checkWork(index)"
          >
            {{ item }}
          </view>
        </view>
      </view>
    </view>

    <view class="submit-btn" @click="goJobPostingSecond">下一步</view>

    <!-- 开始时间选择器 -->
    <u-picker
      mode="time"
      v-model="showStartTime"
      @confirm="onStartTimeConfirm"
      :params="timeParams"
    ></u-picker>

    <!-- 结束时间选择器 -->
    <u-picker
      mode="time"
      v-model="showEndTime"
      @confirm="onEndTimeConfirm"
      :params="timeParams"
    ></u-picker>
  </view>
</template>
<script>
import navBar from "@/components/nav-bar/index.vue";
export default {
  data() {
    return {
      peopleList: ["双休", "排版轮休"],
      workList: ["不加班", "偶尔加班", "弹性工作"],
      check: 0,
      work: 0,
      text: "",
      showStartTime: false,
      showEndTime: false,
      startTime: "",
      endTime: "",
      timeParams: {
        year: false,
        month: false,
        day: false,
        hour: true,
        minute: true,
        second: false,
      },
      numList: [
        {
          name: "填写基本信息",
        },
        {
          name: "选择职位要求",
        },
      ],
    };
  },
  components: {
    navBar,
  },
  onLoad(options) {
    if (options.text) {
      this.text = options.text;
    }
  },
  methods: {
    goBusinessLicense() {
      uni.navigateTo({ url: "/pages/my/businessLicense" });
    },
    checkPeople(index) {
      this.check = index;
    },
    checkWork(index) {
      this.work = index;
    },
    // 开始时间确认
    onStartTimeConfirm(e) {
      this.startTime = `${e.hour}:${e.minute}`;
    },

    // 结束时间确认
    onEndTimeConfirm(e) {
      this.endTime = `${e.hour}:${e.minute}`;
    },

    goJobPostingSecond() {
      // 验证时间选择
      if (!this.startTime || !this.endTime) {
        uni.showToast({
          title: "请选择完整的工作时间",
          icon: "none",
        });
        return;
      }

      // 这里可以添加时间逻辑验证，比如结束时间不能早于开始时间
      if (this.compareTime(this.startTime, this.endTime) >= 0) {
        uni.showToast({
          title: "结束时间必须晚于开始时间",
          icon: "none",
        });
        return;
      }

      // 将选择的时间数据传递到下一页或保存
      const companyData = {
        workTime: `${this.startTime}-${this.endTime}`,
        workRestTime: this.peopleList[this.check],
		    workOverTime:this.workList[this.work]
      };
	  
      uni.navigateTo({
        url:
          "/my/renzheng/companyDev?companyData=" +
          encodeURIComponent(JSON.stringify(companyData)),
      });
    },

    // 比较时间大小
    compareTime(time1, time2) {
      const [h1, m1] = time1.split(":").map(Number);
      const [h2, m2] = time2.split(":").map(Number);

      if (h1 !== h2) {
        return h1 - h2;
      }
      return m1 - m2;
    },
  },
};
</script>
<style lang="scss" scoped>
.switch-roles {
  background-color: #fff;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;

  .roles-content {
    width: 100%;
    flex: 1;
    overflow: hidden;
    overflow-y: auto;

    .content {
      padding: 40rpx;
      box-sizing: border-box;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      .progress-num {
        color: #016bf6;
        font-family: DM Sans;
        font-size: 24rpx;
        font-weight: 500;
        width: 100%;
        padding-bottom: 20rpx;
        box-sizing: border-box;
        text {
            font-size: 48rpx;
            font-weight: 700;
        }
      }
      .title {
        color: #333;
        width: 100%;
        font-family: DM Sans;
        font-size: 48rpx;
        font-weight: 700;
      }

      .desc {
        color: rgba(102, 102, 102, 1);
        width: 100%;
        font-family: DM Sans;
        font-size: 24rpx;
        font-weight: 400;
        line-height: 32rpx;
        letter-spacing: 0.5%;
        text-align: left;
        padding: 20rpx 0;
        box-sizing: border-box;
      }

      .time-picker-container {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 20rpx;
        .time-item-box {
          text-align: center;
          min-width: 40%;
        }
        .time-item {
          flex: 1;
          border-radius: 100rpx;
          padding: 20rpx 32rpx;
          box-sizing: border-box;
          border: 2rpx solid rgba(227, 231, 236, 1);
          display: flex;
          flex-direction: column;
          align-items: center;
        }

        .time-label {
          color: rgba(153, 153, 153, 1);
          font-family: DM Sans;
          font-size: 20rpx;
          font-weight: 400;
          padding-bottom: 20rpx;
          box-sizing: border-box;
        }

        .time-value {
          color: rgba(51, 51, 51, 1);
          font-family: DM Sans;
          font-size: 24rpx;
          font-weight: 500;
        }

        .time-separator {
          margin: 0 20rpx;
          padding-top: 40rpx;
          box-sizing: border-box;
          color: rgba(102, 102, 102, 1);
          font-family: DM Sans;
          font-size: 24rpx;
          font-weight: 500;
        }
      }

      .selected-time {
        width: 100%;
        text-align: center;
        color: #016bf6;
        font-family: DM Sans;
        font-size: 28rpx;
        font-weight: 500;
        margin-bottom: 20rpx;
        padding: 16rpx;
        background: rgba(245, 248, 254, 1);
        border-radius: 16rpx;
      }

      .check-title-big {
        color: rgba(31, 44, 55, 1);
        font-family: DM Sans;
        font-size: 28rpx;
        font-weight: 500;
        line-height: 44rpx;
        letter-spacing: 0.5%;
        width: 100%;
        padding: 20rpx 0;
        box-sizing: border-box;
      }

      .check-box {
        width: 100%;
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 24rpx;

        .check-item {
          border-radius: 16rpx;
          background: rgba(245, 248, 254, 1);
          color: rgba(153, 153, 153, 1);
          font-family: DM Sans;
          font-size: 28rpx;
          font-weight: 400;
          line-height: 44rpx;
          text-align: center;
          padding: 12rpx 48rpx;
          box-sizing: border-box;
        }

        .is-check {
          box-sizing: border-box;
          border: 1rpx solid #016bf6;
          border-radius: 16rpx;
          background: rgba(252, 233, 220, 1);
          color: #016bf6;
        }
      }
      .grid-three {
        grid-template-columns: repeat(3, 1fr) !important;
      }
    }
  }

  .submit-btn {
    flex-shrink: 0;
    border-radius: 999px;
    background: #ff6600;
    color: rgba(255, 255, 255, 1);
    font-family: DM Sans;
    font-size: 32rpx;
    font-weight: 400;
    line-height: 48rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 16rpx 32rpx;
    box-sizing: border-box;
    margin: 60rpx 62rpx;
  }
}
</style>
