<template>
<v-dialog v-model="dialog_confirm_ro" persistent width="70vw">
    <v-card>
        <v-card-title class="headline grey lighten-2">FORM RO JASA</v-card-title>
        <v-card-text>
            <!-- 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" readonly
                        ></v-date-picker>
                    </v-menu>
                </v-flex>
                <v-flex xs3 pl-2>
                    <v-autocomplete
                        :items="list_branch" hide-details outline readonly
                        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 readonly
                        v-model="form_rojasa.supplierID" label="Vendor"
                        item-text="SupplierName" item-value="SupplierID"
                    ></v-autocomplete>
                </v-flex>
                <v-flex xs3 pl-2>
                    <v-text-field
                        v-model="form_rojasa.orderjasano" hide-details outline 
                        label="No Order Jasa" placeholder="nomor order jasa" readonly
                    ></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="Reference" 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 mt-2>
                    <v-textarea
                        v-model="form_rojasa.catatan" auto-grow readonly
                        rows="1" outline label="Catatan" hide-details
                    ></v-textarea>
                </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-center 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>
                            </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 
                v-if="selected_rojasa.ReceiveOrderPoConfirmed != 'Y'" 
                @click="confirmROJasa()" color="primary" class="white--text"
            >Konfirmasi</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_confirm_ro: {
            get() {
                return this.$store.state.roservice.dialog_confirm_ro
            },
            set(val) {
                this.$store.commit("roservice/update_dialog_confirm_ro", 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)
            }
        },
        selected_rojasa: {
            get() {
                return this.$store.state.roservice.selected_rojasa
            },
            set(val) {
                this.$store.commit("roservice/update_selected_rojasa", val)
            }
        },
        preview_attachment: {
            get() {
                return this.$store.state.roservice.preview_attachment
            },
            set(val) {
                this.$store.commit("roservice/update_preview_attachment", 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}`;
        },
        openInspeksi(item) {
            if (item.inspeksi) {
                this.$store.commit("roservice/update_form_inspeksi", item.inspeksi)
            }
            this.$store.commit("roservice/update_selected_jasainspek", item)
            this.$store.commit("roservice/update_dialog_inspeksi", true)
        },
        batalROJasa() {
            this.selected_rojasa = {}
            this.data_rojasa = []
            this.dialog_confirm_ro = false
        },
        confirmROJasa() {
            let attachment  = this.preview_attachment;
            if (attachment.length <= 0) {
                this.showSnackbar("belum upload nota/attachment");
                return;
            }

            let detail = this.data_rojasa;
            for (const element of detail) {
                if (!element.inspeksi) {
                    this.showSnackbar("belum mengisi form inspeksi jasa");
                    return;
                }
            }

            if (this.loading_process) {
                this.showSnackbar("proses sedang berjalan");
                return;
            } 

            this.$store.dispatch("roservice/confirmReceiveOrder");
        },
        showSnackbar(msg) {
            let snac = {color: 'warning', msg: msg, state: true}
            this.$store.commit("roservice/update_snackbar", snac)
        }
    },
    watch: {
        dialog_confirm_ro(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>