<template>
    <v-layout>
        <v-snackbar v-model="alert_success" color="success" :timeout="3000">
            {{ msg_success }}
        </v-snackbar>
        <v-flex xs12>
            <v-card class="mb-2" color="white">
                <v-toolbar color="blue lighten-3" dark height="50px">
                    <v-toolbar-title>Proyek Group MCU</v-toolbar-title>
                    <v-spacer></v-spacer>
                    <v-btn @click="openFormResultFlag(0)" icon>
                        <v-icon>library_add</v-icon>
                    </v-btn>
                </v-toolbar>
                <v-layout row  class="pa-2">
                    <v-flex pa-2 xs3>
                        <v-autocomplete label="Filter Corporate" v-model="selected_corporate"
                            :items="corporates" :search-input.sync="search_corporate" clearable
                            auto-select-first no-filter item-text="CorporateName" return-object
                            hide-details
                            no-data-text="Cari Berdasarkan Corporate" small required>
                        </v-autocomplete>
                    </v-flex>
                    <v-flex pa-2 xs3>
                        <v-autocomplete label="Filter Proyek" v-model="filter_project"
                            :items="projects" :search-input.sync="search_filter_project" clearable
                            hide-details
                            auto-select-first no-filter item-text="Mgm_McuLabel" return-object
                            no-data-text="Cari Berdasarkan Proyek" small required>
                        </v-autocomplete>
                    </v-flex>
                    <v-flex pa-2 xs3>
                        <v-btn color="primary" @click="goSearch()">Cari</v-btn>
                    </v-flex>
                </v-layout>
     
                <v-divider></v-divider>
                <v-layout row wrap 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="groupprojects" :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)" v-html="props.item.Mgm_McuGroupName">
                                </td>
                                <td class="text-xs-left pa-2"
                                    v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                    @click="selectMe(props.item)">
                                    <span v-for="(x,idx) in props.item.details" :key="x.Mgm_McuGroupDetailID">({{ x.Mgm_McuNumber }}) - {{ x.Mgm_McuLabel }} <span v-if="idx != props.item.details.length - 1 && props.item.details.length > 1">,</span></span>
                                </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="error"
                                        @click="DeleteAlertGroupProject(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"
                    @input="goSearch(curr_page)"
                    :length="xtotal_page">
                </v-pagination>
            </v-card>
        </v-flex>

        <v-dialog v-model="dialogresultflag" persistent max-width="60%">
            <v-card>
                <v-card-title>
                    <span class="headline">FORM ADD PROYEK GROUP MCU</span>
                </v-card-title>
                <v-card-text>
                    <v-layout row>
                        <v-flex pa-2 xs12>
                            <v-text-field label="Nama Group*" v-model="xname"  placeholder="Nama Group" outlined dense></v-text-field>
                            <p v-if="xname == ''" class="pa-2 error white--text">Nama Group harus diisi</p>
                        </v-flex>
                    </v-layout>
                    <v-layout row>
                        <v-flex pa-2 xs8>
                            <v-autocomplete label="Proyek*" v-model="selected_project"
                                :items="projects" :search-input.sync="search_project" clearable
                                auto-select-first no-filter item-text="Mgm_McuLabel" return-object
                                no-data-text="Cari Proyek" small required>
                            </v-autocomplete>
                        </v-flex>
                        <v-flex pa-2 xs4>
                            <v-btn color="primary" @click="addProyekGroupMcu()">Tambahkan Proyek</v-btn>
                        </v-flex>
                    </v-layout>
                    <v-layout row>
                        <v-flex pa-2 xs12>
                            <v-card>
                                <v-card-title>
                                    <span class="headline">Proyek Anakan</span>
                                </v-card-title>
                                <v-card-text>
                                    <v-divider></v-divider>
                                    <v-layout wrap>
                                        <v-flex pa-2 xs12>
                                            <v-text-field outlined :value="proyek.Mgm_McuLabel + ' (' + proyek.Mgm_McuNumber + ')'" v-if="addDetails.length > 0" class="mr-2" v-for="proyek in addDetails"  color="success" @click="deleteProyekGroupMcu(x)">
                                                
                                                <v-template slot="append">
                                                    <v-icon color="error" @click="deleteProyekGroupMcu(proyek)">clear</v-icon>
                                                </v-template>
                                            </v-text-field>
                                            <p v-if="addDetails.length == 0" class="pa-2 white--text error">Proyek Anakan belum ditambahkan</p>
                                        </v-flex>
                                        
                                    </v-layout>
                                </v-card-text>
                            </v-card>
                        </v-flex>
                    </v-layout>
                </v-card-text>
                <v-card-actions>
                    <v-progress-circular v-if="save_status" indeterminate color="primary"></v-progress-circular>
                    <v-spacer v-if="!save_status"></v-spacer>
                    <v-btn color="primary" flat @click="dialogresultflag = false" :disabled="save_status">
                        Tutup
                    </v-btn>
                    <v-btn :disabled="save_status" color="primary" flat @click="saveFormResultFlag()" >
                        Simpan
                    </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>

        

        <!-- <one-dialog-print :title="printtitle" :width="printwidth" :height="500" :status="openprint" :urlprint="urlprint" @close-dialog-print="closePrint"></one-dialog-print> -->
        

            <v-dialog v-model="dialogdeletealertresultflag" 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>
                                                {{ msgalertresultflag }}
                                            </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="dialogdeletealertresultflag = false">
                            Tutup
                        </v-btn>
                        <v-btn color="primary" flat @click="DeleteGroupProject()">
                            Ya, hapus
                        </v-btn>
                    </v-card-actions>
                </v-card>
            </v-dialog>

            <v-dialog v-if="save_error_message !== ''" 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">
                        <p class="pa-2">{{ save_error_message }}</p>
                    </v-card-text>
                    <v-card-actions>
                        <v-spacer></v-spacer>
                        <v-btn color="primary" flat @click="save_error_message = ''">
                            Tutup
                        </v-btn>
                    </v-card-actions>
                </v-card>
            </v-dialog>

  

    </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 = {
    components: {
        'one-date-picker': httpVueLoader('../../common/oneDatePicker.vue'),
        'one-dialog-print': httpVueLoader('../../common/oneDialogPrintX.vue')
    },
    data() {
        return {
            search_project: '',
            search_filter_project: '',
            search_corporate: '',
            resultproject: '',
            resultname: '',
            resultflag: '',
            flagRules: [
                v => !!v || 'Alamat harus diisi'
            ],
            resultRules: [
                v => !!v || 'Hasil harus diisi'
            ],
            testRules: [
                v => !!v || 'Pemeriksaan harus dipilih'
            ],
            headers: [{
                text: "Nama Group",
                align: "center",
                sortable: false,
                value: "lab",
                width: "30%",
                class: "blue lighten-3 white--text"
            },
            {
                text: "Proyek Anakan",
                align: "center",
                sortable: false,
                value: "",
                width: "50%",
                class: "blue lighten-3 white--text"
            },
            {
                text: "Aksi",
                align: "center",
                sortable: false,
                value: "status",
                width: "10%",
                class: "blue lighten-3 white--text"
            }
            ],
            validresultflag: false,
            xid: 0,
            dialogdeletealertresultflag: false,
            msgalertresultflag: ""
        };
    },
    mounted() {
        this.$store.dispatch("resultflag/searchgroupproject", {
            filter_corporate: {},
            filter_project: {},
            current_page: 1
        })
    },
    computed: {
        filter_project: {
            get() {
                return this.$store.state.resultflag.filter_project
            },
            set(val) {
                this.$store.commit("resultflag/update_filter_project", val)
                
            }
        },
        save_status : {
            get() {
                return this.$store.state.resultflag.save_status
            },
            set(val) {
                this.$store.commit("resultflag/update_save_status", val)
            }
        },
        groupprojects() {
                return this.$store.state.resultflag.groupprojects
            },
        dialogresultflag : {
            get() {
                return this.$store.state.resultflag.dialog_form_result_flag
            },
            set(val) {
                this.$store.commit("resultflag/update_dialog_form_result_flag", val)
            }
        },
        addDetails : {
            get() {
                return this.$store.state.resultflag.addDetails
            },
            set(val) {
                this.$store.commit("resultflag/update_addDetails", val)
            }
        },
        xname : {
            get() {
                return this.$store.state.resultflag.xname
            },
            set(val) {
                this.$store.commit("resultflag/update_xname", val)
            }
        },
        xerrors() {
            return this.$store.state.resultflag.errors
        },
        isLoading() {
            return this.$store.state.resultflag.search_status == 1
        },
        save_error_message: {
            get() {
                return this.$store.state.resultflag.save_error_message
            },
            set(val) {
                this.$store.commit("resultflag/update_save_error_message", val)
            }
        },
        xdescription: {
            get() {
                return this.$store.state.resultflag.description
            },
            set(val) {
                this.$store.commit("resultflag/update_description", val)
            }
        },
        xresult: {
            get() {
                return this.$store.state.resultflag.result
            },
            set(val) {
                this.$store.commit("resultflag/update_result", val)
            }
        },
        xflag: {
            get() {
                return this.$store.state.resultflag.flag
            },
            set(val) {
                this.$store.commit("resultflag/update_flag", val)
            }
        },
        projects() {
            return this.$store.state.resultflag.projects
        },
        selected_project: {
            get() {
                return this.$store.state.resultflag.selected_project
            },
            set(val) {
                this.$store.commit("resultflag/update_selected_project", val)
            }
        },
        selected_corporate: {
            get() {
                return this.$store.state.resultflag.selected_corporate
            },
            set(val) {
                this.$store.commit("resultflag/update_selected_corporate", val)
                
            }
        },
        corporates() {
            return this.$store.state.resultflag.corporates
        },
        xsearchtest: {
            get() {
                return this.$store.state.resultflag.x_search_test
            },
            set(val) {
                this.$store.commit("resultflag/update_x_search_test", val)
            }
        },
        message_save: {
            get() {
                return this.$store.state.resultflag.message_save
            },
            set(val) {
                this.$store.commit("resultflag/update_message_save", val)
            }
        },
        xsearchresultname: {
            get() {
                return this.$store.state.resultflag.x_search_resultname
            },
            set(val) {
                this.$store.commit("resultflag/update_x_search_resultname", val)
            }
        },
        xsearchflag: {
            get() {
                return this.$store.state.resultflag.x_search_flag
            },
            set(val) {
                this.$store.commit("resultflag/update_x_search_flag", val)
            }
        },
        curr_page: {
            get() {
                return this.$store.state.resultflag.current_page
            },
            set(val) {
                this.$store.commit("resultflag/update_current_page", val)
            }
        },
        xtotal_page: {
            get() {
                return this.$store.state.resultflag.total_page
            },
            set(val) {
                this.$store.commit("resultflag/update_total_page", val)
            }
        },
        alert_success: {
            get() {
                return this.$store.state.resultflag.alert_success
            },
            set(val) {
                this.$store.commit("resultflag/update_alert_success", val)
            }
        },
        msg_success: {
            get() {
                return this.$store.state.resultflag.msg_success
            },
            set(val) {
                this.$store.commit("resultflag/update_msg_success", val)
            }
        }
    },
    methods: {
        addProyekGroupMcu(){
            let addDetails = this.addDetails
            let selected_project = this.$store.state.resultflag.selected_project
            console.log(selected_project)
            if(addDetails.length == 0){
                this.addDetails.push(selected_project)
            }else{
                let _this = this
                let filter = _.findIndex(addDetails, function(o) { return o.Mgm_McuNumber == selected_project.Mgm_McuNumber})
                console.log(filter)
                if(filter == -1){
                    this.addDetails.push(selected_project)
                }
            }
        },
        deleteProyekGroupMcu(val){
            this.addDetails = this.addDetails.filter(x => x.Mgm_McuNumber != val.Mgm_McuNumber)
        },
        goSearch(page=1){
            this.curr_page = 1
            this.$store.dispatch("resultflag/searchgroupproject", {
                filter_corporate: this.selected_corporate,
                filter_project: this.filter_project,
                current_page: this.curr_page
            })
        },
        isSelected(p) {
            return p.Mgm_McuGroupID == this.$store.state.resultflag.selected_groupproject.Mgm_McuGroupID
        },
        selectMe(sc) {
            this.$store.commit("resultflag/update_selected_groupproject", sc)
        },
        closeDialogFormResultFlag() {
            this.$store.commit("resultflag/update_save_error_message", '')
            this.$store.commit("resultflag/update_errors", [])
            this.$store.commit("resultflag/update_selected_project", {})
            this.$store.commit("resultflag/update_result", '')
            this.$store.commit("resultflag/update_flag", '')
            this.$store.commit("resultflag/update_dialog_form_result_flag", false)
        },
        saveFormResultFlag() {
            const selected = this.$store.state.resultflag.selected_project;
            this.$store.commit("resultflag/update_save_error_message", '')
            if (!selected || Object.keys(selected).length === 0) {
                this.$store.commit("resultflag/update_errors", [{ msg: "Pilih Pemeriksaannya Dulu" }]);
                return;
            }
            this.$store.dispatch("resultflag/addresultflag", {
                name: this.xname,
                details: this.addDetails
                })

        },
        thr_search_project: _.debounce(function () {
            this.$store.dispatch("resultflag/searchproject", {
                search_project: this.search_project,
                details: this.addDetails
            })
        }, 1000),
        thr_search_filter_project: _.debounce(function () {
            this.$store.dispatch("resultflag/searchfilterproject", {
                search_project: this.search_filter_project,
                details: []
            })
        }, 1000),
        thr_search_corporate(){
            this.$store.dispatch("resultflag/searchcorporate", {
                search_corporate: this.search_corporate,
            })
        },
        
        openFormResultFlag(val) {
            this.$store.commit("resultflag/update_dialog_form_result_flag", true)
        },
        DeleteAlertGroupProject(data) {
            this.xid = data.Mgm_McuGroupID

            this.msgalertresultflag = "Yakin, mau hapus group proyek "+data.Mgm_McuGroupName+"?"
            this.dialogdeletealertresultflag = true
        },
        DeleteGroupProject() {
            console.log('do delete')
            this.$store.dispatch("resultflag/deletegroupproject", {
                id: this.xid?this.xid:this.$store.state.resultflag.selected_groupproject.Mgm_McuGroupID
            })
            this.dialogdeletealertresultflag = false
        }
      
    },
    watch: {
        search_project(val, old) {
            if (val == old) return
            if (!val) return
            if (val.length < 1) return
            if (this.$store.state.resultflag.update_autocomplete_status == 1) return
            this.thr_search_project()
        },
        search_corporate(val, old) {
            if (val == old) return
            if (!val) return
            if (val.length < 1) return
            if (this.$store.state.resultflag.update_autocomplete_status == 1) return
            this.thr_search_corporate()
        },
        search_filter_project(val, old) {
            if (val == old) return
            if (!val) return
            if (val.length < 1) return
            if (this.$store.state.resultflag.update_autocomplete_status == 1) return
            this.thr_search_filter_project()
        },
    }
}
</script>