<template>
  <BaseLayout>
    <BaseContainer>
      <div class="pb-5">
        <Heading tag="h1" level="primary">A warm welcome from Taakd.</Heading>
        <Heading tag="h2" level="secondary-2" class="mt-1">
          Let’s set up your account. All fields below are required.
        </Heading>
      </div>
      <!-- <p class="my-5">[User's Name]</p> -->
      <p class="my-5">{{ session.user }}</p>
      <form @submit.prevent="confirmData">
        <div class="py-5">
          <Heading class="my-1" tag="h3" level="secondary-2">Create New Password</Heading>
          <div class="py-3 lg:w-1/2">
            <FormControl
              :type="'password'"
              size="sm"
              variant="subtle"
              placeholder="Enter password"
              label="Enter password"
              v-model="enterPassword"
              :class="{ 'border-red-500': passwordError }"
              @input="validatePasswords"
            />
            <p v-if="passwordError" class="text-red-500">{{ passwordError }}</p>
          </div>
          <div class="py-3 lg:w-1/2">
            <FormControl 
              :type="'password'"
              size="sm"
              variant="subtle"
              placeholder="Confirm password"
              label="Confirm Password"
              v-model="confirmPassword"
              :class="{ 'border-red-500': passwordError }"
              @input="validatePasswords"
              required
            />
            <p v-if="passwordError" class="text-red-500">{{ passwordError }}</p>
          </div>
        </div>
        <div class="py-5">
          <Heading class="my-1" tag="h3" level="secondary-2">Text notifications</Heading>
          <p class="text-sm font-medium">Would you like to subscribe to text messages for notifications and password resets?</p>
          <div>
            <div class="flex items-start gap-1 py-2">
              <input type="radio" name="textNotifications" value="yes" v-model="textNotifications" />
              <Label class="block"> Yes, </Label>
              <p class="mt-1">I acknowledge and agree that I may receive texts for notifications and password resets, and that standard data and text charges may apply.</p>
            </div>
            <div class="flex gap-1 py-2">
              <input type="radio" name="textNotifications" value="no" v-model="textNotifications" />
              <Label class="block"> No, </Label>
              <p class="mt-1">I acknowledge and agree that I may receive texts for notifications and password resets, and that standard data and text charges may apply.</p>
            </div>
          </div>
        </div>
        <div class="pt-5 flex w-full justify-center">
          <!-- الزر هنا معطل عند تحميل الصفحة -->
          <Button type="submit" :disabled="disabled" label="Next ->" :class="['bg-secondary hover:bg-secondary_hover px-4 py-2 text-white']" >

          </Button>
        </div>
      </form>
    </BaseContainer>
  </BaseLayout>
</template>

<script setup>
import { ref, watch } from 'vue';
import { Button, FormControl } from 'frappe-ui';
import { createResource , createListResource} from 'frappe-ui'
import Heading from "../components/heading.vue";
import BaseContainer from "../components/baseContainer.vue";
import BaseLayout from "../layouts/baseLayout.vue";
import { session } from '../data/session'

const enterPassword = ref('');
const confirmPassword = ref('');
const textNotifications = ref('');
const disabled = ref(true); // الزر معطل عند تحميل الصفحة
const passwordError = ref('');
const subscribeToTextMmessages = ref(false);
const createNewPassword = ref(false);

// ======================================
const userList = createListResource({
  doctype: 'User',
  fields: ['email','username','full_name','new_password','name','first_name'],
  filters: {
      email: session.user
    },
    auto:true,
  // pageLength: 1,
    setValue: {
        onSuccess(d) {
          createNewPassword = true;
          // session.logout.submit()
          updateCreateNewPassword() 
        },
        onError(error) {
        }
    },
})

function updatePassword() {
  if (!session.user) {
    console.error("Session user is not defined");
    return;
  }
  userList.setValue.submit({
    name: session.user,  // Ensure this is the correct identifier for the user document
    new_password: confirmPassword.value
  });
}

const requestList = createListResource({
  doctype: 'Verification Instructions Request',
  fields: ['name','user_id','create_new_password','subscribe_to_text_messages'],
  filters: {
    user_id: session.user
    },
    auto:true,
    pageLength: 1,
    setValue: {
        onSuccess(data) {
        },
        onError(error) {
        }
    },
})
function updateCreateNewPassword() {
  const requestName = requestList.data[0].name;
  if (!requestName) {
    console.error("Request document name is not available");
    return;
  }
  requestList.setValue.submit({
    name: requestName,
    create_new_password: createNewPassword,
    subscribe_to_text_messages: subscribeToTextMmessages
  });
}
// ======================================

const validatePasswords = () => {
  passwordError.value = enterPassword.value === confirmPassword.value ? '' : 'Passwords do not match';
  disabled.value = enterPassword.value === '' || confirmPassword.value === '' || passwordError.value !== '';
};

const confirmData = () => {
  if (enterPassword.value === confirmPassword.value) {
    // Handle submission logic here
    updatePassword()
  } else {
    passwordError.value = 'Passwords do not match';
  }
};

// مراقبة تغييرات الحقلين
watch([enterPassword, confirmPassword], validatePasswords);
</script>

<style scoped>
/* Custom styles if needed */
</style>