<template>
  <img
    class="meeting-cover"
    :src="meeting.meetingPic ? meeting.meetingPic : store.getters.defaultCover"
    alt=""
  />
  <h3 class="meeting-title">{{ meeting.meetingName }}</h3>
  <p class="meeting-time">
    <span class="title">会议时间</span>
    <span class="time">{{ meeting.startTime }}</span>
  </p>
  <div class="form-container">
    <el-form
      :model="newJoinUser"
      ref="newJoinUserRef"
      :rules="joinUserRules"
      label-position="left"
    >
      <div class="joinuser-wrap" style="display: flex; flex-wrap: wrap">
        <div class="joinuser-item">
          <el-form-item label="IC卡编号/手机号码" prop="icCard">
            <el-input
              v-model="newJoinUser.icCard"
              placeholder="请输入IC卡号或手机号码"
            />
          </el-form-item>
        </div>
        <div class="joinuser-item">
          <el-form-item label="姓名" prop="nickname">
            <el-input v-model="newJoinUser.nickname" placeholder="请输入姓名" />
          </el-form-item>
        </div>
      </div>
      <el-row type="flex" justify="center"> </el-row>
    </el-form>
    <div
      class="agreement"
      style="
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #333;
      "
    >
      <el-checkbox v-model="isAgreeAgreement"></el-checkbox>
      <span>&nbsp;</span>
      <span>同意</span>
      <el-button type="text" @click="showUserAgreement = true">
        楚桓会议系统用户信息协议
      </el-button>
    </div>

    <div style="display: flex; justify-content: center">
      <el-button type="primary" @click="submitVerify(newJoinUserRef)"
        >加入会议</el-button
      >
    </div>
  </div>
  <agreement
    :show-dialog="showUserAgreement"
    @close="showUserAgreement = false"
    @agree="isAgreeAgreement = true"
    @decline="isAgreeAgreement = false"
  />
</template>

<script setup>
import { reactive, ref, toRefs } from "vue";
import { useStore } from "vuex";
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import agreement from "@/components/agreement.vue";
import _ from "lodash";
const store = useStore();
const router = useRouter();
const route = useRoute();
const data = reactive({
  newJoinUser: {},
  fieldConfig: {
    icCard: true,
  },
  meeting: {},
  joinUserRules: {
    icCard: [
      {
        required: true,
        trigger: "blur",
        message: "必填",
      },
    ],

    nickname: [
      {
        required: true,
        trigger: "blur",
        message: "必填",
      },
    ],
  },
});
const { newJoinUser, joinUserRules, meeting } = toRefs(data);
const meetingId = ref(route.params.meetingId);

meeting.value = _.cloneDeep(store.state.meeting);
newJoinUser.value.meetingId = meetingId.value;

const showUserAgreement = ref(false); // 是否显示用户协议弹窗
const isAgreeAgreement = ref(false); // 是否同意协议

const newJoinUserRef = ref(null);
const submitVerify = async (newJoinUserRef) => {
  if (!isAgreeAgreement.value) {
    ElMessage.error("请勾选同意用户协议");
    return;
  }
  const valid = await newJoinUserRef.validate();
  store.commit("setJoinUser", newJoinUser.value);
  router.push(`/panelist/${meetingId.value}`);
};
</script>

<style lang="scss" scoped>
@import "@/style/appoint-mobile.scss";
@import "@/style/appoint-desktop.scss";

.meeting-title {
  text-align: center;
}
.meeting-time {
  text-align: center;
}
.form-item-container {
  display: flex;
}
</style>
