<template>
<v-dialog v-model="dialog_formro" persistent width="70vw">
    <v-card>
        <v-card-title class="headline grey lighten-2">FORM RO JASA</v-card-title>
        <v-card-text style="max-height: 80vh; overflow-y: auto;">
            <!-- first row -->
            <v-layout row wrap>
                <v-flex xs3>
                    <v-menu
                        v-model="menuROdate" 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="formattedRODate" label="Tanggal RO"
                                hide-details readonly v-on="on" outline
                            ></v-text-field>
                        </template>
                        <v-date-picker
                            v-model="form_rojasa.rodate" no-title 
                            @input="menuROdate = false"
                        ></v-date-picker>
                    </v-menu>
                </v-flex>
                <v-flex xs3 pl-2>
                    <v-autocomplete
                        :items="list_branch" hide-details outline 
                        v-model="form_rojasa.branchcode" label="Cabang"
                        item-text="M_BranchName" item-value="M_BranchCode"
                    ></v-autocomplete>
                </v-flex>
                <v-flex xs3 pl-2>
                    <v-autocomplete
                        :items="list_supplier" hide-details outline 
                        v-model="form_rojasa.supplierID" label="Vendor"
                        item-text="SupplierName" item-value="SupplierID"
                        @input="changeSupplier(form_rojasa)"
                    ></v-autocomplete>
                </v-flex>
                <v-flex xs3 pl-2>
                    <v-text-field
                        v-model="form_rojasa.orderjasano" hide-details outline 
                        label="Referensi" placeholder="Nomor Delivery / Supplier Invoice"
                    ></v-text-field>
                </v-flex>
            </v-layout>

            <!-- second row -->
            <v-layout row wrap class="mt-2">
                <v-flex xs3>
                    <v-text-field
                        v-model="form_rojasa.reference" hide-details outline 
                        label="No PO" placeholder="purchase order" readonly
                    ></v-text-field>
                </v-flex>
                <v-flex xs3 pl-2>
                    <v-text-field
                        v-model="form_rojasa.typekontrak" hide-details outline 
                        label="Type Kontrak" placeholder="contract type" readonly
                    ></v-text-field>
                </v-flex>
                <v-flex xs2 pl-2>
                    <v-text-field
                        v-model="form_rojasa.startdate" hide-details outline 
                        label="Periode" placeholder="contract periode" readonly
                    ></v-text-field>
                </v-flex>
                <v-flex xs2 pl-2>
                    <v-text-field
                        v-model="form_rojasa.enddate" hide-details outline 
                        label="Periode" placeholder="contract periode" readonly
                    ></v-text-field>
                </v-flex>
                <v-flex xs2 pl-2>
                    <v-text-field
                        v-model="form_rojasa.usedcount" hide-details outline 
                        label="Terpakai / Sisa Kontrak" placeholder="0 / 0" readonly
                    ></v-text-field>
                </v-flex>
            </v-layout>

            <!-- third row -->
            <v-layout row wrap class="mt-2">
                <v-flex xs12>
                    <v-textarea
                        v-model="form_rojasa.catatan" auto-grow 
                        rows="1" outline label="Catatan" hide-details
                    ></v-textarea>
                </v-flex>
                <v-flex xs3 mt-2>
                    <v-btn 
                        block color="info" @click="selectOrderJasa()"
                        :disabled="form_rojasa.branchcode == '' || form_rojasa.supplierID == ''"
                    >TAMBAH ITEM</v-btn>
                </v-flex>
            </v-layout>

            <v-layout row wrap class="mt-3">
                <v-flex xs12 v-if="data_rojasa.length > 0">
                    <v-data-table
                        :headers="headers" :items="data_rojasa" :expand="expand"
                        class="elevation-1" hide-actions
                    >
                        <template v-slot:items="props">
                        <tr>
                            <td class="text-xs-left pa-2">
                                {{ props.item.M_ItemDesc }}
                                <v-chip label small>{{ props.item.M_ItemCode }}</v-chip>
                            </td>
                            <td class="text-xs-center pa-2" >
                                {{ props.item.qty }}
                            </td>
                            <td class="text-xs-center pa-2">
                                {{ props.item.ItemUnitName }}
                            </td>
                            <td class="text-xs-center pa-2">
                                <div v-if="props.item.inspeksi">
                                    <p v-if="props.item.inspeksi.kesimpulan == 'accept'" class="mb-0">
                                        Diterima
                                    </p>
                                    <p v-else-if="props.item.inspeksi.kesimpulan == 'reject'" class="mb-0">
                                        Ditolak <br>
                                        {{ props.item.inspeksi.catatan_kesimpulan }}
                                    </p>
                                    <p v-else class="mb-0">
                                        Diterima dengan catatan <br>
                                        {{ props.item.inspeksi.catatan_kesimpulan }}
                                    </p>
                                </div>
                            </td>
                            <td class="text-xs-center pa-2">
                                <v-tooltip bottom pr-2>
                                    <template v-slot:activator="{ on }">
                                        <v-btn
                                            color="green" dark @click="openInspeksi(props.item)" 
                                            style="min-width: 40px; margin: 0;" small v-on="on"
                                        >
                                            <v-icon small>note_add</v-icon>
                                        </v-btn>
                                    </template>
                                    <span>Inspeksi Jasa</span>
                                </v-tooltip>

                                <v-tooltip bottom>
                                    <template v-slot:activator="{ on }">
                                        <v-btn
                                            color="warning" dark @click="cancelOrder(props.item)" 
                                            style="min-width: 40px; margin: 0;" small v-on="on"
                                        >
                                            <v-icon small>cancel</v-icon>
                                        </v-btn>
                                    </template>
                                    <span>Batal Order</span>
                                </v-tooltip>
                            </td>
                        </tr>
                        </template>
                    </v-data-table>
                </v-flex>
            </v-layout>
        </v-card-text>
        <v-card-actions>
            <v-spacer></v-spacer>
            <v-btn @click="batalROJasa()" color="error" class="white--text">Batal</v-btn>
            <v-btn @click="simpanROJasa()" color="primary" class="white--text">Simpan</v-btn>
        </v-card-actions>
    </v-card>
</v-dialog>
</template>

<script>
module.exports = {
    data() {
        return {
            menuROdate: false,
            expand: false,
            headers: [
                {
                    text: "ITEM JASA", align: "center", sortable: false,
                    value: "nopo", width: "40%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "QTY", align: "center", sortable: false,
                    value: "type", width: "10%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "UNIT ITEM", align: "center", sortable: false,
                    value: "item", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "INSPEKSI", align: "center", sortable: false,
                    value: "inspec", width: "20%", class: "pa-1 blue lighten-3 white--text",
                },
                {
                    text: "AKSI", align: "center", sortable: false,
                    value: "act", width: "15%", class: "pa-1 blue lighten-3 white--text",
                },
            ]
        }
    },
    computed: {
        dialog_formro: {
            get() {
                return this.$store.state.roservice.dialog_formro
            },
            set(val) {
                this.$store.commit("roservice/update_dialog_formro", val)
            }
        },
        form_rojasa: {
            get() {
                return this.$store.state.roservice.form_rojasa
            },
            set(val) {
                this.$store.commit("roservice/update_form_rojasa", val)
            }
        },
        data_rojasa: {
            get() {
                return this.$store.state.roservice.data_rojasa
            },
            set(val) {
                this.$store.commit("roservice/update_data_rojasa", val)
            }
        },
        formattedRODate() {
            return this.formatDate(this.form_rojasa.rodate)
        },
        list_supplier() {
            return this.$store.state.roservice.list_supplier
        },
        list_branch() {
            return this.$store.state.roservice.list_branch
        },
        loading_process() {
            return this.$store.state.roservice.loading_process
        }
    },
    methods: {
        formatDate(date) {
            if (!date) return null;
            const [year, month, day] = date.split('-');
            return `${day}-${month}-${year}`;
        },
        changeSupplier(oldform) {
            let newform = {
                ...oldform,
                reference: "",
                typekontrak: "",
                startdate: "",
                enddate: "",
                usedcount: "0 / 0",
                catatan: ""
            }

            this.form_rojasa = newform
            this.data_rojasa = []
        },
        selectOrderJasa() {
            this.$store.dispatch("roservice/lookupOrderJasa", 'PO')
            this.$store.commit("roservice/update_dialog_pickjasa", true)
        },
        openInspeksi(item) {
            this.$store.commit("roservice/update_selected_jasainspek", item)
            this.$store.commit("roservice/update_dialog_inspeksi", true)
        },
        cancelOrder(item) {
            let current = JSON.parse(JSON.stringify(this.data_rojasa))
            let neulist = current.filter((obj) => obj.PODetailID == item.PODetailID)
            this.data_rojasa = neulist
        },
        batalROJasa() {
            this.dialog_formro = false
            this.data_rojasa = []
        },
        simpanROJasa() {
            if (this.data_rojasa.length <= 0) {
                let snac = {color: 'warning', msg: "item tidak boleh kosong", state: true}
                this.$store.commit("roservice/update_snackbar", snac)
            }

            if (!this.loading_process) {
                this.$store.dispatch("roservice/saveReceiveOrder")
            } else {
                let snac = {color: 'warning', msg: "proses sedang berjalan", state: true}
                this.$store.commit("roservice/update_snackbar", snac)
            }
        }
    },
    watch: {
        dialog_formro(val) {
            if (val) {
                this.$nextTick(() => {
                    this.form_rojasa = {
                        rodate: moment(new Date()).format('YYYY-MM-DD'),
                        supplierID: 0,
                        orderjasano: "",
                        branchcode: one_user()['M_BranchCode'],
                        reference: "",
                        typekontrak: "",
                        startdate: "",
                        enddate: "",
                        usedcount: "0 / 0",
                        catatan: ""
                    }
                    this.data_rojasa = []
                })
            }
        }
    }
}
</script>