<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="dialogcontrol" persistent max-width="600px">
                <v-card>
                    <v-card-title>
                        <span class="headline">FORM INSTRUMENT</span></v-card-title>
                    <v-card-text class="pt-0 pb-0">
                        <v-form ref="formcontrol" v-model="valid" lazy-validation>
                            <v-layout wrap>
                                <v-flex xs12>
                                    <v-text-field v-model="xname" label="Nama*"></v-text-field>
                                    <p v-if="checkError('requirename')" class="error pl-2 pr-2" style="color:#fff">Nama harus diisi dong</p>
                                </v-flex>
                                <v-flex xs12>
                                    <v-select item-text="Nat_QcLevelName" return-object :items="xlevels" v-model="xlevel" label="Level Control*"></v-select>
                                    <p v-if="checkError('requirelevel')" class="error pl-2 pr-2" style="color:#fff">Level Control harus diisi dong</p>
                                </v-flex>
                                <v-flex xs12>
                                    <v-text-field v-model="xlotnumber" label="Lot Number*"></v-text-field>
                                    <p v-if="checkError('requirelotnumber')" class="error pl-2 pr-2" style="color:#fff">Lot Number harus diisi dong</p>
                                </v-flex>
                                <v-flex xs12>
                                    <v-menu class="xs3 pr-2" v-model="menufilterdateexp" :close-on-content-click="false" :nudge-right="40" lazy transition="scale-transition"
                                        offset-y full-width max-width="290px" min-width="290px">
                                        <template v-slot:activator="{ on }">
                                            <v-text-field class="mt-1" v-model="filterComputedDateFormattedExp" label="Tanggal Kadaluarsa*" readonly v-on="on" @blur="date = deFormatedDateExp(filterComputedDateFormattedExp)"></v-text-field>
                                        </template>
                                        <v-date-picker v-model="xexpdate" no-title @input="menufilterdateexp = false"></v-date-picker>
                                    </v-menu>
                                    <p v-if="checkError('requireexp')" class="error pl-2 pr-2" style="color:#fff">Tanggal Kadaluarsa harus diisi dong</p>
                                </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="updateDialogFormControl()">Tutup</v-btn>
                        <v-btn v-if="xact === 'new'" color="blue darken-1" flat @click="saveFormControl()">Simpan</v-btn>
                        <v-btn v-if="xact === 'edit'" color="blue darken-1" flat @click="updateFormControl()">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>HISTORY FORCE PAYMENT </v-toolbar-title>
                    <v-spacer></v-spacer>
                   <!-- <v-btn @click="openFormControl(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 Nomor Transaksi" />
                        <v-menu v-model="menufilterdatestart" :close-on-content-click="false" :nudge-right="40" lazy transition="scale-transition"
                            offset-y full-width max-width="290px" min-width="290px">
                            <template v-slot:activator="{ on }">
                           
                                    <input type="text" v-model="filterComputedDateFormattedStart" v-on="on" @blur="date = deFormatedDate(filterComputedDateFormattedStart)"
                            class="textinput" />
                            </template>
                            <v-date-picker v-model="xstartdate" no-title @input="menufilterdatestart = false"></v-date-picker>
                        </v-menu></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="vcontrols" :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.tanggal}}
                                </td>
                                <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{props.item.F_RegSendTransactionNoTransaction}}
                                </td>
                                <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{props.item.F_RegSendTransactionNoRef}}
                                </td>
                                <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.F_RegSendTransactionStatus}}
                                </td>
                                <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                    <p style="color:black" class="mb-0 font-weight-bold caption">{{props.item.F_RegSendTransactionMessage}}</p>
                                </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: '',
                xcode: '',
                xlotnumber: '',
                xnameRules: [
                    v => !!v || 'Nama harus diisi'
                ],
                instrumentRules: [
                    v => !!v || 'Instrument harus dipilih'
                ],
                name: '',
                isdefault: false,
                scode: '',
                search_doctor: '',
                search_testheader: '',
                search_template: '',
                page: 1,
                menufilterdateexp: false,
                menufilterdatestart: false,
                headers: [{
                        text: "TANGGAL",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "10%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "NO TRANSAKSI",
                        align: "left",
                        sortable: false,
                        value: "status",
                        width: "15%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "NO REF",
                        align: "left",
                        sortable: false,
                        value: "status",
                        width: "10%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "USERNAME",
                        align: "left",
                        sortable: false,
                        value: "status",
                        width: "15%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "STATUS",
                        align: "left",
                        sortable: false,
                        value: "status",
                        width: "15%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "PESAN",
                        align: "left",
                        sortable: false,
                        value: "status",
                        width: "30%",
                        class: "blue lighten-3 white--text"
                    }
                ],
                pagination: {
                    descending: true,
                    page: 1,
                    rowsPerPage: 100,
                    sortBy: 'id DESC',
                    totalItems: this.$store.state.control.total_filter_controls
                }
            };
        },
        mounted() {
            this.$store.dispatch("control/lookupbyname", {
                search: this.xsearch,
                sdate: this.$store.state.control.startdate,
                current_page: 1,
                lastid: -1
            })
        },
        computed: {
            xact() {
                return this.$store.state.control.act
            },
            xerrors() {
                return this.$store.state.control.errors
            },
            xshowall() {
                return this.$store.state.control.show_all
            },
            vcontrols() {
                return this.$store.state.control.controls
            },
            xtotalcontrols() {
                return this.$store.state.control.total_controls
            },
            xtotalfiltercontrols() {
                return this.$store.state.control.total_filter_controls
            },
            xlevels() {
                return this.$store.state.control.levels
            },
            xlevel: {
                get() {
                    return this.$store.state.control.level
                },
                set(val) {
                    this.$store.commit("control/update_level", val)
                }

            },
            filterComputedDateFormattedStart() {
                return this.formatDate(this.xstartdate)
            },
            xstartdate: {
                get() {
                    return this.$store.state.control.startdate
                },
                set(val) {
                    this.$store.commit("control/update_startdate", val)
                }
            },
            filterComputedDateFormattedExp() {
                return this.formatDateExp(this.xexpdate)
            },
            xexpdate: {
                get() {
                    return this.$store.state.control.xexpdate
                },
                set(val) {
                    this.$store.commit("control/update_xexpdate", val)
                    console.log(val)
                }
            },
            xsearch: {
                get() {
                    return this.$store.state.control.x_search
                },
                set(val) {
                    this.$store.commit("control/update_x_search", val)
                }
            },
            curr_page: {
                get() {
                    return this.$store.state.control.current_page
                },
                set(val) {
                    this.$store.commit("control/update_current_page", val)
                    this.$store.dispatch("control/lookupbyname", {
                        search: this.xsearch,
                sdate: this.$store.state.control.startdate,
                        current_page: val,
                        lastid: -1
                    })
                }
            },
            xtotal_page: {
                get() {
                    return this.$store.state.control.total_controls
                },
                set(val) {
                    this.$store.commit("control/update_total_controls", val)
                }
            },
            dialogcontrol() {
                return this.$store.state.control.dialog_form_control
            },
            snackbar: {
                get() {
                    return this.$store.state.control.alert_success
                },
                set(val) {
                    this.$store.commit("control/update_alert_success", val)
                }
            },
            msgsnackbar() {
                return this.$store.state.control.msg_success
            },
            lookupstatus() {
                return this.$store.state.control.lookup_control
            },
            xcontroltypes() {
                return this.$store.state.control.controltypes
            },
            xcontroltype: {
                get() {
                    return this.$store.state.control.controltype
                },
                set(val) {
                    this.$store.commit("control/update_controltype", val)
                }

            },
            isLoading() {
                return this.$store.state.control.search_status == 1
            },
            xnonlabs() {
                return this.$store.state.control.nonlabs
            },
            xnonlab: {
                get() {
                    return this.$store.state.control.selected_nonlab
                },
                set(val) {
                    this.$store.commit("control/update_selected_nonlab", val)
                }
            },
            xcities() {
                return this.$store.state.control.cities
            },
            qcaddress: {
                get() {
                    return this.$store.state.control.qc_address
                },
                set(val) {
                    this.$store.commit("control/update_qc_address", val)
                    this.$store.dispatch("control/getdistrict", this.$store.state.control.qc_address)
                }
            },
            xdoctors() {
                return this.$store.state.control.doctors
            },
            doctor: {
                get() {
                    return this.$store.state.control.doctor
                },
                set(val) {
                    this.$store.commit("control/update_doctor", val)
                }
            },
            xinstruments() {
                return this.$store.state.control.instruments
            },
            xinstrument: {
                get() {
                    return this.$store.state.control.instrument
                },
                set(val) {
                    this.$store.commit("control/update_instrument", val)
                }
            },
            xautotemplates() {
                return this.$store.state.control.autotemplates
            },
            xautotemplate: {
                get() {
                    return this.$store.state.control.autotemplate
                },
                set(val) {
                    this.$store.commit("control/update_autotemplate", val)
                }
            },
            xdistricts() {
                return this.$store.state.control.districts
            },
            districtaddress: {
                get() {
                    return this.$store.state.control.district_address
                },
                set(val) {
                    this.$store.commit("control/update_district_address", val)
                    this.$store.dispatch("control/getkelurahan", this.$store.state.control.district_address)
                }
            },
            xkelurahans() {
                return this.$store.state.control.kelurahans
            },
            kelurahanaddress: {
                get() {
                    return this.$store.state.control.kelurahan_address
                },
                set(val) {
                    this.$store.commit("control/update_kelurahan_address", val)
                }
            }
        },
        methods: {
            updateShowAll(val) {
                this.$store.commit("control/update_show_all", val)
                this.$store.dispatch("control/lookupbyname", {
                    search: this.xsearch,
                sdate: this.$store.state.control.startdate,
                    current_page: this.curr_page,
                    lastid: -1
                })
            },
            searchControl() {
                this.$store.dispatch("control/lookup", {
                    search: this.xsearch,
                sdate: this.$store.state.control.startdate,
                    current_page: 1,
                    lastid: -1
                })
            },
            deFormatedDateExp(date) {
                if (!date) return null

                const [day, month, year] = date.split('-')
                return `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`
            },
            formatDateExp(date) {
                console.log(date)
                if (!date) return null

                const [year, month, day] = date.split('-')
                return `${day}-${month}-${year}`
            },
            formatDate(date) {
                if (!date) return null

                const [year, month, day] = date.split('-')
                return `${day}-${month}-${year}`
            },
            deFormatedDate(date) {
                if (!date) return null

                const [day, month, year] = date.split('-')
                return `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`
            },
            isSelected(p) {
                return p.id == this.$store.state.control.selected_control.id

            },
            subname(name) {
                var xname = name
                if (xname.length > 18) {
                    xname = xname.substring(0, 18) + '...'
                }
                return xname
            },
            checkError(value) {
                var errors = this.$store.state.control.errors
                if (errors.includes(value)) {
                    return true
                } else {
                    return false
                }
            },
            selectMe(sc) {


            },
            updateDialogFormControl() {
                this.$store.commit("control/update_dialog_form_control", false)
            },
            openFormControl() {
                this.xlotnumber = ""
                this.xname = ""
                this.xexpdate = moment(new Date()).format('YYYY-MM-DD')
                this.$store.commit("control/update_level", {})
                this.$refs.formcontrol.reset()
                this.$refs.formcontrol.resetValidation()
                this.$store.commit("control/update_act", 'new')
                this.$store.commit("control/update_dialog_form_control", true)
            },
            thr_search_instrument: _.debounce(function () {
                this.$store.dispatch("control/searchinstrument", {
                    tes: this.search_instrument
                })
            }, 2000),
            saveFormControl() {
                this.$store.commit("control/update_errors", [])
                var errors = this.$store.state.control.errors

                if (_.isEmpty(this.xname)) {
                    errors.push("requirename")
                }
                if (_.isEmpty(this.xlotnumber)) {
                    errors.push("requirelotnumber")
                }
                if (_.isEmpty(this.xlevel)) {
                    errors.push("requirelevel")
                }
                if (this.xexpdate === '') {
                    errors.push("requireexp")
                }
                if (errors.length === 0) {
                    this.$store.dispatch("control/save", {
                        name: this.xname,
                        level: this.xlevel.Nat_QcLevelID,
                        lotnumber: this.xlotnumber,
                        exp: this.xexpdate,
                        scontrol: this.xsearch,
                        sinstrument: this.$store.state.instrument.x_search,
                        sqc: this.$store.state.qc.x_search
                    })

                }
            },
            updateFormControl() {
                this.$store.commit("control/update_errors", [])
                var errors = this.$store.state.control.errors

                if (_.isEmpty(this.xname)) {
                    errors.push("requirename")
                }
                if (_.isEmpty(this.xlotnumber)) {
                    errors.push("requirelotnumber")
                }
                if (_.isEmpty(this.xlevel)) {
                    errors.push("requirelevel")
                }
                if (this.xexpdate === '') {
                    errors.push("requireexp")
                }
                if (errors.length === 0) {
                    this.$store.dispatch("control/update", {
                        id: this.xid,
                        name: this.xname,
                        level: this.xlevel.Nat_QcLevelID,
                        lotnumber: this.xlotnumber,
                        exp: this.xexpdate,
                        scontrol: this.xsearch,
                        sinstrument: this.$store.state.instrument.x_search,
                        sqc: this.$store.state.qc.x_search
                    })
                }
            },
            updateAlert_success(val) {
                this.$store.commit("control/update_alert_success", val)
            },
            editControl(data) {
                this.xid = data.id
                this.xname = data.name
                this.xlotnumber = data.Nat_QcControlLotNumber
                this.xexpdate = data.Nat_QcControlExpired
                this.$store.commit("control/update_level", {
                    Nat_QcLevelID: data.Nat_QcLevelID,
                    Nat_QcLevelName: data.Nat_QcLevelName
                })
                this.$store.commit("control/update_act", 'edit')
                this.$store.commit("control/update_dialog_form_control", true)
            },
            deleteControl(data) {
                this.xid = data.id
                var xdata = {
                    id: data.id,
                    name: data.name,
                    qcs: 'xxx'
                }
                this.$store.commit("control/update_selected_control", xdata)
                this.msgalert = "Yakin, mau hapus control " + data.name + "  ?"
                this.dialogdeletealert = true
            },
            changeNewControlType(value) {
                this.readonlytypecontrol = value === true ? true : false
                this.readonlytypecontrolnew = value === true ? false : true
            },
            newControlType() {
                readonlytypecontrol: true
                readonlytypecontrolnew: false
            },
            closeDeleteAlert() {
                this.$store.dispatch("control/delete", {
                    controlid: this.$store.state.control.selected_control.id,
                    xname: this.$store.state.control.selected_control.name,
                    scontrol: this.xsearch,
                    sinstrument: this.$store.state.instrument.x_search,
                    sqc: this.$store.state.qc.x_search
                })
                this.dialogdeletealert = false
            },
            thr_search: _.debounce(function () {
                this.$store.dispatch("control/lookupbyname", {
                    search: this.xsearch,
                sdate: this.$store.state.control.startdate,
                    current_page: 1,
                    lastid: -1
                })
            }, 1000),
            thr_sdate: _.debounce(function () {
                this.$store.dispatch("control/lookupbyname", {
                    search: this.xsearch,
                sdate: this.$store.state.control.startdate,
                    current_page: 1,
                    lastid: -1
                })
            }, 1000)
        },
        watch: {
            xsearch(val, old) {
                console.log(val)
                this.xsearch = val
                this.thr_search()

            },
            xstartdate(val, old) {
                console.log(val)
                this.xstartdate = val
                this.thr_sdate()
            },
            search_instrument(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.control.update_autocomplete_status == 1) return
                this.thr_search_instrument()
            }
        }
    }
</script>