<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="dialogcompany" persistent max-width="600px">
                <v-card>
                    <v-card-title>
                        <span class="headline">FORM COMPANY</span></v-card-title>
                    <v-card-text class="pt-0 pb-0">
                        <v-form ref="formcompany" v-model="valid" lazy-validation>
                            <v-layout wrap>
                                <v-flex xs12>
                                    <v-autocomplete label="Company*" v-model="xcompany" :items="vcompany"
                                        :search-input.sync="search_company" auto-select-first no-filter
                                        item-text="M_CompanyName" return-object no-data-text="Cari Company" small
                                        :rules="companyRules" required>
                                        <template slot="item" slot-scope="{ item }">
                                            <v-list-tile-content>
                                                <v-list-tile-title v-text="item.M_CompanyName"></v-list-tile-title>
                                            </v-list-tile-content>
                                        </template>
                                    </v-autocomplete>
                                </v-flex>
                                <v-flex xs12>
                                    <v-flex xs12>
                                        <v-autocomplete v-if="xact === 'new'" label="Agreement*" v-model="vmou"
                                            :items="xmoussearch" :search-input.sync="search_mou"
                                            auto-select-first no-filter item-text="M_MouName" return-object
                                            no-data-text="Pilih Company dulu" small :rules="mouRules" required>
                                            <template slot="item" slot-scope="{ item }">
                                                <v-list-tile-content>
                                                    <v-list-tile-title
                                                        v-text="item.M_MouName"></v-list-tile-title>
                                                </v-list-tile-content>
                                            </template>
                                        </v-autocomplete>
                                    </v-flex>
                                </v-flex>
                                <v-flex xs12>
                                    <v-select v-model="xregional" :items="vregional" item-text="S_RegionalName"
                                        item-value="S_RegionalID" return-object label="Regional*" :rules="regionalRules" required>
                                    </v-select>
                                </v-flex>
                                <v-flex xs12>
                                    <v-text-field v-model="xname" label="Username*" :rules="xnameRules"
                                        required></v-text-field>
                                </v-flex>
                                <v-flex xs12>
                                    <v-text-field v-if="xact === 'new'" type="password" v-model="xpassword" label="Password*"
                                        :rules="xpasswordRules" required></v-text-field>
                                </v-flex>
                                <v-flex xs12>
                                    <v-text-field v-if="xact === 'new'" type="password" v-model="xpasswordconfirm"
                                        label="Konfirmasi Password*" :rules="xpasswordconfirmRules"
                                        required></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="updateDialogFormCompany()">Tutup</v-btn>
                        <v-btn v-if="xact === 'new'" color="blue darken-1" flat
                            @click="saveFormCompany()">Simpan</v-btn>
                        <v-btn v-if="xact === 'edit'" color="blue darken-1" flat @click="updateFormCompany()">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>USER MITRA</v-toolbar-title>
                    <v-spacer></v-spacer>
                    <v-btn @click="openFormCompany(0)" icon>
                        <v-icon>library_add</v-icon>
                    </v-btn>
                </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:5px;" justify-left>
                    <v-list-tile>
                        <input type="text" v-model="xsearch" class="textinput" label="Nama Station"
                            placeholder="Cari ..." />
                        </v-list-tile-content>
                    </v-list-tile>
                </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="vcompanys" :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.M_UserUsername }}</td>
                                    <td class="text-xs-left pa-2"
                                        v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                        @click="selectMe(props.item)">{{ props.item.M_CompanyName }}</td>
                                    <td class="text-xs-left pa-2"
                                        v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                        @click="selectMe(props.item)">{{ props.item.S_RegionalName }}</td>
                                    <td class="text-xs-left pa-2"
                                        v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                        @click="selectMe(props.item)">
                                        <v-icon small class="ml-3" color="primary"
                                            @click="editCompany(props.item)">edit</v-icon>
                                        <v-icon small class="ml-3" color="error"
                                            @click="deleteCompany(props.item)">clear</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_instrument: '',
            items: [],
            xname: '',
            xpassword: '',
            xpasswordconfirm: '',
            xnameRules: [
                v => !!v || 'User harus diisi'
            ],
            xpasswordRules: [
                v => !!v || 'Password harus diisi'
            ],
            xpasswordconfirmRules: [
                v => !!v || 'Konfirmasi Password harus diisi'
            ],
            companyRules: [
                v => !!v || 'Company harus dipilih'
            ],
            regionalRules: [
                v => !!v || 'Regional harus dipilih'
            ],
            mouRules: [
                v => !!v || 'Agreement harus dipilih'
            ],
            name: '',
            isdefault: false,
            scode: '',
            search_doctor: '',
            search_testheader: '',
            search_template: '',
            page: 1,
            search_company: '',
            search_mou: '',
            headers: [{
                text: "USERNAME",
                align: "left",
                sortable: false,
                value: "mr",
                width: "30%",
                class: "pa-2 blue lighten-3 white--text"
            },
            {
                text: "COMPANY",
                align: "left",
                sortable: false,
                value: "mr",
                width: "30%",
                class: "pa-2 blue lighten-3 white--text"
            },
            {
                text: "REGIONAL",
                align: "left",
                sortable: false,
                value: "mr",
                width: "30%",
                class: "pa-2 blue lighten-3 white--text"
            },
            {
                text: "AKSI",
                align: "left",
                sortable: false,
                value: "mr",
                width: "10%",
                class: "pa-2 blue lighten-3 white--text"
            }
            ],
            pagination: {
                descending: true,
                page: 1,
                rowsPerPage: 100,
                sortBy: 'id DESC',
                totalItems: this.$store.state.company.total_filter_companys
            }
        };
    },
    mounted() {
        this.$store.dispatch("company/lookupbyname", {
            search: this.xsearch,
            current_page: 1,
            lastid: -1
        })
        this.$store.dispatch("company/get_regional")
    },
    computed: {
        xmoussearch() {
            return this.$store.state.company.mous_search
        },
        xact() {
            return this.$store.state.company.act
        },
        xerrors() {
            return this.$store.state.company.errors
        },
        xshowall() {
            return this.$store.state.company.show_all
        },
        vcompanys() {
            return this.$store.state.company.companys
        },
        xtotalcompanys() {
            return this.$store.state.company.total_companys
        },
        xtotalfiltercompanys() {
            return this.$store.state.company.total_filter_companys
        },
        vmou: {
            get() {
                return this.$store.state.company.vmou
            },
            set(val) {
                this.$store.commit("company/update_vmou", val)
            }
        },
        xsearch: {
            get() {
                return this.$store.state.company.x_search
            },
            set(val) {
                this.$store.commit("company/update_x_search", val)
            }
        },
        curr_page: {
            get() {
                return this.$store.state.company.current_page
            },
            set(val) {
                this.$store.commit("company/update_current_page", val)
                this.$store.dispatch("company/lookupbyname", {
                    search: this.xsearch,
                    current_page: val,
                    lastid: -1
                })
            }
        },
        xtotal_page: {
            get() {
                return this.$store.state.company.total_companys
            },
            set(val) {
                this.$store.commit("company/update_total_companys", val)
            }
        },
        dialogcompany() {
            return this.$store.state.company.dialog_form_company
        },
        snackbar: {
            get() {
                return this.$store.state.company.alert_success
            },
            set(val) {
                this.$store.commit("company/update_alert_success", val)
            }
        },
        msgsnackbar() {
            return this.$store.state.company.msg_success
        },
        lookupstatus() {
            return this.$store.state.company.lookup_company
        },
        vcompany() {
            return this.$store.state.company.vcompany
        },
        xcompany: {
            get() {
                return this.$store.state.company.xcompany
            },
            set(val) {
                this.$store.commit("company/update_xcompany", val)
            }
        },
        vregional() {
            return this.$store.state.company.vregional
        },
        xregional: {
            get() {
                return this.$store.state.company.xregional
            },
            set(val) {
                this.$store.commit("company/update_xregional", val)
            }
        },
        xcompanytypes() {
            return this.$store.state.company.companytypes
        },
        xcompanytype: {
            get() {
                return this.$store.state.company.companytype
            },
            set(val) {
                this.$store.commit("company/update_companytype", val)
            }

        },
        isLoading() {
            return this.$store.state.company.search_status == 1
        },
        xnonlabs() {
            return this.$store.state.company.nonlabs
        },
        xnonlab: {
            get() {
                return this.$store.state.company.selected_nonlab
            },
            set(val) {
                this.$store.commit("company/update_selected_nonlab", val)
            }
        },
        xcities() {
            return this.$store.state.company.cities
        },
        mouaddress: {
            get() {
                return this.$store.state.company.mou_address
            },
            set(val) {
                this.$store.commit("company/update_mou_address", val)
                this.$store.dispatch("company/getdistrict", this.$store.state.company.mou_address)
            }
        },
        xdoctors() {
            return this.$store.state.company.doctors
        },
        doctor: {
            get() {
                return this.$store.state.company.doctor
            },
            set(val) {
                this.$store.commit("company/update_doctor", val)
            }
        },
        xinstruments() {
            return this.$store.state.company.instruments
        },
        xinstrument: {
            get() {
                return this.$store.state.company.instrument
            },
            set(val) {
                this.$store.commit("company/update_instrument", val)
            }
        },
        xautotemplates() {
            return this.$store.state.company.autotemplates
        },
        xautotemplate: {
            get() {
                return this.$store.state.company.autotemplate
            },
            set(val) {
                this.$store.commit("company/update_autotemplate", val)
            }
        },
        xdistricts() {
            return this.$store.state.company.districts
        },
        districtaddress: {
            get() {
                return this.$store.state.company.district_address
            },
            set(val) {
                this.$store.commit("company/update_district_address", val)
                this.$store.dispatch("company/getkelurahan", this.$store.state.company.district_address)
            }
        },
        xkelurahans() {
            return this.$store.state.company.kelurahans
        },
        kelurahanaddress: {
            get() {
                return this.$store.state.company.kelurahan_address
            },
            set(val) {
                this.$store.commit("company/update_kelurahan_address", val)
            }
        }
    },
    methods: {
        updateShowAll(val) {
            this.$store.commit("company/update_show_all", val)
            this.$store.dispatch("company/lookupbyname", {
                search: this.xsearch,
                current_page: this.curr_page,
                lastid: -1
            })
        },
        searchCompany() {
            this.$store.dispatch("company/lookup", {
                search: this.xsearch,
                current_page: 1,
                lastid: -1
            })
        },
        isSelected(p) {
            return p.id == this.$store.state.company.selected_company.id
        },
        subname(name) {
            var xname = name
            if (xname.length > 18) {
                xname = xname.substring(0, 18) + '...'
            }
            return xname
        },
        selectMe(sc) {
            this.$store.commit("mou/update_current_page", 1)
            this.$store.commit("company/update_selected_company", sc)
            this.$store.dispatch("mou/lookupbyidname", {
                id: this.$store.state.company.selected_company.id,
                search: '',
                current_page: 1,
                lastid: -1
            })
            this.$store.commit("company/update_vcompany", [{
                M_CompanyID: this.$store.state.company.selected_company.M_CompanyID,
                M_CompanyName: this.$store.state.company.selected_company.M_CompanyName
            }])
            this.$store.commit("company/update_xcompany", {
                M_CompanyID: this.$store.state.company.selected_company.M_CompanyID,
                M_CompanyName: this.$store.state.company.selected_company.M_CompanyName
            })
        },
        updateDialogFormCompany() {
            this.$store.commit("company/update_dialog_form_company", false)
            this.$store.commit("company/update_errors", [])
        },
        openFormCompany() {
            this.$refs.formcompany.reset()
            this.$refs.formcompany.resetValidation()
            this.$store.commit("company/update_act", 'new')
            this.$store.commit("company/update_dialog_form_company", true)
        },
        thr_search_mou: _.debounce(function () {
            this.$store.dispatch("company/searchmou", {
                companyId: this.$store.state.company.xcompany.M_CompanyID,
                search: this.search_mou
            })
        }, 2000),
        thr_search_company: _.debounce(function () {
            this.$store.dispatch("company/searchcompany", {
                search: this.search_company
            })
        }, 2000),
        thr_search_instrument: _.debounce(function () {
            this.$store.dispatch("company/searchinstrument", {
                tes: this.search_instrument
            })
        }, 2000),
        saveFormCompany() {
            if (this.$refs.formcompany.validate()) {
                this.$store.dispatch("company/save", {
                    companyId: this.xcompany.M_CompanyID,
                    mouId: this.vmou.M_MouID,
                    regionalId: this.xregional.S_RegionalID,
                    username: this.xname,
                    password: this.xpassword,
                    confirm_password: this.xpasswordconfirm,
                    search: this.xsearch
                })

            }
        },
        updateFormCompany() {

            if (this.$refs.formcompany.validate()) {
                this.$store.dispatch("company/update", {
                    Id: this.xid,
                    companyId: this.xcompany.M_CompanyID,
                    mouId: this.vmou.M_MouID,
                    regionalId: this.xregional.S_RegionalID,
                    username: this.xname,
                    search: this.xsearch
                })
            }
        },
        updateAlert_success(val) {
            this.$store.commit("company/update_alert_success", val)
        },
        editCompany(data) {

            this.xid = data.id
            this.$store.commit("company/update_xcompany", {
                M_CompanyID: data.M_CompanyID,
                M_CompanyName: data.M_CompanyName
            })

            this.$store.commit("company/update_vcompany", [{
                M_CompanyID: data.M_CompanyID,
                M_CompanyName: data.M_CompanyName
            }])

            // this.$store.commit("company/update_vmou", {
            //     M_MouID: data.M_MouID,
            //     M_MouName: data.M_MouName
            // })

            // this.$store.commit("company/update_mous_search", [{
            //     M_MouID: data.M_MouID,
            //     M_MouName: data.M_MouName
            // }])

            this.$store.commit("company/update_xregional", {
                S_RegionalID: data.S_RegionalID,
                S_RegionalName: data.S_RegionalName
            })

            this.$store.commit("company/update_vregional", [{
                S_RegionalID: data.S_RegionalID,
                S_RegionalName: data.S_RegionalName
            }])

            this.xname = data.M_UserUsername
            
            this.$store.commit("company/update_act", 'edit')
            this.$store.commit("company/update_dialog_form_company", true)
        },
        deleteCompany(data) {
            this.xid = data.id
            var xdata = {
                id: data.id,
                username: data.M_UserUsername,

            }
            this.$store.commit("company/update_selected_company", xdata)
            this.msgalert = "Yakin, mau hapus user " + data.M_UserUsername + "  ?"
            this.dialogdeletealert = true
        },
        changeNewCompanyType(value) {
            this.readonlytypecompany = value === true ? true : false
            this.readonlytypecompanynew = value === true ? false : true
        },
        newCompanyType() {
            readonlytypecompany: true
            readonlytypecompanynew: false
        },
        closeDeleteAlert() {
            this.$store.dispatch("company/delete", {
                Id: this.$store.state.company.selected_company.id,
                username: this.$store.state.company.selected_company.username,
                search: this.xsearch
            })
            this.dialogdeletealert = false
        },
        thr_search: _.debounce(function () {
            this.$store.dispatch("company/lookupbyname", {
                search: this.xsearch,
                current_page: 1,
                lastid: -1
            })
        }, 1000)
    },
    watch: {
        xsearch(val, old) {
            this.xsearch = val
            this.thr_search()

        },
        search_company(val, old) {
            if (val == old) return
            if (!val) return
            if (val.length < 1) return
            if (this.$store.state.company.update_autocomplete_status == 1) return
            this.thr_search_company()
        },
        search_mou(val, old) {
            if (val == old) return
            if (!val) return
            if (val.length < 1) return
            if (this.$store.state.company.update_autocomplete_status == 1) return
            this.thr_search_mou()
        },
        search_instrument(val, old) {
            if (val == old) return
            if (!val) return
            if (val.length < 1) return
            if (this.$store.state.company.update_autocomplete_status == 1) return
            this.thr_search_instrument()
        }
    }
}
</script>