<template>
<v-dialog v-model="dialog_editro" persistent width="70vw">
    <v-card>
        <v-card-title class="headline grey lighten-2">EDIT 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" 
                        ></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="No Order Jasa" placeholder="nomor order jasa"
                    ></v-text-field>
                </v-flex>
            </v-layout>

            <!-- second row -->
            <v-layout row wrap 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 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="changeItemJasa()"
                        :disabled="form_rojasa.branchcode == '' || form_rojasa.supplierID == ''"
                    >TAMBAH ITEM</v-btn>
                </v-flex>
            </v-layout>

            <!-- table detail item -->
            <v-layout row wrap 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 class="mb-0" v-if="props.item.inspeksi.kesimpulan == 'accept'">
                                        Diterima
                                    </p>
                                    <p class="mb-0" v-if="props.item.inspeksi.kesimpulan == 'reject'">
                                        Ditolak <br>
                                        {{ props.item.inspeksi.catatan_kesimpulan }}
                                    </p>
                                    <p class="mb-0" v-if="props.item.inspeksi.kesimpulan == 'acceptnote'">
                                        Diterima dgn 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="batalEdit()" color="error" class="white--text">Batal</v-btn>
            <v-btn @click="simpanEdit()" 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_editro: {
            get() {
                return this.$store.state.roservice.dialog_editro
            },
            set(val) {
                this.$store.commit("roservice/update_dialog_editro", 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)
            }
        },
        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 new_form = {
                ...oldform,
                reference: "",
                typekontrak: "",
                startdate: "",
                enddate: "",
                usedcount: "0 / 0",
                catatan: ""
            }

            this.form_rojasa = new_form
            this.data_rojasa = []
        },
        changeItemJasa() {
            this.$store.dispatch("roservice/lookupOrderJasa", 'PO')
            this.$store.commit("roservice/update_dialog_pickjasa", true)
        },
        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)
        },
        simpanEdit() {
            if (this.data_rojasa.length <= 0) {
                let snac = {color: 'warning', msg: "item tidak boleh kosong", state: true}
                this.$store.commit("roservice/update_snackbar", snac)
                return
            }

            if (!this.loading_process) {
                this.$store.dispatch("roservice/saveEditReceiveOrder")
            } else {
                let snac = {color: 'warning', msg: "proses sedang berjalan", state: true}
                this.$store.commit("roservice/update_snackbar", snac)
            }
        },
        batalEdit() {
            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 = []
            this.dialog_editro = false
        }
    },
}
</script>