<template>
    <v-layout class="mb-2" column >
        <v-dialog v-model="dialogconfirmationdelete" persistent  max-width="290">
            <v-card>
                <v-card-title
                    dark
                    class="headline error pt-2 pb-2"
                    primary-title
                    style="color:white"
                >
                    <h4 dark>Konfirmasi</h4>
                </v-card-title>
                <v-card-text>
                    {{msgconfirmationdelete}}
                </v-card-text>
                <v-card-actions>
                <v-spacer></v-spacer>
                    <v-btn small color="error darken-1 text-sm-left" flat @click="doDeleteTrx()">Hapus</v-btn>
                    <v-btn small color="primary darken-1 text-sm-right" flat @click="dialogconfirmationdelete = false">Batal</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
        <v-dialog v-model="xdialogaction" persistent  max-width="350">
            <v-card>
                <v-card-title color="warning"  class="headline">Konfirmasi</v-card-title>
                <v-card-text v-html="xmsgaction">
                    
                </v-card-text>
                <v-card-actions>
                <v-spacer></v-spacer>
                    <v-btn color="primary darken-1" flat @click="closeDialogAction()">Ya</v-btn>
                    <v-btn color="error darken-1" flat @click="xdialogaction = false">Tutup</v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
            <v-layout  row>
                
                <v-flex  xs12>
                    <v-card>
                    <v-subheader red--text text--lighten-1> FORM SURAT JALAN
                    <span v-if="xact !== 'new'"> &nbsp;[ <span style="color:#2196F3;font-weight: 900;">{{xnumbering}}</span> ]</span>
                    <span v-if="xact !== 'new' && currStatus === 'CREATED'">
                        <v-btn small @click="releaseToCourier()" color="warning">SERAHKAN KE KURIR</v-btn>
                    </span>
                    <span v-if="xact !== 'new' && currStatus === 'RELEASEC'">
                        <v-btn small @click="receiveDoctor()" color="info">DITERIMA DOKTER</v-btn>
                    </span>
                     <span v-if="xact !== 'new' && currStatus === 'RCVDOC'">
                        <v-btn small @click="receiveFromCourier()" color="primary">TERIMA DARI KURIR</v-btn>
                    </span>
                    <span v-if="xact !== 'new' && currStatus === 'PARTDONE'">
                        <v-btn small @click="forceClose()" color="success">TUTUP TRANSAKSI MANUAL</v-btn>
                    </span>
                    <v-flex text-md-right>
                        <v-btn  v-if="xact !== 'new' && currStatus === 'CREATED'" @click="deleteTrx()" small color="error">Hapus</v-btn>
                        <v-btn  v-if="( xact !== 'new' && currStatus === 'CREATED') || xact === 'new'" @click="saveTrx()" small color="primary">Simpan</v-btn>
                    </v-flex>
                    </v-subheader>
                    <v-divider></v-divider>
                    <v-layout class="pb-2" row wrap>
                        <v-flex xs12 pa-2>
                            <v-layout row>
                                <v-flex xs2 pa-1>
                                    <v-menu class="pr-2"
                                        v-model="menutrxdate"
                                        :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="filterComputedDateFormattedTrx"
                                            label="Tanggal Transaksi*"
                                            :disabled="xact === 'edit'"
                                            readonly
                                            v-on="on"
                                            @blur="date = deFormatedDate(filterComputedDateFormattedTrx)"
                                            ></v-text-field>
                                        </template>
                                        <v-date-picker v-model="xtrxdate" no-title @input="menutrxdate = false"></v-date-picker>
                                    </v-menu>
                                </v-flex>
                                <v-flex pa-1 xs3>
                                    <v-select
                                        item-text="name"
                                        return-object
                                        :items="xdoctors" 
                                        v-model="xselecteddoctor"
                                        :disabled="xact === 'edit'"
                                        label="Dokter*"
                                    ></v-select>
                                    <p  v-if="checkError('requiredoctor')" class="error pl-2 pr-2" style="color:#fff">Dokter dipilih dulu dong</p>
                                </v-flex>
                                <v-flex pa-1 xs4>
                                    <v-select
                                        item-text="name"
                                        return-object
                                        :disabled="xact === 'edit'"
                                        :items="xdoctoraddress" 
                                        v-model="xselecteddoctoraddress"
                                        label="Alamat Dokter*"
                                    ></v-select>
                                    <p  v-if="checkError('requiredoctoraddress')" class="error pl-2 pr-2" style="color:#fff">Alamatnya juga dong dipilih</p>
                                </v-flex>
                                <v-flex pa-1 xs3>
                                    <v-select
                                        item-text="name"
                                        return-object
                                        :disabled="xact === 'edit' && currStatus !== 'CREATED'"
                                        :items="xcouriers" 
                                        v-model="xselectedcourier"
                                        label="Kurir"
                                    ></v-select>
                                    <p  v-if="checkError('requirecourier')" class="error pl-2 pr-2" style="color:#fff">Yang ngirim siapa dong ?</p>
                                </v-flex>
                            </v-layout>
                            <v-layout row>
                                <v-flex xs12 pa-1>
                                    <v-textarea
                                        filled
                                        outline
                                        :disabled="xact === 'edit' && currStatus === 'DONE'"
                                        label="Catatan"
                                        v-model="xnote"
                                        rows="3"
                                    ></v-textarea>
                                </v-flex>
                            </v-layout>
                        </v-flex>
                    </v-layout>
                    </v-card>
                    <v-layout class="pt-2 mb-2" column >
                        <v-card>
                            <v-layout  row>
                                <v-flex  xs12>
                                    <v-subheader red--text text--lighten-1> DETAIL TRANSAKSI</v-subheader>
                                    <v-divider></v-divider>
                                    <v-layout row wrap>
                                        <v-flex xs12 pa-2>
                                            <v-layout v-if="xact === 'new' || (xact !== 'new' && ( currStatus === 'CREATED' || currStatus === 'RELEASEC'))" row>
                                                <v-flex xs12 pa-1>
                                                    <v-data-table 
                                                        :headers="headers" 
                                                        :items="details"
                                                        :loading="isLoading"
                                                        hide-actions class="elevation-1">
                                                        <template slot="items" slot-scope="props">
                                                            <td class="text-xs-left pa-2">{{ props.item.labnumber }}</td>
                                                            <td class="text-xs-left pa-2">{{ props.item.patient_fullname }}</td>
                                                            <td class="text-xs-center pa-2" >{{ props.item.samplename }}</td>
                                                            <td class="text-xs-center pa-2">
                                                                <div>
                                                                    <v-btn @click="changeFlagResult(props.item)" v-if="props.item.flag_result === 'N'" small color="error"><v-icon left>close</v-icon>  Tidak</v-btn>
                                                                    <v-btn @click="changeFlagResult(props.item)" v-if="props.item.flag_result === 'Y'" flat small color="success"><v-icon left>check</v-icon> Ada</v-btn>
                                                                </div>
                                                            </td>
                                                            <td class="text-xs-center pa-2">
                                                                <div>
                                                                    <v-btn @click="changeActive(props.item)" v-if="props.item.active === 'N'" small color="error"><v-icon left>close</v-icon>  Belum dipilih</v-btn>
                                                                    <v-btn @click="changeActive(props.item)" v-if="props.item.active === 'Y'" flat small color="success"><v-icon left>check</v-icon> Sudah terpilih</v-btn>
                                                                </div>
                                                        </template>
                                                    </v-data-table>
                                                </v-flex>
                                            </v-layout>
                                            <v-layout v-if="xact !== 'new' && ( currStatus !== 'CREATED' && currStatus !== 'RELEASEC')" row>
                                                <v-flex xs12 pa-1>
                                                    <v-data-table 
                                                        :headers="headerreceive" 
                                                        :items="details"
                                                        :loading="isLoading"
                                                        hide-actions class="elevation-1">
                                                        <template slot="items" slot-scope="props">
                                                            <td class="text-xs-left pa-2">{{ props.item.labnumber }}</td>
                                                            <td class="text-xs-left pa-2">{{ props.item.patient_fullname }}</td>
                                                            <td class="text-xs-center pa-2" >{{ props.item.samplename }}</td>
                                                            <td class="text-xs-center pa-2">
                                                                <div>
                                                                    <v-btn @click="changeFlagResultReceive(props.item)" v-if="props.item.flag_result_receive === 'N'" small color="error"><v-icon left>close</v-icon>  Tidak</v-btn>
                                                                    <v-btn @click="changeFlagResultReceive(props.item)" v-if="props.item.flag_result_receive === 'Y'" flat small color="success"><v-icon left>check</v-icon> Ada</v-btn>
                                                                </div>
                                                        </template>
                                                    </v-data-table>
                                                </v-flex>
                                            </v-layout>
                                        </v-flex>
                                    </v-layout>
                                </v-flex>
                            </v-flex>          
                        </v-card>
                        
                    </v-layout>
                </v-flex>
            </v-flex>          
    </v-layout>
</template>

<style scoped>
    table, td, th {  
    border: 1px solid #ddd;
    text-align: left;
    }

    table {
    border-collapse: collapse;
    width: 100%;
    }

    th, td {
    padding-top: 5px;
    padding-bottom: 5px;
    padding-left: 8px;
    padding-right: 5px;
    }

    .mini-input .v-input{
        margin-top: 0px; 
    }

    .mini-input .v-input, .mini-input .v-input--selection-controls,.mini-input .v-input__slot{
        margin-top: 0px; 
        margin-bottom:0px;
        margin-left:3px;
    }
    .mini-input .v-messages{
        min-height:0px;
    }
    input.fhm-input{
        border: 1px solid black; 
        border-radius: 2px;
        -webkit-box-shadow: 
        inset 0 0 2px  rgba(0,0,0,0.1),
                0 0 4px rgba(0,0,0,0.1); 
        -moz-box-shadow: 
        inset 0 0 2px  rgba(0,0,0,0.1),
                0 0 4px rgba(0,0,0,0.1); 
        box-shadow: 
        inset 0 0 2px  rgba(0,0,0,0.1),
                0 0 4px rgba(0,0,0,0.1); 
        padding: 2px 4px;
        background: rgba(255,255,255,0.5);
        margin: 0 0 1px 0;
        width:30px;
        text-align:center;
    }
</style>

<script>
module.exports = {
    data: () => ({
        menutrxdate:false,
        date: new Date().toISOString().substr(0, 10),
        isLoading:false,
        headers: [
            {
               text: "NOLAB",
               align: "left",
               sortable: false,
               width: "15%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "PASIEN",
               align: "left",
               sortable: false,
               width: "20%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "SAMPLE",
               align: "center",
               sortable: false,
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "LAMPIRAN HASIL",
               align: "center",
               sortable: false,
               width: "20%",
               class: "pa-2 blue lighten-3 white--text"
            },

            {
               text: "AKSI",
               align: "center",
               sortable: false,
               width: "20%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ],
         headerreceive: [
            {
               text: "NOLAB",
               align: "left",
               sortable: false,
               width: "15%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "PASIEN",
               align: "left",
               sortable: false,
               width: "20%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "SAMPLE",
               align: "center",
               sortable: false,
               width: "10%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "LAMPIRAN HASIL",
               align: "center",
               sortable: false,
               width: "20%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ],
    }),
    computed: {
        xact: {
            get() {
                return this.$store.state.sample.act
            },
            set(val) {
                this.$store.commit("sample/update_act",val)
            }
        },
        xnumbering(){
            if(this.$store.state.sample.selected_transaction)
                return this.$store.state.sample.selected_transaction.trx_numbering
            else
                return '-' 
        },
        currStatus(){
            if(this.$store.state.sample.selected_transaction)
                return this.$store.state.sample.selected_transaction.status
            else
                return 'NONE' 
        },
        xselectedtransaction(){
             return this.$store.state.sample.selected_transaction
        },
        xtrxdate : {
            get() {
                return this.$store.state.sample.trx_date
            },
            set(val) {
                this.$store.commit("sample/update_trx_date",val)
            }
        },
        dialogconfirmationdelete:{
            get() {
                return this.$store.state.sample.dialog_confirmation_delete
            },
            set(val) {
                this.$store.commit("sample/update_dialog_confirmation_delete",val)
            }
        },
        msgconfirmationdelete(){
            return  this.$store.state.sample.msg_confirmation_delete
        },
        filterComputedDateFormattedTrx () {
            return this.formatDate(this.xtrxdate)
        },
        xselecteddoctor: {
            get() {
                return this.$store.state.sample.selected_doctor
            },
            set(val) {
                this.$store.commit("sample/update_selected_doctor",val)
                this.$store.dispatch("sample/getdoctoraddress",{
                    id:val.id
                })
               
            }
        },
        xdoctors(){
            return this.$store.state.sample.doctors
        },
        xselecteddoctoraddress: {
            get() {
                return this.$store.state.sample.selected_doctor_address
            },
            set(val) {
                this.$store.commit("sample/update_selected_doctor_address",val)
                this.$store.dispatch("sample/getordersamples",{
                    doctorid:this.$store.state.sample.selected_doctor.id,
                    doctoraddressid:val.id
                })
            }
        },
        xdoctoraddress(){
            return this.$store.state.sample.doctor_address
        },
        xselectedcourier: {
            get() {
                return this.$store.state.sample.selected_courier
            },
            set(val) {
                this.$store.commit("sample/update_selected_courier",val)
                
            }
        },
        xcouriers(){
            return this.$store.state.sample.couriers
        },
        xnote: {
            get() {
                return this.$store.state.sample.trx_note
            },
            set(val) {
                this.$store.commit("sample/update_trx_note",val)
            }
        },
        details(){
            return this.$store.state.sample.details
        },
        xdialogaction:{
            get() {
                return this.$store.state.sample.dialog_action
            },
            set(val) {
                this.$store.commit("sample/update_dialog_action",val)
            }
        },
        xmsgaction:{
            get() {
                return this.$store.state.sample.msg_action
            },
            set(val) {
                this.$store.commit("sample/update_msg_action",val)
            }
        }
    },
    methods : {
        checkError(value){
            var errors = this.$store.state.sample.errors
            if(errors.includes(value)){
                return true
            }
            else{
                return false
            }
        },
        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')}`
        },
        saveTrx(){
            this.$store.commit("sample/update_errors",[])
            var errors = this.$store.state.sample.errors
            if(_.isEmpty(this.xselecteddoctor)){
                errors.push("requiredoctor")
            }
            if(_.isEmpty(this.xselecteddoctoraddress)){
                errors.push("requiredoctoraddress")
            }
            if(errors.length === 0){
                if(this.$store.state.sample.details.length > 0){
                    var details = this.$store.state.sample.details
                    var prm = {}
                    console.log(this.xact)
                    if(this.xact === 'new')
                        prm.trx_id = 0
                    else
                        prm.trx_id = this.$store.state.sample.selected_transaction.trx_id

                    console.log(prm.trx_id)
                    prm.details = details
                    prm.trx_date = this.xtrxdate
                    prm.trx_note = this.xnote
                    prm.doctor = this.xselecteddoctor
                    prm.doctoraddress = this.xselecteddoctoraddress
                    prm.courier = this.xselectedcourier
                    prm.startdate = this.$store.state.sample.start_date
                    prm.enddate = this.$store.state.sample.end_date
                    prm.search  = this.$store.state.sample.name_lab
                    prm.stationid = this.$store.state.sample.selected_station.id
                    prm.lastid = -1
                    this.$store.dispatch("sample/save",prm)
                }
                else{
                    var msg = "Belum ada sample yang dipilih dong "
                    this.$store.commit("sample/update_msg_info",msg)
                    this.$store.commit("sample/update_open_dialog_info",true)
                }
            }
        },
        changeFlagResult(value){
            console.log('changeFlagResult')
            if(this.xact === 'new' || (this.xact !== 'new' && this.currStatus === 'CREATED')){
                let arr = this.$store.state.sample.details
                var idx = _.findIndex(arr, item => item.orderid === value.orderid && item.sampleid === value.sampleid)
                arr[idx].flag_result = value.flag_result === 'N' ? 'Y':'N'
                this.$store.commit("sample/update_details",arr)
            }
        },
        changeActive(value){
            console.log('changeActive')
            if(this.xact === 'new' || (this.xact !== 'new' && this.currStatus === 'CREATED')){
                let arr = this.$store.state.sample.details
                var idx = _.findIndex(arr, item => item.orderid === value.orderid && item.sampleid === value.sampleid)
                arr[idx].active = value.active === 'N' ? 'Y':'N'
                this.$store.commit("sample/update_details",arr)
            }
        },
        changeFlagImageReceive(value){
            console.log('changeFlagImageReceive')
            if(this.xact !== 'new' && this.currStatus !== 'DONE'){
                let arr = this.$store.state.sample.details
                var idx = _.findIndex(arr, item => item.orderid === value.orderid && item.sampleid === value.sampleid)
                arr[idx].flag_image_receive = value.flag_image_receive === 'N' ? 'Y':'N'
                this.$store.commit("sample/update_details",arr)
            }
        },
        changeFlagResultReceive(value){
            console.log('changeFlagResultReceive')
            if(this.xact !== 'new' && this.currStatus !== 'DONE'){
                let arr = this.$store.state.sample.details
                var idx = _.findIndex(arr, item => item.orderid === value.orderid && item.sampleid === value.sampleid)
                arr[idx].flag_result_receive = value.flag_result_receive === 'N' ? 'Y':'N'
                this.$store.commit("sample/update_details",arr)
            }
        },
        deleteTrx(){
            var trx = this.$store.state.sample.selected_transaction
            console.log(trx)
            let msg = "Yakin, akan menghapus data transaksi "+trx.trx_numbering+" ?"
            this.$store.commit("sample/update_msg_confirmation_delete",msg)
            this.$store.commit("sample/update_dialog_confirmation_delete",true)
        },
        doDeleteTrx(){
            var trx = this.$store.state.sample.selected_transaction
            this.$store.dispatch("sample/deletetrx",trx)
        },
        releaseToCourier(){
            var trx = this.$store.state.sample.selected_transaction
            var msg = "Anda yakin akan menyerahkan dokumen transaksi "+trx.trx_numbering+" ke kurir ? "
            this.$store.commit("sample/update_msg_action",msg)
            this.$store.commit("sample/update_act",'RELEASEC')
            this.$store.commit("sample/update_dialog_action",true)
        },
        receiveDoctor(){
            var trx = this.$store.state.sample.selected_transaction
            var msg = "Dokumen transaksi "+trx.trx_numbering+" telah diterima dokter ? "
            this.$store.commit("sample/update_msg_action",msg)
            this.$store.commit("sample/update_act",'RCVDOC')
            this.$store.commit("sample/update_dialog_action",true)
        },
        receiveFromCourier(){
            var trx = this.$store.state.sample.selected_transaction
            var msg = "Dokumen transaksi "+trx.trx_numbering+" diterima dari kurir ? "
            this.$store.commit("sample/update_msg_action",msg)
            this.$store.commit("sample/update_act",'DONE')
            this.$store.commit("sample/update_dialog_action",true)
        },
        forceClose(){
            var trx = this.$store.state.sample.selected_transaction
            var msg = "Dokumen transaksi "+trx.trx_numbering+" ditutup ? "
            this.$store.commit("sample/update_msg_action",msg)
            this.$store.commit("sample/update_act",'FORCEDONE')
            this.$store.commit("sample/update_dialog_action",true)
        },
        closeDialogAction(){
            var act = this.$store.state.sample.act
            var trx = this.$store.state.sample.selected_transaction
            let arrtrx = this.$store.state.sample.transactions
            var idx = _.findIndex(arrtrx, item => item.trx_id === trx.trx_id)
            var prm = {}
            prm.act = act
            prm.trx = trx
            prm.startdate = this.$store.state.sample.start_date
            prm.enddate = this.$store.state.sample.end_date
            prm.search  = this.$store.state.sample.name_lab
            prm.stationid = this.$store.state.sample.selected_station.id
            prm.lastid = idx
            prm.details = this.$store.state.sample.details
            prm.trx_note = this.xnote
            this.$store.dispatch("sample/doaction",prm)
        },
    }
}
</script>
