<script setup>
import { watch } from 'vue'
import { Inertia } from '@inertiajs/inertia'
import { useForm } from '@inertiajs/inertia-vue3'
import { useFormErrorReset } from '@/components/useFormErrorReset'
import AppInputText from '@/components/AppInputText.vue'
import AppDropdown from '@/components/AppDropdown.vue'
import DashboardLayout from '@/layouts/DashboardLayout.vue'
import { InTable, OutTable } from './TableHeader'

defineProps({
  entryTransactions: Object,
  outTransactions: Object,
  typeVehicles: Object,
})

const form = useForm({
  plat_number: null,
  entry_transaction_id: null,
  type_vehicle_id: null,
})

useFormErrorReset(form)

watch(
  () => form.plat_number,
  () => {
    Inertia.reload({ only: ['typeVehicles'], data: { plat_number: form.plat_number } })
  }
)

const submit = () => {
  form.post(route('transactions.store'), { onSuccess: () => form.reset() })
}
</script>
<template>
  <DashboardLayout>
    <div class="grid">
      <div class="col-12 md:col-6">
        <Card>
          <template #title> Tambah Transaksi Keluar </template>
          <template #content>
            <div class="grid">
              <div class="col-12 md:col-6">
                <AppInputText
                  v-model="form.plat_number"
                  label="Plat Kendaraan"
                  placeholder="plat kendaraan"
                  :error="form.errors.plat_number"
                />
              </div>
              <div class="col-12 md:col-6">
                <AppInputText
                  v-model="form.entry_transaction_id"
                  label="Nomor Transaksi"
                  placeholder="nomor transaksi"
                  :error="form.errors.entry_transaction_id"
                />
              </div>
              <div class="col-12 md:col-6">
                <AppDropdown
                  v-model="form.type_vehicle_id"
                  label="Jenis Kendaraan"
                  placeholder="jenis kendaraan"
                  :options="typeVehicles"
                  :error="form.errors.type_vehicle_id"
                />
              </div>
            </div>
          </template>
          <template #footer>
            <div class="flex flex-column md:flex-row justify-content-end">
              <Button
                label="Simpan"
                icon="pi pi-check"
                class="p-button-outlined"
                :disabled="form.processing"
                @click="submit"
              />
            </div>
          </template>
        </Card>
      </div>
      <div class="col-12 md:col-6">
        <div class="grid">
          <div class="col-12">
            <Card>
              <template #title> Kendaraan Masuk </template>
              <template #content>
                <DataTable
                  responsiveLayout="scroll"
                  columnResizeMode="expand"
                  :value="entryTransactions"
                  :rowHover="true"
                  :stripedRows="true"
                >
                  <template #header>
                    <span style="color: var(--primary-color)">Maksimal ditampilkan :</span> 10
                    <br />
                    <span style="color: var(--primary-color)">Ditampilkan :</span>
                    {{ Object.keys(entryTransactions).length }}
                  </template>

                  <Column
                    v-for="inTable in InTable"
                    :field="inTable.field"
                    :header="inTable.header"
                    :key="inTable.field"
                  />
                </DataTable>
              </template>
            </Card>
          </div>
          <div class="col-12">
            <Card>
              <template #title> Kendaraan Keluar </template>
              <template #content>
                <DataTable
                  responsiveLayout="scroll"
                  columnResizeMode="expand"
                  :value="outTransactions"
                  :rowHover="true"
                  :stripedRows="true"
                >
                  <template #header>
                    <span style="color: var(--red-600)">Maksimal ditampilkan :</span> 10
                    <br />
                    <span style="color: var(--red-600)">Ditampilkan :</span>
                    {{ Object.keys(outTransactions).length }}
                  </template>

                  <Column
                    v-for="outTable in OutTable"
                    :field="outTable.field"
                    :header="outTable.header"
                    :key="outTable.field"
                  />
                </DataTable>
              </template>
            </Card>
          </div>
        </div>
      </div>
    </div>
  </DashboardLayout>
</template>
