<script setup>
import { useForm, Head } from '@inertiajs/inertia-vue3'

import AppButtonCreate from '@/components/AppButtonCreate.vue'
import AppTextInput from '@/components/AppTextInput.vue'
import AppSelectInput from '@/components/AppSelectInput.vue'
import DefaultLayout from '@/layouts/DefaultLayout.vue'

defineProps({
  roles: Array,
  outlets: Array,
})

const form = useForm({
  name: '',
  phone: '',
  email: '',
  address: '',
  gender: '',
  outlet_id: '',
  role_id: '',
})

const submit = () => {
  form.post(route('users.store'))
}
</script>

<template>
  <Head title="Tambah User" />

  <DefaultLayout>
    <CRow>
      <CCol md="8">
        <CCard color="light" class="border-light">
          <CForm @submit.prevent="submit">
            <CRow class="p-4">
              <CCol md="6" class="mb-4">
                <AppTextInput label="Nama" placeholder="nama" :error="form.errors.name" v-model="form.name" />
              </CCol>

              <CCol md="6" class="mb-4">
                <AppTextInput label="Nomor HP" placeholder="nomor hp" :error="form.errors.phone" v-model="form.phone" />
              </CCol>

              <CCol md="6" class="mb-4">
                <AppTextInput label="Email" placeholder="email" :error="form.errors.email" v-model="form.email" />
              </CCol>

              <CCol md="6" class="mb-4">
                <AppTextInput
                  label="Address"
                  placeholder="address"
                  :error="form.errors.address"
                  v-model="form.address"
                />
              </CCol>

              <CCol md="6" class="mb-4">
                <AppSelectInput label="Jenis Kelamin" :error="form.errors.gender" v-model="form.gender">
                  <option value="1">Perempuan</option>
                  <option value="2">Laki-laki</option>
                </AppSelectInput>
              </CCol>

              <CCol md="6" class="mb-4">
                <AppSelectInput label="Outlet" :error="form.errors.outlet_id" v-model="form.outlet_id">
                  <option v-for="outlet in outlets" key="outlet.id" :value="outlet.id">{{ outlet.name }}</option>
                </AppSelectInput>
              </CCol>

              <CCol md="6">
                <AppSelectInput label="Hak Akses" :error="form.errors.role_id" v-model="form.role_id">
                  <option v-for="role in roles" key="role.id" :value="role.id">{{ role.name }}</option>
                </AppSelectInput>
              </CCol>
            </CRow>

            <CCardFooter class="d-flex justify-content-end">
              <AppButtonCreate :disabled="form.processing">Tambah User</AppButtonCreate>
            </CCardFooter>
          </CForm>
        </CCard>
      </CCol>
    </CRow>
  </DefaultLayout>
</template>
