<template>
<v-dialog v-model="dialog_create_contract" persistent width="80vw">
    <v-card style="max-height: 80vh; display: flex; flex-direction: column;">
        <v-card-title class="headline grey lighten-2">BUAT KONTRAK BARU</v-card-title>
        <v-card-text style="flex: 1 1 auto; overflow: visible;">
            <!-- field forms -->
            <v-layout row wrap>
                <v-flex xs4 pa-1>
                    <v-text-field
                        v-model="form_kontrak.nama_kontrak" hide-details outline
                        label="Nama Kontrak" placeholder="masukan nama kontrak"
                    ></v-text-field>
                </v-flex>
                <v-flex xs4 pa-1>
                    <v-menu
                        v-model="menuKontrakDate" offset-y lazy min-width="290px"
                        transition="scale-transition" max-width="290px" full-width
                        :nudge-right="40" :close-on-content-click="false"
                    >
                        <template v-slot:activator="{ on }">
                            <v-text-field
                                v-model="formattedDateKontrak" label="Tanggal Kontrak"
                                hide-details readonly outline v-on="on"
                            ></v-text-field>
                        </template>
                        <v-date-picker v-model="form_kontrak.tanggal_kontrak"
                            no-title @input="menuKontrakDate = false"
                        ></v-date-picker>
                    </v-menu>
                </v-flex>
                <v-flex xs4 pa-1>
                    <v-autocomplete
                        v-model="form_kontrak.supplierID_kontrak" hide-details
                        :items="supplier_list" label="Supplier" outline
                        item-text="SupplierName" item-value="SupplierID"
                    ></v-autocomplete>
                </v-flex>
            </v-layout>

            <v-layout row wrap>
                <v-flex xs3 pa-1>
                    <v-menu
                        v-model="menuKontrakStartdate" offset-y lazy min-width="290px"
                        transition="scale-transition" max-width="290px" full-width
                        :nudge-right="40" :close-on-content-click="false"
                    >
                        <template v-slot:activator="{ on }">
                            <v-text-field
                                v-model="formattedStartdateKontrak" outline v-on="on"
                                label="Tanggal Awal Kontrak" hide-details readonly
                            ></v-text-field>
                        </template>
                        <v-date-picker v-model="form_kontrak.tanggalAwal_kontrak"
                            no-title @input="menuKontrakStartdate = false"
                        ></v-date-picker>
                    </v-menu>
                </v-flex>
                <v-flex xs3 pa-1>
                    <v-menu
                        v-model="menuKontrakEnddate" offset-y lazy min-width="290px"
                        transition="scale-transition" max-width="290px" full-width
                        :nudge-right="40" :close-on-content-click="false"
                    >
                        <template v-slot:activator="{ on }">
                            <v-text-field
                                v-model="formattedEnddateKontrak" outline v-on="on"
                                label="Tanggal Akhir Kontrak" hide-details readonly
                            ></v-text-field>
                        </template>
                        <v-date-picker v-model="form_kontrak.tanggalAkhir_kontrak"
                            no-title @input="menuKontrakEnddate = false"
                        ></v-date-picker>
                    </v-menu>
                </v-flex>
                <v-flex xs2 pa-1>
                    <v-text-field
                        v-model="form_kontrak.durasi_cicilan" hide-details outline
                        label="Durasi Kontrak (Bulan)" placeholder="1" readonly
                    ></v-text-field>
                </v-flex>
                <v-flex xs2 pa-1>
                    <v-text-field
                        v-model="form_kontrak.jumlah_cicilan" hide-details
                        label="Jumlah Cicilan" placeholder="1" outline
                    ></v-text-field>
                </v-flex>
                <v-flex xs2 pa-1>
                    <v-text-field
                        v-model="form_kontrak.tanggalBayar_cicilan" hide-details
                        label="Tanggal Bayar Cicilan" placeholder="1" outline
                    ></v-text-field>
                </v-flex>
            </v-layout>

            <v-layout row wrap>
                <v-flex xs4 pa-1>
                    <v-text-field
                        v-model="form_kontrak.besarNilai_cicilan" hide-details
                        label="Besar Bayar Cicilan" placeholder="0" prefix="Rp." outline
                    ></v-text-field>
                </v-flex>
                <v-flex xs1 pa-1>
                    <v-autocomplete
                        :items="['nominal', 'persen']" label="Tipe Nilai"
                        v-model="form_kontrak.type_downpayment" hide-details
                        @input="changeTypeAmount('DP')" outline
                    ></v-autocomplete>
                </v-flex>
                <v-flex xs3 pa-1>
                    <v-text-field
                        v-model="form_kontrak.besarNilai_downpayment" hide-details
                        label="Jumlah Uang Muka" placeholder="0" suffix="%" outline
                    ></v-text-field>
                </v-flex>
                <v-flex xs1 pa-1>
                    <v-autocomplete
                        :items="['nominal', 'persen']" label="Tipe Nilai"
                        v-model="form_kontrak.type_pajak" hide-details
                        @input="changeTypeAmount('tax')" outline
                    ></v-autocomplete>
                </v-flex>
                <v-flex xs3 pa-1>
                    <v-text-field
                        v-model="form_kontrak.besarNilai_pajak" hide-details
                        label="Jumlah Pajak" placeholder="0" suffix="%" outline
                    ></v-text-field>
                </v-flex>
            </v-layout>

            <!-- table item -->
            <v-layout row wrap class="mt-2">
                <v-flex xs3 pa-1>
                    <v-btn block color="info">TAMBAH ITEM</v-btn>
                </v-flex>
                <v-flex xs12 pa-1>
                    <div style="max-height: 35vh; overflow-y: auto;">
                        <v-data-table
                            :headers="headers" :items="[]"
                            item-key="M_ItemID" class="elevation-1" hide-actions
                        >
                            <template v-slot:items="props">
                                <tr>
                                    <td class="text-xs-center pa-1"></td>
                                    <td class="text-xs-center pa-1"></td>
                                    <td class="text-xs-center pa-1"></td>
                                    <td class="text-xs-center pa-1"></td>
                                    <td class="text-xs-center pa-1"></td>
                                    <td class="text-xs-center pa-1"></td>
                                </tr>
                            </template>
                        </v-data-table>
                    </div>
                </v-flex>
            </v-layout>
        </v-card-text>
        <v-card-actions>
            <v-spacer></v-spacer>
            <v-btn @click="" color="error" class="white--text">Batal</v-btn>
            <v-btn @click="" color="primary" class="white--text">Simpan</v-btn>
        </v-card-actions>
    </v-card>
</v-dialog>
</template>

<script>
module.exports = {
    data() {
        return {
            menuKontrakDate: false,
            menuKontrakStartdate: false,
            menuKontrakEnddate: false,
            headers: [
                {
                    text: "NO", align: "center", sortable: false, value: "no",
                    width: "5%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "ITEM", align: "center", sortable: false, value: "item",
                    width: "25%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "JUMLAH", align: "center", sortable: false, value: "qty",
                    width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "HARGA", align: "center", sortable: false, value: "price",
                    width: "25%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "TOTAL", align: "center", sortable: false, value: "total",
                    width: "20%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "AKSI", align: "center", sortable: false, value: "act",
                    width: "10%", class: "pa-1 blue lighten-3 white--text",
                }
            ]
        };
    },
    computed: {
        dialog_create_contract: {
            get() {
                return this.$store.state.kontrakAsset.dialog_create_contract
            },
            set(val) {
                this.$store.commit("kontrakAsset/update_dialog_create_contract", val);
            }
        },
        supplier_list() {
            return [];
        },
        form_kontrak: {
            get() {
                return this.$store.state.kontrakAsset.form_kontrak
            },
            set(val) {
                this.$store.commit("kontrakAsset/update_form_kontrak", val);
            }
        },
        formattedDateKontrak() {
            return this.formatDate(this.form_kontrak.tanggal_kontrak);
        },
        formattedStartdateKontrak() {
            return this.formatDate(this.form_kontrak.tanggalAwal_kontrak);
        },
        formattedEnddateKontrak() {
            return this.formatDate(this.form_kontrak.tanggalAkhir_kontrak);
        }
    },
    methods: {
        formatDate(date) {
            if (!date) return null;
            const [year, month, day] = date.split('-');
            return `${day}-${month}-${year}`;
        },
        countMonths(startdate, enddate) {
            const start = new Date(stardate);
            const end = new Date(enddate);

            let months = (end.getFullYear() - start.getFullYear()) * 12 + (end.getMonth() - start.getMonth());
            if (end.getDate() > start.getDate()) {
                months += 1;
            }

            return Math.max(months, 1);
        },
        changeTypeAmount(name) {
            let form = this.form_kontrak;
            if (name == 'DP') {
                form_kontrak.jumlah_downpayment = 0;
            }

            if (name == 'tax') {
                form_kontrak.jumlah_pajak = 0;
            }
        }
    }
}
</script>
