<template>
    <div>
        <v-layout row wrap>
            <v-flex xs3 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 xs3 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" v-on="on" hide-details readonly outline
                            label="Tanggal Kontrak"></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 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" hide-details
                            label="Tanggal Mulai Kontrak" 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" hide-details
                            label="Tanggal Akhir Kontrak" 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-layout>

        <v-layout row wrap>
            <v-flex xs2 pa-1>
                <v-text-field v-model="form_kontrak.durasi_cicilan" hide-details outline placeholder="1"
                    label="Durasi Kontrak (Bulan)" readonly></v-text-field>
            </v-flex>
            <v-flex xs2 pa-1>
                <v-text-field v-model="form_kontrak.jumlah_cicilan" hide-details placeholder="1" outline
                    label="Jumlah Cicilan"></v-text-field>
            </v-flex>
            <v-flex xs2 pa-1>
                <v-text-field v-model="form_kontrak.tanggalBayar_cicilan" hide-details placeholder="1" outline
                    label="Tanggal Bayar Cicilan"></v-text-field>
            </v-flex>
            <v-flex xs2 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-if="form_kontrak.type_downpayment == 'persen'" label="Jumlah Uang Muka" hide-details
                    v-model="form_kontrak.besarNilai_downpayment" placeholder="0" suffix="%" outline></v-text-field>
                <v-text-field v-else label="Jumlah Uang Muka" hide-details placeholder="0" prefix="Rp." outline
                    v-model="form_kontrak.besarNilai_downpayment"></v-text-field>
            </v-flex>
        </v-layout>
    </div>
</template>

<script>
module.exports = {
    data() {
        return {
            menuKontrakDate: false,
            menuKontrakStartdate: false,
            menuKontrakEnddate: false,
        };
    },
    computed: {
        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(startdate);
            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.jumlah_downpayment = 0;
            }
        }
    },
    watch: {

    }
}
</script>
