<template>
    <v-layout>
        <template>

            <v-dialog v-model="dialogdeletealert" max-width="30%">
                <v-card>
                    <v-card-title class="headline grey lighten-2 pt-2 pb-2" primary-title>
                        Peringatan !
                    </v-card-title>
                    <v-card-text class="pt-2 pb-2">
                        <v-layout row>
                            <v-flex xs12 d-flex>
                                <v-layout row>
                                    <v-flex pb-1 xs12>
                                        <v-layout row>
                                            <v-flex pt-2 pr-2 xs12>
                                                {{msgalert}}
                                            </v-flex>
                                        </v-layout>
                                    </v-flex>
                                </v-layout>
                            </v-flex>
                        </v-layout>
                    </v-card-text>
                    <v-divider></v-divider>
                    <v-card-actions>
                        <v-spacer></v-spacer>
                        <v-btn color="primary" flat @click="dialogdeletealert = false">
                            Tutup
                        </v-btn>
                        <v-btn color="primary" flat @click="closeDeleteAlert()">
                            Yakin lah
                        </v-btn>
                    </v-card-actions>
                </v-card>
            </v-dialog>

            <v-dialog v-model="dialogdeliveryorder" persistent max-width="600px">
                <v-card>
                    <v-card-title>
                        <span class="headline">FORM SURAT JALAN RUJUKAN</span></v-card-title>
                    <v-card-text class="pt-0 pb-0">
                        <v-form ref="formdeliveryorder" v-model="valid" lazy-validation>
                            <v-layout wrap>
                                <v-flex xs12>
                                    <v-text-field label="Tanggal*" placeholder="dd-mm-yyyy" mask="##-##-####" v-model="xstartdate"></v-text-field>
                                    <p v-if="checkError('requiredate')" class="error pl-2 pr-2" style="color:#fff">Tanggal harus diisi</p>
                                </v-flex>
                                <v-flex xs12>
                                    <v-checkbox v-model="isexternal" label="External ?"></v-checkbox>
                                </v-flex>
                                <v-flex xs12>
                                    <v-layout>
                                        <v-flex xs6 pa-2>
                                            <v-autocomplete readonly label="Cabang Pengirim" v-model="branch" :items="xbranchs" :search-input.sync="search_branch" auto-select-first
                                                no-filter item-text="M_BranchName" return-object :loading="isLoading" no-data-text="Pilih Cabang">
                                                <template slot="item" slot-scope="{ item }">
                                                    <v-list-tile-content>
                                                        <v-list-tile-title v-text="item.M_BranchName"></v-list-tile-title>
                                                    </v-list-tile-content>
                                                </template>
                                            </v-autocomplete>
                                            <p v-if="checkError('requirebranch')" class="error pl-2 pr-2" style="color:#fff">Cabang harus dipilih</p>
                                        </v-flex>
                                        <v-flex xs6 pa-2>
                                            <v-autocomplete label="Tujuan*" v-model="destination" :items="xdestinations" :search-input.sync="search_destination"
                                                auto-select-first no-filter item-text="M_DestinationName" return-object
                                                :loading="isLoading" no-data-text="Pilih Tujuan">
                                                <template slot="item" slot-scope="{ item }">
                                                    <v-list-tile-content>
                                                        <v-list-tile-title v-text="item.M_DestinationName"></v-list-tile-title>
                                                    </v-list-tile-content>
                                                </template>
                                            </v-autocomplete>
                                            <p v-if="checkError('requiredestination')" class="error pl-2 pr-2" style="color:#fff">Tujuan harus dipilih</p>
                                        </v-flex>
                                    </v-layout>
                                </v-flex>
                                <v-flex xs12>
                                    <v-layout>
                                        <v-flex xs6 pa-2>
                                            <v-autocomplete :readonly="this.xid !== 0" label="Sample" v-model="sample" :items="xsamples" :search-input.sync="search_sample"
                                                auto-select-first no-filter item-text="T_SampleName" return-object :loading="isLoading"
                                                no-data-text="Pilih Sample">
                                                <template slot="item" slot-scope="{ item }">
                                                    <v-list-tile-content>
                                                        <v-list-tile-title v-text="item.T_SampleName"></v-list-tile-title>
                                                    </v-list-tile-content>
                                                </template>
                                            </v-autocomplete>
                                            <p v-if="checkError('requiresample')" class="error pl-2 pr-2" style="color:#fff">Sample harus dipilih</p>
                                        </v-flex>
                                        <v-flex xs6 pa-2>
                                            <v-autocomplete label="Kurir" v-model="courier" :items="xcouriers" :search-input.sync="search_courier" auto-select-first
                                                no-filter item-text="M_StaffName" return-object :loading="isLoading" no-data-text="Pilih Kurir">
                                                <template slot="item" slot-scope="{ item }">
                                                    <v-list-tile-content>
                                                        <v-list-tile-title v-text="item.M_StaffName"></v-list-tile-title>
                                                    </v-list-tile-content>
                                                </template>
                                            </v-autocomplete>
                                            <p v-if="checkError('requirecourier')" class="error pl-2 pr-2" style="color:#fff">Kurir harus dipilih</p>
                                        </v-flex>
                                    </v-layout>
                                    <v-flex xs12>
                                        <v-textarea v-model="xaddress" label="Alamat*"></v-textarea>
                                        <p v-if="checkError('requireaddress')" class="error pl-2 pr-2" style="color:#fff">Alamat hharus diisi</p>
                                    </v-flex>
                                    <v-flex xs12>
                                        <v-layout>
                                            <v-flex xs4 pa-2>
                                                <v-autocomplete label="Kota*" v-model="xautocity" :items="xautocitys" :search-input.sync="search_city" auto-select-first
                                                    no-filter item-text="M_CityName" return-object no-data-text="Cari Kota" small>
                                                    <template slot="item" slot-scope="{ item }">
                                                        <v-list-tile-content>
                                                            <v-list-tile-title v-text="item.M_CityName"></v-list-tile-title>
                                                        </v-list-tile-content>
                                                    </template>
                                                </v-autocomplete>
                                                <p v-if="checkError('requirecity')" class="error pl-2 pr-2" style="color:#fff">Kota harus dipilih</p>
                                            </v-flex>
                                            <v-flex xs4 pa-2>
                                                <v-autocomplete label="Kecamatan*" v-model="xautodistrict" :items="xautodistricts" :search-input.sync="search_district" auto-select-first
                                                    no-filter item-text="M_DistrictName" return-object no-data-text="Cari Kecamatan"
                                                    small>
                                                    <template slot="item" slot-scope="{ item }">
                                                        <v-list-tile-content>
                                                            <v-list-tile-title v-text="item.M_DistrictName"></v-list-tile-title>
                                                        </v-list-tile-content>
                                                    </template>
                                                </v-autocomplete>
                                                <p v-if="checkError('requiredistrict')" class="error pl-2 pr-2" style="color:#fff">Kecamatan harus dipilih</p>
                                            </v-flex>
                                            <v-flex xs4 pa-2>
                                                <v-autocomplete label="Kelurahan*" v-model="xautokelurahan" :items="xautokelurahans" :search-input.sync="search_kelurahan"
                                                    auto-select-first no-filter item-text="M_KelurahanName" return-object no-data-text="Cari Kelurahan">
                                                    <template slot="item" slot-scope="{ item }">
                                                        <v-list-tile-content>
                                                            <v-list-tile-title v-text="item.M_KelurahanName"></v-list-tile-title>
                                                        </v-list-tile-content>
                                                    </template>
                                                </v-autocomplete>
                                                <p v-if="checkError('requirekelurahan')" class="error pl-2 pr-2" style="color:#fff">Kelurahan harus dipilih</p>
                                            </v-flex>
                                        </v-layout>
                                    </v-flex>
                                    <v-flex xs12>
                                        <v-text-field label="UP" v-model="xup"></v-text-field>
                                    </v-flex>
                                    <v-flex>
                                        <p v-for="(xerror,idx) in xerrors" class="error pl-2 pr-2" style="color:#fff">{{xerror.msg}}</p>
                                    </v-flex>
                            </v-layout>
                    </v-card-text>
                    <v-card-actions>
                        <v-spacer></v-spacer>
                        <v-btn color="blue darken-1" flat @click="updateDialogFormDeliveryOrder()">Tutup</v-btn>
                        <v-btn v-if="xact === 'new'" color="blue darken-1" flat @click="saveFormDeliveryOrder()">Simpan</v-btn>
                        <v-btn v-if="xact === 'edit'" color="blue darken-1" flat @click="updateFormDeliveryOrder()">Simpan Perubahan</v-btn>
                    </v-card-actions>
                    </v-form>
                </v-card>
            </v-dialog>
        </template>


        <v-flex xs12>
            <v-card>
                <v-toolbar color="blue lighten-3" dark height="50px">
                    <v-toolbar-title>SURAT JALAN RUJUKAN</v-toolbar-title>
                    <v-spacer></v-spacer>
                </v-toolbar>
                <v-snackbar v-model="snackbar" :timeout="5000" :multi-line="false" :vertical="false" :top="true">
                    {{msgsnackbar}}
                    <v-btn flat @click="updateAlert_success(false)">
                        Tutup
                    </v-btn>
                </v-snackbar>
                <v-layout row style="background:#bbdefb;padding-top:3px;">
                <v-flex xs6 class="text-sm-left" pa-2>
                        <one-date-picker label="Tanggal" :date="init_date" @change="searchDeliveryOrder" :data="null" :max_date="null" class="mr-4"></one-date-picker>                     
                </v-flex>
                </v-layout>
                <v-divider></v-divider>
                <div>

                    <v-layout row class="scroll-container" style="max-height:600px;overflow: auto;">
                        <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                            <v-data-table :headers="headers" :items="vdeliveryorders" :loading="isLoading" hide-actions class="elevation-1">
                                <template slot="items" slot-scope="props">
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.T_RefDeliveryOrderDatex}}</td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.T_RefDeliveryOrderNumber}}</td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.isexternal}}</td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.destination}}</td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                    <v-icon v-if="props.item.T_RefDeliveryOrderIsConfirm === 'N'" style="color:red">clear</v-icon>
                                        <v-icon v-if="props.item.T_RefDeliveryOrderIsConfirm !== 'N'" style="color:green">check</v-icon></td>
                                    
                                </template>
                            </v-data-table>
                        </v-flex>
                    </v-layout>



                    <v-divider></v-divider>
                  <!--  <v-pagination style="margin-top:10px;margin-bottom:10px" v-model="curr_page" :length="xtotal_page"></v-pagination> -->
                </div>




            </v-card>
        </v-flex>
    </v-layout>
</template>

<style scoped>
    .searchbox .v-input.v-text-field .v-input__slot {
        min-height: 60px;
    }

    .searchbox .v-btn {
        min-height: 60px;
    }

    table.v-table tbody td,
    table.v-table tbody th {
        height: 40px;
    }

    table.v-table thead tr {
        height: 40px;
    }

    .textinput {
        -webkit-transition: width 0.4s ease-in-out;
        transition: width 0.4s ease-in-out;
        background-color: white;
        background-position: 10px 10px;
        background-repeat: no-repeat;
        padding-left: 40px;
        width: 100%;
        padding: 8px 10px;
        margin-bottom: 5px;
        box-sizing: border-box;
        border: 1px solid #607d8b;

    }

    .textinput:focus {
        width: 100%;
    }

    .textinput:focus::-webkit-input-placeholder {
        color: transparent;
    }

    .textinput:focus::-moz-placeholder {
        color: transparent;
    }

    .textinput:-moz-placeholder {
        color: transparent;
    }

    .boxoutline {
        color: red;
        border: 1px solid red;
        justify-content: center;
        height: 45px;
        line-height: 45px;
        padding-left: 10px;
        background: #ffffff;
        font-size: 14px;
        font-weight: 500;
        border-radius: 1px
    }

    .boxoutline:hover {
        background: rgba(0, 0, 0, 0.07) !important;
        font-size: 15px;
        font-weight: 700;
    }

    .boxsolid {
        color: #ffffff;
        border: 1px solid #ffffff;
        justify-content: center;
        height: 45px;
        line-height: 45px;
        padding-left: 10px;
        background: #f44336;
        font-size: 14px;
        font-weight: 500;
        border-radius: 1px
    }

    .boxsolid:hover {
        background: #f44336de;
        font-size: 15px;
        font-weight: 700;
    }

    .scroll-container {
        scroll-padding: 50px 0 0 50px;
    }

    ::-webkit-scrollbar {
        width: 7px;
    }

    /* this targets the default scrollbar (compulsory) */

    ::-webkit-scrollbar-track {
        background-color: #73baf3;
    }

    /* the new scrollbar will have a flat appearance with the set background color */

    ::-webkit-scrollbar-thumb {
        background-color: #2196f3;
    }

    /* this will style the thumb, ignoring the track */

    ::-webkit-scrollbar-button {
        background-color: #0079da;
    }

    /* optionally, you can style the top and the bottom buttons (left and right for horizontal bars) */

    ::-webkit-scrollbar-corner {
        background-color: black;
    }

    /* if both the vertical and the horizontal bars appear, then perhaps the right bottom corner also needs to be styled */
</style>

<script>
    module.exports = {
        data() {
            return {
                msgalertconfirmation: "Perubahan yang telah dilakukan belum disimpan dong !",
                dialogdeletealert: false,
                msgalert: "",
                valid: false,
                xid: 0,
                search_courier: "",
                items: [],
                xname: "",
                xcode: "",
                xnameRules: [
                    v => !!v || 'Nama harus diisi'
                ],
                courierRules: [
                    v => !!v || 'Instrument harus dipilih'
                ],
                name: "",
                isdefault: false,
                xdestination: "",
                xup: "",
                xaddress: "",
                scode: "",                
                search_branch: "",
                search_destination: "",
                search_sample: "",
                search_courier: "",
                search_testheader: "",
                search_template: "",
                search_city: '',
                search_district: '',
                search_kelurahan: '',
                page: 1,
                headers: [{
                        text: "TANGGAL",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "NOMOR",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "TIPE",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "TUJUAN",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "25%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "STATUS",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "25%",
                        class: "pa-2 blue lighten-3 white--text"
                    }
                ],
                pagination: {
                    descending: true,
                    page: 1,
                    rowsPerPage: 100,
                    sortBy: 'id DESC',
                    totalItems: this.$store.state.deliveryorder.total_filter_deliveryorders
                }
            };
        },
        components: {
            'one-date-picker': httpVueLoader('./oneDatePicker2.vue')
        },
        mounted() {
            this.$store.dispatch("deliveryorder/lookupbyname", {
                deliveryorder: this.init_date,
                current_page: 1,
                lastid: -1
            })
            this.$store.dispatch("deliveryorder/searchbranch", this.search_branch)
            this.$store.dispatch("deliveryorder/searchsample", this.search_sample)
            this.$store.dispatch("deliveryorder/searchcourier", this.search_courier)
        },
        computed: {
            xact() {
                return this.$store.state.deliveryorder.act
            },
            xerrors() {
                return this.$store.state.deliveryorder.errors
            },
            xshowall() {
                return this.$store.state.deliveryorder.show_all
            },
            vdeliveryorders() {
                return this.$store.state.deliveryorder.deliveryorders
            },
            xtotaldeliveryorders() {
                return this.$store.state.deliveryorder.total_deliveryorders
            },
            xtotalfilterdeliveryorders() {
                return this.$store.state.deliveryorder.total_filter_deliveryorders
            },
            init_date: {
                get() {
                    return this.$store.state.deliveryorder.init_date
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_init_date", val)
                }
            },
            curr_page: {
                get() {
                    return this.$store.state.deliveryorder.current_page
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_current_page", val)
                    this.$store.dispatch("deliveryorder/lookupbyname", {
                        deliveryorder: this.init_date,
                        current_page: val,
                        lastid: -1
                    })
                }
            },
            xtotal_page: {
                get() {
                    return this.$store.state.deliveryorder.total_deliveryorders
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_total_deliveryorders", val)
                }
            },
            dialogdeliveryorder() {
                return this.$store.state.deliveryorder.dialog_form_deliveryorder
            },
            snackbar: {
                get() {
                    return this.$store.state.deliveryorder.alert_success
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_alert_success", val)
                }
            },
            msgsnackbar() {
                return this.$store.state.deliveryorder.msg_success
            },
            lookupstatus() {
                return this.$store.state.deliveryorder.lookup_deliveryorder
            },
            xstartdate: {
                get() {
                    return this.$store.state.deliveryorder.startdate
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_startdate", val)
                }
            },
            isexternal: {
                get() {
                    return this.$store.state.deliveryorder.isexternal
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_isexternal", val)
                    this.$store.dispatch("deliveryorder/searchdestination",{
                    search: this.search_destination,
                    tipe: val === true ? 'External' : 'Internal'
                })
                }
            },
            xdeliveryordertypes() {
                return this.$store.state.deliveryorder.deliveryordertypes
            },
            xdeliveryordertype: {
                get() {
                    return this.$store.state.deliveryorder.deliveryordertype
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_deliveryordertype", val)
                }

            },
            isLoading() {
                return this.$store.state.deliveryorder.search_status == 1
            },
            xnonlabs() {
                return this.$store.state.deliveryorder.nonlabs
            },
            xnonlab: {
                get() {
                    return this.$store.state.deliveryorder.selected_nonlab
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_selected_nonlab", val)
                }
            },
            xcities() {
                return this.$store.state.deliveryorder.cities
            },
            xbranchs() {
                return this.$store.state.deliveryorder.branchs
            },
            branch: {
                get() {
                    return this.$store.state.deliveryorder.branch
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_branch", val)
                }
            },
            xdestinations() {
                return this.$store.state.deliveryorder.destinations
            },
            destination: {
                get() {
                    return this.$store.state.deliveryorder.destination
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_destination", val)
                    this.xaddress = val ? val.M_DestinationAddress : ""
                    this.xup = val ? val.M_DestinationUp : ""
                    this.$store.commit("deliveryorder/update_autocitys", [{
                        M_CityID: val.M_CityID,
                        M_CityName: val.M_CityName
                    }])
                    this.$store.commit("deliveryorder/update_autocity", {
                        M_CityID: val.M_CityID,
                        M_CityName: val.M_CityName
                    })
                    this.$store.commit("deliveryorder/update_autodistricts", [{
                        M_DistrictID: val.M_DistrictID,
                        M_DistrictName: val.M_DistrictName
                    }])
                    this.$store.commit("deliveryorder/update_autodistrict", {
                        M_DistrictID: val.M_DistrictID,
                        M_DistrictName: val.M_DistrictName
                    })
                    this.$store.commit("deliveryorder/update_autokelurahans", [{
                        M_KelurahanID: val.M_KelurahanID,
                        M_KelurahanName: val.M_KelurahanName
                    }])
                    this.$store.commit("deliveryorder/update_autokelurahan", {
                        M_KelurahanID: val.M_KelurahanID,
                        M_KelurahanName: val.M_KelurahanName
                    })
                }
            },
            xsamples() {
                return this.$store.state.deliveryorder.samples
            },
            sample: {
                get() {
                    return this.$store.state.deliveryorder.sample
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_sample", val)
                }
            },
            xcouriers() {
                return this.$store.state.deliveryorder.couriers
            },
            courier: {
                get() {
                    return this.$store.state.deliveryorder.courier
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_courier", val)
                }
            },
            xautocitys() {
                return this.$store.state.deliveryorder.autocitys
            },
            xautocity: {
                get() {
                    return this.$store.state.deliveryorder.autocity
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_autocity", val)
                    this.$store.dispatch("deliveryorder/searchdistrict", {
                        search: this.search_district,
                        id: val.M_CityID
                    })
                }
            },
            xautodistricts() {
                return this.$store.state.deliveryorder.autodistricts
            },
            xautodistrict: {
                get() {
                    return this.$store.state.deliveryorder.autodistrict
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_autodistrict", val)
                    this.$store.dispatch("deliveryorder/searchkelurahan", {
                        search: this.search_kelurahan,
                        id: val.M_DistrictID
                    })
                }
            },
            xautokelurahans() {
                return this.$store.state.deliveryorder.autokelurahans
            },
            xautokelurahan: {
                get() {
                    return this.$store.state.deliveryorder.autokelurahan
                },
                set(val) {
                    this.$store.commit("deliveryorder/update_autokelurahan", val)
                }
            }
        },
        methods: {
            updateShowAll(val) {
                this.$store.commit("deliveryorder/update_show_all", val)
                this.$store.dispatch("deliveryorder/lookupbyname", {
                    deliveryorder: this.init_date,
                    current_page: this.curr_page,
                    lastid: -1
                })
            },
            searchDeliveryOrder(prm) {
                this.$store.dispatch("deliveryorder/lookupbyname", {
                    deliveryorder: prm.new_date,
                    current_page: this.curr_page,
                    lastid: -1
                })
                this.init_date = prm.new_date
            },
            isSelected(p) {
                return p.id == this.$store.state.deliveryorder.selected_deliveryorder.id
            },
            subname(name) {
                var xname = name
                if (xname.length > 18) {
                    xname = xname.substring(0, 18) + '...'
                }
                return xname
            },
            selectMe(sc) {
                this.$store.commit("deliveryorder/update_selected_deliveryorder", sc)
                this.$store.commit("deliveryorder/update_xcek", 0)
                this.$store.dispatch("sample/selected", {
                    id: this.$store.state.deliveryorder.selected_deliveryorder.id
                })
                this.$store.commit("sample/update_couriers", [{
                    M_CourierID: this.$store.state.deliveryorder.selected_deliveryorder.M_CourierID,
                    M_StaffName: this.$store.state.deliveryorder.selected_deliveryorder.M_StaffName
                }])
                this.$store.commit("sample/update_courier", {
                    M_CourierID: this.$store.state.deliveryorder.selected_deliveryorder.M_CourierID,
                    M_StaffName: this.$store.state.deliveryorder.selected_deliveryorder.M_StaffName
                })
            },
            updateDialogFormDeliveryOrder() {
                this.$store.commit("deliveryorder/update_dialog_form_deliveryorder", false)
            },
            openFormDeliveryOrder() {
                this.$store.commit("deliveryorder/update_act", 'new')
                this.$store.commit("deliveryorder/update_dialog_form_deliveryorder", true)
                this.xstartdate = moment(new Date()).format('DD-MM-YYYY')
                this.xdestination = ""
                this.xaddress = ""
                this.isexternal = ""
                this.xup = ""
                this.xid = 0
                this.search_destination = ""
                this.$store.commit("deliveryorder/update_destinations", [])
                this.destination = {}
                this.search_sample = ""
                this.$store.commit("deliveryorder/update_samples", [])
                this.sample = {}
                this.search_courier = ""
                this.$store.commit("deliveryorder/update_couriers", [])
                this.courier = {}
                this.$store.commit("deliveryorder/update_autocitys", [])
                this.$store.commit("deliveryorder/update_autocity", {})
                this.$store.commit("deliveryorder/update_autodistricts", [])
                this.$store.commit("deliveryorder/update_autodistrict", {})
                this.$store.commit("deliveryorder/update_autokelurahans", [])
                this.$store.commit("deliveryorder/update_autokelurahan", {})
            },
            thr_search_branch: _.debounce(function () {
                this.$store.dispatch("deliveryorder/searchbranch", this.search_branch)
            }, 200),
            thr_search_destination: _.debounce(function () {
                this.$store.dispatch("deliveryorder/searchdestination",{
                    search: this.search_destination,
                    tipe: this.isexternal === true ? 'External' : 'Internal'
                })
            }, 200),
            thr_search_sample: _.debounce(function () {
                this.$store.dispatch("deliveryorder/searchsample", this.search_sample)
            }, 200),
            thr_search_courier: _.debounce(function () {
                this.$store.dispatch("deliveryorder/searchcourier", this.search_courier)
            }, 200),
            thr_search_city: _.debounce(function () {
                this.$store.dispatch("deliveryorder/searchcity", {
                    search: this.search_city
                })
            }, 200),
            thr_search_district: _.debounce(function () {
                this.$store.dispatch("deliveryorder/searchdistrict", {
                    search: this.search_district,
                    id: this.xautocity.M_CityID
                })
            }, 200),
            thr_search_kelurahan: _.debounce(function () {
                this.$store.dispatch("deliveryorder/searchkelurahan", {
                    search: this.search_kelurahan,
                    id: this.xautodistrict.M_DistrictID
                })
            }, 200),
            checkError(value) {
                var errors = this.$store.state.deliveryorder.errors
                if (errors.includes(value)) {
                    return true
                } else {
                    return false
                }
            },
            saveFormDeliveryOrder() {
                this.$store.commit("deliveryorder/update_errors", [])
                var errors = this.$store.state.deliveryorder.errors
                if (this.xstartdate === "") {
                    errors.push("requiredate")
                }
                if (_.isEmpty(this.branch)) {
                    errors.push("requirebranch")
                }
                if ((this.isexternal === false || this.isexternal === "") && _.isEmpty(this.destination)) {
                    errors.push("requiredestination")
                }
                if (this.xaddress === "" || _.isEmpty(this.xaddress)) {
                    errors.push("requireaddress")
                }
                if (_.isEmpty(this.sample)) {
                    errors.push("requiresample")
                }
                if (_.isEmpty(this.courier)) {
                    errors.push("requirecourier")
                }
                if (_.isEmpty(this.xautocity)) {
                    errors.push("requirecity")
                }
                if (_.isEmpty(this.xautodistrict)) {
                    errors.push("requiredistrict")
                }
                if (_.isEmpty(this.xautokelurahan)) {
                    errors.push("requirekelurahan")
                }
                if (errors.length === 0) {
                    this.$store.dispatch("deliveryorder/save", {
                        sdate: moment(this.xstartdate, 'DDMMYYYY').format('DD-MM-YYYY'),
                        isexternal: this.isexternal === true ? "Y" : "N",
                        branchid: this.branch.M_BranchID,
                        sampleid: this.sample.T_SampleID,
                        courierid: this.courier.M_CourierID,
                        destinationid: this.destination.M_DestinationID,
                        address: this.xaddress,
                        cityid: this.$store.state.deliveryorder.autocity.M_CityID,
                        districtid: this.$store.state.deliveryorder.autodistrict.M_DistrictID,
                        kelurahanid: this.$store.state.deliveryorder.autokelurahan.M_KelurahanID,
                        up: this.xup,
                        sdeliveryorder: this.init_date
                    })
                }
            },
            updateFormDeliveryOrder() {
                this.$store.commit("deliveryorder/update_errors", [])
                var errors = this.$store.state.deliveryorder.errors
                if (this.xstartdate === "") {
                    errors.push("requiredate")
                }
                if ((this.isexternal === false || this.isexternal === "") && _.isEmpty(this.destination)) {
                    errors.push("requiredestination")
                }
                if (this.xaddress === "" || _.isEmpty(this.xaddress)) {
                    errors.push("requireaddress")
                }
                if (_.isEmpty(this.sample)) {
                    errors.push("requiresample")
                }
                if (_.isEmpty(this.courier)) {
                    errors.push("requirecourier")
                }
                if (_.isEmpty(this.xautocity)) {
                    errors.push("requirecity")
                }
                if (_.isEmpty(this.xautodistrict)) {
                    errors.push("requiredistrict")
                }
                if (_.isEmpty(this.xautokelurahan)) {
                    errors.push("requirekelurahan")
                }
                if (errors.length === 0) {
                    this.$store.dispatch("deliveryorder/update", {
                        id: this.xid,
                        sdate: moment(this.xstartdate, 'DDMMYYYY').format('DD-MM-YYYY'),
                        isexternal: this.isexternal === true ? "Y" : "N",
                        branchid: this.branch.M_BranchID,
                        sampleid: this.sample.T_SampleID,
                        courierid: this.courier.M_CourierID,
                        destinationid: this.destination.M_DestinationID,
                        address: this.xaddress,
                        cityid: this.$store.state.deliveryorder.autocity.M_CityID,
                        districtid: this.$store.state.deliveryorder.autodistrict.M_DistrictID,
                        kelurahanid: this.$store.state.deliveryorder.autokelurahan.M_KelurahanID,
                        up: this.xup,
                        sdeliveryorder: this.init_date
                    })
                }
            },
            updateAlert_success(val) {
                this.$store.commit("deliveryorder/update_alert_success", val)
            },
            editDeliveryOrder(data) {
                this.xid = data.id
                this.xstartdate = data.startdate
                this.isexternal = data.T_RefDeliveryOrderIsExternal === 'N' ? false : true
                this.$store.commit("deliveryorder/update_branchs", [{
                    M_BranchID: data.M_BranchID,
                    M_BranchName: data.M_BranchName
                }])
                this.branch = {
                    M_BranchID: data.M_BranchID,
                    M_BranchName: data.M_BranchName
                }
                this.$store.commit("deliveryorder/update_samples", [{
                    T_SampleID: data.T_SampleID,
                    T_SampleName: data.T_SampleName
                }])
                this.sample = {
                    T_SampleID: data.T_SampleID,
                    T_SampleName: data.T_SampleName
                }
                this.$store.commit("deliveryorder/update_couriers", [{
                    M_CourierID: data.M_CourierID,
                    M_StaffName: data.M_StaffName
                }])
                this.courier = {
                    M_CourierID: data.M_CourierID,
                    M_StaffName: data.M_StaffName
                }
                this.$store.commit("deliveryorder/update_destinations", [{
                    M_DestinationID: data.M_DestinationID,
                    M_DestinationName: data.M_DestinationName,
                    M_CityID: data.M_CityID,
                    M_CityName: data.M_CityName,
                    M_DistrictID: data.M_DistrictID,
                    M_DistrictName: data.M_DistrictName,
                    M_KelurahanID: data.M_KelurahanID,
                    M_KelurahanName: data.M_KelurahanName
                }])
                this.destination = {
                    M_DestinationID: data.M_DestinationID,
                    M_DestinationName: data.M_DestinationName,
                    M_CityID: data.M_CityID,
                    M_CityName: data.M_CityName,
                    M_DistrictID: data.M_DistrictID,
                    M_DistrictName: data.M_DistrictName,
                    M_KelurahanID: data.M_KelurahanID,
                    M_KelurahanName: data.M_KelurahanName
                }
                this.$store.commit("deliveryorder/update_autocitys", [{
                    M_CityID: data.M_CityID,
                    M_CityName: data.M_CityName
                }])
                this.$store.commit("deliveryorder/update_autocity", {
                    M_CityID: data.M_CityID,
                    M_CityName: data.M_CityName
                })
                this.$store.commit("deliveryorder/update_autodistricts", [{
                    M_DistrictID: data.M_DistrictID,
                    M_DistrictName: data.M_DistrictName
                }])
                this.$store.commit("deliveryorder/update_autodistrict", {
                    M_DistrictID: data.M_DistrictID,
                    M_DistrictName: data.M_DistrictName
                })
                this.$store.commit("deliveryorder/update_autokelurahans", [{
                    M_KelurahanID: data.M_KelurahanID,
                    M_KelurahanName: data.M_KelurahanName
                }])
                this.$store.commit("deliveryorder/update_autokelurahan", {
                    M_KelurahanID: data.M_KelurahanID,
                    M_KelurahanName: data.M_KelurahanName
                })              
                
                this.xup = data.T_RefDeliveryOrderUp
                this.xaddress = data.T_RefDeliveryOrderAddress
                this.$store.commit("deliveryorder/update_act", 'edit')
                this.$store.commit("deliveryorder/update_dialog_form_deliveryorder", true)
            },
            deleteDeliveryOrder(data) {
                this.xid = data.id
                var xdata = {
                    id: data.id,
                    name: data.T_RefDeliveryOrderNumber,
                    deliveryorderdetails: 'xxx'
                }
                this.$store.commit("deliveryorder/update_selected_deliveryorder", xdata)
                this.msgalert = "Yakin, mau hapus surat jalan " + data.T_RefDeliveryOrderNumber + "  ?"
                this.dialogdeletealert = true
            },
            changeNewDeliveryOrderType(value) {
                this.readonlytypedeliveryorder = value === true ? true : false
                this.readonlytypedeliveryordernew = value === true ? false : true
            },
            newDeliveryOrderType() {
                readonlytypedeliveryorder: true
                readonlytypedeliveryordernew: false
            },
            closeDeleteAlert() {
                this.$store.dispatch("deliveryorder/delete", {
                    deliveryorderid: this.$store.state.deliveryorder.selected_deliveryorder.id,
                    xname: this.$store.state.deliveryorder.selected_deliveryorder.T_RefDeliveryOrderNumber,
                    sdeliveryorder: this.init_date
                })
                this.dialogdeletealert = false
            },
            thr_search: _.debounce(function () {
                this.$store.dispatch("deliveryorder/lookupbyname", {
                    deliveryorder: this.init_date,
                    current_page: 1,
                    lastid: -1
                })
            }, 1000)
        },
        watch: {
            xsearch(val, old) {
                console.log(val)
                this.xsearch = val
                this.thr_search()

            },
            search_branch(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.deliveryorder.update_autocomplete_status == 1) return
                this.thr_search_branch()
            },
            search_destination(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.deliveryorder.update_autocomplete_status == 1) return
                this.thr_search_destination()
            },
            search_sample(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.deliveryorder.update_autocomplete_status == 1) return
                this.thr_search_sample()
            },
            search_courier(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.deliveryorder.update_autocomplete_status == 1) return
                this.thr_search_courier()
            },
            search_city(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.deliveryorder.update_autocomplete_status == 1) return
                this.thr_search_city()
            },
            search_district(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.deliveryorder.update_autocomplete_status == 1) return
                this.thr_search_district()
            },
            search_kelurahan(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.deliveryorder.update_autocomplete_status == 1) return
                this.thr_search_kelurahan()
            }
        }
    }
</script>