<script setup>
import { Head } from '@inertiajs/vue3';
import { useForm } from '@inertiajs/vue3';
import DashboardLayout from '@/Layouts/DashboardLayout.vue';

const props = defineProps({ user: Object });

const profileForm = useForm({
  first_name: props.user.first_name,
  last_name: props.user.last_name,
  email: props.user.email,
});

const passwordForm = useForm({
  current_password: '',
  password: '',
  password_confirmation: '',
});

const updateProfile = () => {
  profileForm.put('/storesight/dashboard/account');
};

const updatePassword = () => {
  passwordForm.put('/storesight/dashboard/account/password', {
    onSuccess: () => passwordForm.reset(),
  });
};
</script>

<template>
  <DashboardLayout>
    <Head title="Account Settings" />
    <h1 class="text-2xl font-bold text-gray-900 mb-6">Account Settings</h1>

    <!-- Profile -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 mb-6">
      <h2 class="text-lg font-semibold text-gray-900 mb-4">Profile</h2>
      <form @submit.prevent="updateProfile" class="space-y-5">
        <div class="grid grid-cols-2 gap-4">
          <div>
            <label class="block text-sm font-medium text-gray-700 mb-1">First Name</label>
            <input v-model="profileForm.first_name" type="text" class="w-full px-4 py-3 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-primary focus:border-primary outline-none">
            <p v-if="profileForm.errors.first_name" class="text-red-500 text-xs mt-1">{{ profileForm.errors.first_name }}</p>
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-700 mb-1">Last Name</label>
            <input v-model="profileForm.last_name" type="text" class="w-full px-4 py-3 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-primary focus:border-primary outline-none">
            <p v-if="profileForm.errors.last_name" class="text-red-500 text-xs mt-1">{{ profileForm.errors.last_name }}</p>
          </div>
        </div>
        <div>
          <label class="block text-sm font-medium text-gray-700 mb-1">Email</label>
          <input v-model="profileForm.email" type="email" class="w-full px-4 py-3 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-primary focus:border-primary outline-none">
          <p v-if="profileForm.errors.email" class="text-red-500 text-xs mt-1">{{ profileForm.errors.email }}</p>
        </div>
        <button type="submit" :disabled="profileForm.processing" class="bg-primary text-white font-semibold px-6 py-2.5 rounded-lg hover:bg-primary-dark transition text-sm">Save Changes</button>
      </form>
    </div>

    <!-- Password -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 mb-6">
      <h2 class="text-lg font-semibold text-gray-900 mb-4">Change Password</h2>
      <form @submit.prevent="updatePassword" class="space-y-5">
        <div>
          <label class="block text-sm font-medium text-gray-700 mb-1">Current Password</label>
          <input v-model="passwordForm.current_password" type="password" class="w-full px-4 py-3 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-primary focus:border-primary outline-none">
          <p v-if="passwordForm.errors.current_password" class="text-red-500 text-xs mt-1">{{ passwordForm.errors.current_password }}</p>
        </div>
        <div>
          <label class="block text-sm font-medium text-gray-700 mb-1">New Password</label>
          <input v-model="passwordForm.password" type="password" class="w-full px-4 py-3 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-primary focus:border-primary outline-none">
          <p v-if="passwordForm.errors.password" class="text-red-500 text-xs mt-1">{{ passwordForm.errors.password }}</p>
        </div>
        <div>
          <label class="block text-sm font-medium text-gray-700 mb-1">Confirm New Password</label>
          <input v-model="passwordForm.password_confirmation" type="password" class="w-full px-4 py-3 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-primary focus:border-primary outline-none">
        </div>
        <button type="submit" :disabled="passwordForm.processing" class="bg-primary text-white font-semibold px-6 py-2.5 rounded-lg hover:bg-primary-dark transition text-sm">Update Password</button>
      </form>
    </div>

    <!-- Billing -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
      <h2 class="text-lg font-semibold text-gray-900 mb-4">Billing</h2>
      <div class="space-y-4">
        <div class="flex justify-between items-center py-3 border-b border-gray-100">
          <span class="text-sm text-gray-500">Current Plan</span>
          <span class="text-sm font-medium text-gray-900">&pound;29/month</span>
        </div>
        <div class="flex justify-between items-center py-3 border-b border-gray-100">
          <span class="text-sm text-gray-500">Payment Method</span>
          <span class="text-sm text-gray-600">Managed via Stripe</span>
        </div>
      </div>
      <div class="mt-6 pt-6 border-t border-gray-200">
        <h3 class="text-sm font-medium text-red-600 mb-2">Danger Zone</h3>
        <p class="text-sm text-gray-500 mb-3">Cancel your subscription. Your access will continue until the end of your current billing period.</p>
        <button class="text-sm text-red-600 border border-red-200 px-4 py-2 rounded-lg hover:bg-red-50 transition">Cancel Subscription</button>
      </div>
    </div>
  </DashboardLayout>
</template>
