<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="deleteSummaryDetail()">
                            Yakin lah
                        </v-btn>
                    </v-card-actions>
                </v-card>
            </v-dialog>

            <v-dialog v-model="xdialogsummarydetail" persistent max-width="600px">
                <v-card>
                    <v-card-title>
                        <span class="headline">FORM SUMMARY FISIK DETAIL</span></v-card-title>
                    <v-card-text class="pt-0 pb-0">
                        <v-form ref="formvalidatedetail" v-model="validdetail" lazy-validation>
                            <v-layout wrap>
                                <v-flex xs12>
                                    <v-autocomplete label="Template Kode*" :items="xtemplatecode"
                                        v-model="vtemplatecode" :search-input.sync="search_item_templatecode" auto-select-first
                                        no-filter item-text="templatecodename" return-object no-data-text="Pilih Template Kode"
                                        small
                                        :error="errtemplatecode != ''"
                                        :error-messages="errtemplatecode"
                                        >
                                        <template slot="item" slot-scope="{ item }">
                                            <v-list-tile-content>
                                                <v-list-tile-title v-text="item.templatecodename"></v-list-tile-title>
                                            </v-list-tile-content>
                                        </template>
                                    </v-autocomplete>                                
                                </v-flex>
                                <v-flex xs12>
                                    <v-select
                                        v-model="selected_detailtype"
                                        :items="xdetailtype"
                                        :error="errSelectType != ''"
                                        :error-messages="errSelectType"
                                        item-text="name"
                                        label="Tipe"
                                        return-object
                                    ></v-select>
                                </v-flex>
                                <v-flex xs12 v-if="selected_detailtype.id == 'V'">
                                    <v-text-field
                                        label="Value"
                                        v-model="xvalue"
                                        outline
                                        :error="errValue != ''"
                                        :error-messages="errValue"
                                    ></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="closeFormSummaryFisikDetail()">Tutup</v-btn>
                        <v-btn v-if="xact === 'new'" color="blue darken-1" flat @click="saveFormSummaryFisikDetail()">Simpan</v-btn>
                        <v-btn v-if="xact === 'edit'" color="blue darken-1" flat @click="updateFormSummaryFisik()">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>SUMMARY FISIK DETAIL</v-toolbar-title>
                    <v-spacer></v-spacer>
                    <v-btn @click="openFormSummaryFisikDetail(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="Summary fisik detail" 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="xsummaryfisikdetail" :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.FisikTemplateCodeLabel}}
                                        <p style="color:#800000" class="mb-0 font-weight-bold caption">{{ props.item.Mcu_FisikSummaryDetailCode}}</p>
                                    </td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                        {{ props.item.Mcu_FisikSummaryDetailType}}
                                    </td>
                                    <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                        {{ props.item.Mcu_FisikSummaryDetailValue}}
                                    </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="openEditDetail(props.item)">edit</v-icon>
                                        <v-icon small class="ml-3" color="error" @click="openDeleteDetail(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: "",
                validdetail: false,
                xid: 0,
                search_instrument: '',
                items: [],
                xname: '',
                xcode: '',
                errtemplatecode: '',
                errValue: '',
                errSelectType: '',
                name: '',
                isdefault: false,
                scode: '',
                search_doctor: '',
                search_testheader: '',
                search_template: '',
                page: 1,
                search_item_templatecode: "",
                xvalue: "",
                headers: [{
                        text: "CODE",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "20%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "TYPE",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "20%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "VALUE",
                        align: "left",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        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.kelainangroup.total_filter_kelainangroups
                // }
            };
        },
        mounted() {

        },
        computed: {
            xdetailtype() {
                return this.$store.state.fisikdetail.detailtype
            },
            selected_detailtype: {
                get() {
                    return this.$store.state.fisikdetail.selected_detailtype
                },
                set(val) {
                    this.$store.commit("fisikdetail/update_selected_detailtype", val)
                }
            },
            xsummaryfisikdetail() {
                return this.$store.state.fisikdetail.summaryfisikdetail
            },
            isLoading() {
                return this.$store.state.fisikdetail.search_status == 1
            },
            xsearch: {
                get() {
                    return this.$store.state.fisikdetail.x_search
                },
                set(val) {
                    this.$store.commit("fisikdetail/update_x_search", val)
                }
            },
            curr_page: {
                get() {
                    return this.$store.state.fisikdetail.current_page
                },
                set(val) {
                    this.$store.commit("fisikdetail/update_current_page", val)
                    this.$store.dispatch("fisikdetail/searchdetail", {
                        fifiksummaryId: this.$store.state.fisikheader.selected_summaryfisik.Mcu_FisikSummaryID,
                        search: this.xsearch,
                        current_page: val,
                        last_id: -1
                    })
                }
            },
            xtotal_page: {
                get() {
                    return this.$store.state.fisikdetail.total_summaryfisikdetails
                },
                set(val) {
                    this.$store.commit("fisikdetail/update_total_summaryfisikdetails", val)
                }
            },
            xtemplatecode() {
                return this.$store.state.fisikdetail.templatecodes
            },
            vtemplatecode: {
                get() {
                    return this.$store.state.fisikdetail.selected_templatecode
                },
                set(val) {
                    this.$store.commit("fisikdetail/update_selected_templatecode", val)
                }
            },
            xdialogsummarydetail() {
                return this.$store.state.fisikdetail.dialog_form_summary_detail
            },
            xact() {
                return this.$store.state.fisikdetail.act
            },
            xerrors() {
                return this.$store.state.fisikdetail.errors
            },
            snackbar: {
                get() {
                    return this.$store.state.fisikdetail.alert_success
                },
                set(val) {
                    this.$store.commit("fisikdetail/update_alert_success", val)
                }
            },
            msgsnackbar() {
                return this.$store.state.fisikdetail.msg_success
            },
        },
        methods: {
            isSelected(p) {
                return p.Mcu_FisikSummaryDetailID == this.$store.state.fisikdetail.selected_summaryfisikdetail.Mcu_FisikSummaryDetailID
            },
            selectMe(sc) {
                this.$store.commit("fisikdetail/update_selected_summaryfisikdetail", sc)

            },
            closeFormSummaryFisikDetail() {
                this.$store.commit("fisikdetail/update_dialog_form_summary_detail", false)
                this.$store.commit("fisikdetail/update_errors", [])
            },
            thr_search: _.debounce(function () {
                this.$store.dispatch("fisikdetail/searchdetail", {
                    fifiksummaryId: this.$store.state.fisikheader.selected_summaryfisik.Mcu_FisikSummaryID,
                    search: this.xsearch,
                    current_page: 1,
                    lastid: -1
                })
            }, 200),
            thr_search_templatecode: _.debounce(function () {
                this.$store.dispatch("fisikdetail/searchtemplatecode", this.search_item_templatecode)
            }, 2000),
            resetForm() {
                this.vtemplatecode = {}
                this.templatecodes = []
                this.selected_detailtype = {}
                this.detailtype = []
                this.errtemplatecode = ''
                this.errSelectType = ''
                this.errValue = ''
                this.xvalue = ''
            },
            openFormSummaryFisikDetail(val) {
                this.xid = val
                this.resetForm()
                this.$store.commit("fisikdetail/update_act", 'new')
                this.$store.commit("fisikdetail/update_dialog_form_summary_detail", true)
            },
            saveFormSummaryFisikDetail() {
                this.loading_save = true;
                this.errtemplatecode = "";
                if (_.isEmpty(this.vtemplatecode)) {
                    this.errtemplatecode = "Anda belum memilih template code";
                }
                this.errSelectType = "";
                if (_.isEmpty(this.selected_detailtype)) {
                    this.errSelectType = "Anda belum memilih type";
                }

                this.errValue = ""
                if (this.selected_detailtype.id == "V") {
                    if (this.xvalue == "") {
                    this.errValue = "Anda lupa mengisi value";
                    }
                }

                if (
                    this.errtemplatecode == "" &&
                    this.errSelectType == "" &&
                    this.errValue == "" 
                ) {
                    let prm = {
                        fisiksummaryId: this.$store.state.fisikheader.selected_summaryfisik.Mcu_FisikSummaryID == undefined ? 0 : this.$store.state.fisikheader.selected_summaryfisik.Mcu_FisikSummaryID,
                        code: this.vtemplatecode.FisikTemplateCodeName,
                        type: this.selected_detailtype.id,
                        value: this.xvalue,
                        name: this.vtemplatecode.FisikTemplateCodeLabel,
                        search: this.xsearch
                    }
                    this.$store.dispatch("fisikdetail/addnewdetail", prm);
                } else {
                    this.loading_save = false
                }
                
            },
            openEditDetail(data) {
                this.$store.commit("fisikdetail/update_act", 'edit')
                this.$store.commit("fisikdetail/update_dialog_form_summary_detail", true)
                this.xid = data.Mcu_FisikSummaryDetailID
                this.$store.commit("fisikdetail/update_templatecodes",[{
                    FisikTemplateCodeID: data.FisikTemplateCodeID,
                    FisikTemplateCodeName: data.FisikTemplateCodeName,
                    FisikTemplateCodeLabel: data.FisikTemplateCodeLabel,
                    templatecodename: data.templatecodename
                }])
                this.$store.commit("fisikdetail/update_selected_templatecode", {
                    FisikTemplateCodeID: data.FisikTemplateCodeID,
                    FisikTemplateCodeName: data.FisikTemplateCodeName,
                    FisikTemplateCodeLabel: data.FisikTemplateCodeLabel,
                    templatecodename: data.templatecodename
                })

                let typedetail = ""
                if (data.Mcu_FisikSummaryDetailType == "C") {
                    typedetail = "Centang"
                } else if (data.Mcu_FisikSummaryDetailType == "V") {
                    typedetail = "Value"
                } else if (data.Mcu_FisikSummaryDetailType == "VK") {
                    typedetail = "Value Nama Kelainan"
                }
                this.$store.commit("fisikdetail/update_selected_detailtype", {
                    id: data.Mcu_FisikSummaryDetailType,
                    name: typedetail
                })
                this.xvalue = data.Mcu_FisikSummaryDetailValue
            },
            updateFormSummaryFisik() {
                this.loading_save = true;
                this.errtemplatecode = "";
                if (_.isEmpty(this.vtemplatecode)) {
                    this.errtemplatecode = "Anda belum memilih template code";
                }
                this.errSelectType = "";
                if (_.isEmpty(this.selected_detailtype)) {
                    this.errSelectType = "Anda belum memilih type";
                }

                this.errValue = ""
                if (this.selected_detailtype.id == "V") {
                    if (this.xvalue == "") {
                    this.errValue = "Anda lupa mengisi value";
                    }
                }

                if (
                    this.errtemplatecode == "" &&
                    this.errSelectType == "" &&
                    this.errValue == "" 
                ) {
                    let prm = {
                        id: this.$store.state.fisikdetail.selected_summaryfisikdetail.Mcu_FisikSummaryDetailID,
                        fisiksummaryId: this.$store.state.fisikheader.selected_summaryfisik.Mcu_FisikSummaryID == undefined ? 0 : this.$store.state.fisikheader.selected_summaryfisik.Mcu_FisikSummaryID,
                        code: this.vtemplatecode.FisikTemplateCodeName,
                        type: this.selected_detailtype.id,
                        value: this.xvalue,
                        name: this.vtemplatecode.FisikTemplateCodeLabel,
                        search: this.xsearch
                    }
                    this.$store.dispatch("fisikdetail/updatedetail", prm);
                } else {
                    this.loading_save = false
                }
            },
            openDeleteDetail(data) {
                this.xid = data.Mcu_FisikSummaryDetailID
                this.msgalert = "Yakin, mau hapus summary fisik detail kode " + "[" + data.Mcu_FisikSummaryDetailCode + "]" + " ?"
                this.dialogdeletealert = true
            },
            deleteSummaryDetail() {
                this.$store.dispatch("fisikdetail/deletedetail", {
                    search: this.xsearch,
                    name: this.$store.state.fisikdetail.selected_summaryfisikdetail.Mcu_FisikSummaryDetailCode, 
                    id: this.$store.state.fisikdetail.selected_summaryfisikdetail.Mcu_FisikSummaryDetailID,
                    fisiksummaryId: this.$store.state.fisikheader.selected_summaryfisik.Mcu_FisikSummaryID == undefined ? 0 : this.$store.state.fisikheader.selected_summaryfisik.Mcu_FisikSummaryID                
                })
                this.dialogdeletealert = false
            }
        },
        watch: {
            xsearch(val, old) {
                this.xsearch = val
                this.thr_search()
            },
            search_item_templatecode(val, old) {
                if (val == old) return
                if (!val) return

                if (val.length < 1) {
                    this.$store.dispatch("fisikdetail/searchtemplatecode", this.search_item_templatecode)
                    return this.thr_search_templatecode()
                }

                if (val.length > 2) {
                    this.$store.dispatch("fisikdetail/searchtemplatecode", this.search_item_templatecode)
                    return this.thr_search_templatecode()
                }
            },
        }
    }
</script>