<template>
        <v-layout column pb-2>
            <v-card class="mb-2">
                <v-layout row pa-2  align-center wrap >
                    <table>
                        <tr>
                            <th class="text-md-center pt-2 pb-2"> NOTA </th>
                            <th class="text-md-center pt-2 pb-2"> TIPE PEMBAYARAN </th>
                            <th class="text-md-center pt-2 pb-2">JUMLAH</th>
                            <th class="text-md-center pt-2 pb-2">USER</th>
                            <th class="text-md-center pt-2 pb-2">AKSI</th>
                        </tr>
                        <tr class="mini-input" v-if="notes.length > 0" v-for="(note,index) in notes">
                            <td width="25%" v-bind:class="{ 'red--text':note.note_active === 'N','primary--text':note.note_amount < 0}"  style="text-align:center;vertical-align:center;" align="center" >{{note.note_number}}</td>
                            <td width="25%" v-bind:class="{ 'red--text':note.note_active === 'N','primary--text':note.note_amount < 0}" class="text-md-center pl-3 pr-3">{{note.paymenttypes_name}}</td>
                            <td width="15%" v-bind:class="{ 'red--text':note.note_active === 'N','primary--text':note.note_amount < 0}" class="text-md-right pl-3 pr-3">{{convertMoney(note.note_amount)}}</td>
                            <td width="10%" v-bind:class="{ 'red--text':note.note_active === 'N','primary--text':note.note_amount < 0}" class="text-md-center pr-2">{{note.note_user}}</td>
                            <td class="text-md-center">
                                
                                <span @click="printNotered(note,index)"  class="icon-medium-fill-base xs1 white--text red darken-1 icon-print" title="nota internal"></span>
                            </td>
                        </tr>
                        <tr class="mini-input" v-if="notes.length ===  0">
                            <td colspan="5" class="text-md-center pr-2">
                                Tidak ada data
                            </td>
                        </tr>
                    </table>
                </v-layout>
                <v-layout style="border-top:1px dashed rgb(221,221,221)" row mt-1 mb-1></v-layout>
                <!--<v-layout row mt-1 pt-2 mb-3 pl-2 pr-2>
                    <v-flex xs4>
                        <v-select
                            :search-input.sync="searchPrint"
                            autocomplete
                            v-model="selected_typeprint"
                            :items="typeprints"
                
                            auto-select-first
                            item-text = "name"
                            return-object
                            label="Tipe Print"
                            hide-details
                            box
                            >
                            
                        </v-select>
                  
                    </v-flex>
                    <v-flex xs2>
                        <span @click="typePrintNow()"  class="icon-medium-fill-base white--text warning darken-1 icon-print"></span>
                    </v-flex>   
                    <v-flex class="text-xs-right" xs6>
                        <v-btn class="text-md-center" @click="openSettingControl()" color="orange" dark>
                            Kartu Kontrol
                        </v-btn>
                    </v-flex>
                </v-layout>-->
          
            </v-card>
            <v-card >
                <v-layout row pa-2 align-center wrap >
                    <v-flex xs6>
                        <v-layout row>
                            <v-flex xs12>
                                <div class="label-tagihan text-xs-left">Total Tagihan</div>
                            </v-flex>
                        </v-layout>
                        <v-layout pt-1 row>
                            <v-flex xs9>
                                <div class="text-xs-left warning--text">Minimun DP ({{xmindp_percent}}%)</div>
                            </v-flex>
                            <v-flex xs3>
                                <div class="text-xs-right warning--text">{{convertMoney(xmindp_amount)}}</div>
                            </v-flex>
                        </v-layout>
                    </v-flex>
                    <v-flex xs6>
                       <div class="text-tagihan text-xs-right"><kbd>{{convertMoney(restbill)}}</kbd></div>
                    </v-flex>
                </v-layout>
                <v-layout style="border-top:1px dashed rgb(221,221,221)" row mt-1 mb-1></v-layout>
                <div v-for="(type, index) in types">
                    <v-layout row pt-2 pb-1 pl-2 align-center wrap >
                        <v-flex xs12>
                            <v-switch
                                v-model="type.chex"
                                @change="updateChx(type,index)"
                                :label="type.chexlabel"
                            ></v-switch>
                        </v-flex>
                    </v-layout>
                    <v-layout pl-2 row align-center wrap >
                            <v-flex xs4 class="pa-1">
                                <!--<input type="text" @change="updateTotal()" :class="{ 'disabled-background':type.chex === false }" :disabled="!type.chex"  v-model="type.leftvalue" class="input-plain text-xs-right font-weight-bold"/>-->
                                <v-text-field
                                    label="Jumlah"
                                    :disabled="!type.chex"  v-model="type.leftvalue"
                                    @change="updateTotal()"
                                    class="amount-right"
                                    reverse
                                    outline
                                ></v-text-field>
                            </v-flex>
                            <v-flex xs4 v-if="type.code === 'CASH' || type.code === 'VOUCHER'" class="pa-1">
                                <v-text-field
                                    v-if="type.code === 'CASH'"
                                    label="Kembali"
                                    :disabled="!type.chex"  v-model="type.rightvalue"
                                    class="amount-right"
                                    reverse
                                    @change="updateTotal()"
                                    outline
                                ></v-text-field>
                                <v-text-field
                                    v-if="type.code === 'VOUCHER'"
                                    label="Kode Voucher"
                                    :disabled="!type.chex"  v-model="type.rightvalue"
                                    class="amount-right"
                                    outline
                                ></v-text-field>
                            </v-flex>
                            <v-flex xs4 v-if="type.code === 'DEBIT' || type.code === 'CREDIT' || type.code === 'TRANSFER'" class="pa-1">
                                <v-autocomplete
                                    v-if="type.code !== 'TRANSFER'"
                                    label="Kartu"
                                    v-model="type.selected_card"
                                    @change="changeCard('card',type.selected_card,index)"
                                    :disabled="!type.chex"
                                    :items="banks"
                                    auto-select-first
                                    no-filter
                                    outline
                                    item-text="name"
                                    item-value="id"
                                    return-object
                                    no-data-text=""
                                    >
                                </v-autocomplete>
                                <v-autocomplete
                                    v-if="type.code === 'TRANSFER'"
                                    label="Nomor Rekening"
                                    v-model="type.selected_account"
                                    @change="changeCard('account',type.selected_account,index)"
                                     :disabled="!type.chex"
                                    :items="accounts"
                                    auto-select-first
                                    outline
                                    no-filter
                                    item-text="name"
                                    item-value="id"
                                    return-object
                                    no-data-text=""
                                    >
                                </v-autocomplete>
                            </v-flex>
                            <v-flex xs4 v-if="type.code === 'DEBIT' || type.code === 'CREDIT'" class="pa-1">
                                <v-autocomplete
                                    label="EDC"
                                    v-model="type.selected_edc"
                                    @change="changeCard('edc',type.selected_edc,index)"
                                     :disabled="!type.chex"
                                    :items="accounts"
                                    auto-select-first
                                    outline
                                    no-filter
                                    item-text="name"
                                    item-value="id"
                                    return-object
                                    no-data-text=""
                                    >
                                </v-autocomplete>
                            </v-flex>
                    </v-layout>
                    <v-layout style="border-top:1px dashed rgb(221,221,221)" row mt-1 mb-1></v-layout>
                </div>
                <v-layout row pa-2 align-center wrap >
                    <v-flex xs6>
                        <div class="label-tagihan text-xs-left">
                            <v-btn v-if="!save_process" @click="pay()" color="warning" dark>
                                Bayar
                            </v-btn>
                        </div>
                    </v-flex>
                    <v-flex xs6>
                       <div class="text-tagihan text-xs-right"><kbd>{{convertMoney(totpay)}}</kbd></div>
                    </v-flex>
                </v-layout>
            </v-card>
            <template>
                <v-dialog
                v-model="xdialogatasnama"
                max-width="30%"
                persistent
                >
                    <v-card>
                        <v-card-title
                            class="headline success pt-2 pb-2"
                            primary-title
                        >
                        <h4 style="color:#FFEBEE">Kwitansi</h4>
                        </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-text-field
                                                   label="Telah Terima Dari"
                                                   v-model="pembayaran_atas_nama"
                                                   outline
                                               >
                                               </v-text-field>
                                            </v-layout>
                                            <v-layout row>
                                                <v-select
                                                    item-text="name"
                                                    return-object
                                                    :items="languages" 
                                                    v-model="selected_language"
                                                    label="Pilih Bahasa"
                                                ></v-select>
                                            </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
                                flat
                                @click="printKwV2()"
                            >
                            Print
                            </v-btn>
                            <v-btn
                                color="error"
                                flat
                                @click="xdialogatasnama = false"
                            >
                            Tutup
                            </v-btn>
                        </v-card-actions>
                    </v-card>
                </v-dialog>
            </template>


            <template>
                <v-dialog
                v-model="xdialogpaysuccess"
                max-width="30%"
                persistent
                >
                    <v-card>
                        <v-card-title
                            class="headline success pt-2 pb-2"
                            primary-title
                        >
                        <h4 style="color:#FFEBEE">Pembayaran Berhasil</h4>
                        </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 v-html="xmsgpaysuccess" xs12>

                                                </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
                                flat
                                @click="doPrint()"
                            >
                            Print
                            </v-btn>
                            <v-btn
                                color="error"
                                flat
                                @click="closeDialogPaySuccess(false)"
                            >
                            Tutup
                            </v-btn>
                        </v-card-actions>
                    </v-card>
                </v-dialog>
            </template>

            <template>
                <v-dialog v-model="dialog_languages" width="350px">
                    <v-layout row>
                        <v-flex xs12>
                            <v-card>
                                <v-card-text>
                                    <v-select
                                        item-text="name"
                                        return-object
                                        :items="languages" 
                                        v-model="selected_language"
                                        label="Pilih Bahasa"
                                    ></v-select>
                                </v-card-text>
                                <v-card-actions>
                                <v-spacer></v-spacer>
                                <v-btn
                                    color="info"
                                    flat
                                    @click="selectedLanguage()"
                                >
                                Pilih
                                </v-btn>

                            </v-card-actions>
                            </v-card>
                        </v-flex>
                    </v-layout>
                </v-dialog>
                <v-dialog
                v-model="xdialogdelete"
                persistent
                max-width="30%"
                >
                    <v-card>
                        <v-card-title
                            :class="{ 'red':!_.isEmpty(xnotadelete), 'success':_.isEmpty(xnotadelete) }"
                            class="headline darken-1 pt-2 pb-2"
                            primary-title
                        >
                        <h4 style="color:#FFEBEE">
                        <span v-if="!_.isEmpty(xnotadelete)">Peringatan !</span>
                        <span v-if="_.isEmpty(xnotadelete)">Berhasil !</span>
                        </h4>
                        </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 v-html="xmsgdelete" xs12>

                                                </v-flex>
                                            </v-layout>
                                            <v-layout v-if="!_.isEmpty(xnotadelete)" row>
                                                <v-flex pt-2 pr-2 xs12>
                                                    <input style="border: 1px solid black;padding: 5px;width: 100%;" type="text" placeholder="Catatan (*Wajib diisi)" v-model="xnotedelete" class="input-plain"/>
                                                </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="error"
                                v-if="!_.isEmpty(xnotadelete) && xnotedelete "
                                flat
                                @click="doDeleteNote()"
                            >
                            Yakin dong !
                            </v-btn>

                            <v-btn
                                v-if="!_.isEmpty(xnotadelete)"
                                color="primary"
                                flat
                                @click="xdialogdelete = false"
                            >
                            Tutup
                            </v-btn>
                            <v-btn
                                v-if="_.isEmpty(xnotadelete)"
                                color="primary"
                                flat
                                @click="closeDialogDelete()"
                            >
                            Tutup
                            </v-btn>
                        </v-card-actions>
                    </v-card>
                </v-dialog>

            </template>
            <v-dialog  v-model="setting_controll"
                max-width="60%"
                persistent
            >
                <v-card>
                        <v-card-title
                            class="headline success pt-2 pb-2"
                            primary-title
                        >
                        <h4 style="color:#FFEBEE">Kartu Kontrol</h4>
                        </v-card-title>
                        <v-card-text class="pt-2 pb-2 ma-2">
                            <v-layout row>
                                <v-flex xs6 ma-2>
                                    <h4>SAMPLE STATION</h4>
                                </v-flex>
                                <v-flex xs6 ma-2>
                                    <h4>LOKASI</h4>
                                </v-flex>
                            </v-layout>
                            <v-layout v-for="(sl,sl_idx) in data_station_location" :key="sl_idx" row>
                                <v-flex xs6 ma-2>
                                    <v-text-field
                                        :value="sl.station_name"
                                    ></v-text-field>
                                </v-flex>
                                <v-flex xs6 ma-2>
                                    <v-select
                                        v-model="sl.location_id"
                                        :items="sl.locations"
                                        hide-details
 
                                        item-text="location_name"
                                        item-value="location_id"
                                        label="Pilih Lokasi"
                                        ></v-select>
                                </v-flex>
                            </v-layout>
                        </v-card-text>
                        <v-divider></v-divider>
                        <v-card-actions>
                            <v-spacer></v-spacer>
                            
                            <v-btn
                                color="error"
                                flat
                                @click="setting_controll = false"
                            >
                            Tutup
                            </v-btn>
                            <v-btn
                                flat
                                @click="PrintControlCard()"
                            >
                            Print
                            </v-btn>
                        </v-card-actions>
                    </v-card>
            </v-dialog>
            <one-dialog-print :title="printtitle" :width="printwidth" :height="550" :status="openprintnote" :urlprint="urlprintnote" @close-dialog-print="openprintnote = false"></one-dialog-print>
            <one-dialog-print-red :title="printtitle" :width="printwidth" :height="550" :status="openprintnotered" :urlprint="urlprintnote" @close-dialog-print="openprintnotered = false"></one-dialog-print-red>
            <one-dialog-info :status="opendialoginfo" :msg="msginfo" @close-dialog-info="opendialoginfo = false"></one-dialog-info>
        </v-layout>
</template>
<style scoped>
    .label-tagihan{
        text-align:left;
        font-size: 25px;
        font-family: open sans, tahoma, sans-serif;
        font-weight:700;
    }

    .sub-header{
        text-align:left;
        font-size: 18px;
        font-family: open sans, tahoma, sans-serif;
        font-weight:700;
    }

    .sub-title{
        text-align:left;
        font-size: 14px;
        font-family: open sans, tahoma, sans-serif;
        font-weight:700;
    }

    .amount-right{
        text-align:right!important;
        font-size: 14px;
        font-family: open sans, tahoma, sans-serif;
        font-weight:700;
    }

    .text-tagihan{
        text-align:left;
        font-size: 42px;
        font-family: open sans, tahoma, sans-serif;
    }

    .disabled-background{
        background:#b7b7b7;
    }

    .input-cash{
        width: 100%;
        padding: 8px 14px;
        box-sizing: border-box;
        border: 2px solid grey;
        border-radius: 4px;
        font-size: 22px;
        font-weight:700;
        text-align:right;
    }
    .input-plain{
        width: 100%;
        padding: 4px 8px;
        box-sizing: border-box;
        border: 2px solid grey;
        border-radius: 4px;
        font-size: 14px;
    }
    .v-input, .v-input__slot, .v-messages{
        margin:0px;
        padding:0px;
        min-height: 0px;
    }
    .v-input--selection-controls:not(.v-input--hide-details) .v-input__slot {
        margin-bottom: 0px;
    }
    table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
    background:white;
    border: 0px;
    }

    th, td {
    border: 1px solid black;
    border-collapse: collapse;
    padding-top: 2px;
    padding-bottom: 2px;
    }
    table>tr>td {
    padding: 8px;
    }
    table>tr>td:first {
    padding-left:15px!important;
    }
    .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;
    }

    .border-bottom-dashed{
        border-bottom : 1px dashed rgba(0,0,0,.12);
    }
</style>
<script>
module.exports = {
    components : {
        'one-dialog-info':httpVueLoader('../../common/oneDialogInfo.vue'),
        'one-field-verification' : httpVueLoader('../../common/oneFieldVerificationSupply.vue'),
        'one-dialog-print':httpVueLoader('../../common/oneDialogPrintX.vue'),
        'one-dialog-print-red':httpVueLoader('../../common/oneDialogPrintX.vue')
    },
    data () {
      return {
        searchPrint:'',
        selected_typeprint:{},
        typeprints:[
            {id:'inv1',name:'Invoice I'},
            {id:'inv2',name:'Invoice II'},
            {id:'bp',name:'Bukti Pemeriksaan I'},
            {id:'bp1',name:'Bukti Pemeriksaan II'},
            {id:'bp2',name:'Bukti Pemeriksaan III'},
            {id:'kw1',name:'Kwitansi I'},
            {id:'kw2',name:'Kwitansi II'},
            //{id:'kkp',name:'KKP'},
            {id:'ic',name:'Inform Concern'}

        ],
          dialog_languages:false,
          type_dialog_lang:'',
          data_dialog_language:{},
        act_printkw:'',
        xdialogatasnama: false,
        pembayaran_atas_nama: '',
        checkbox: true,
        radioGroup: 1,
        switchCash: true,
        switchDebit: false,
        switchKredit: false,
        dialog:false,
        urlprintnotered:'',
        printtitle:'',
        printwidth:'60%',
        search_card:'',
        search_edc:'',
        isLoading:false,
        fajrihidebtnpay:'Y'
      }
    },
    mounted() {
      this.$store.dispatch("paymentnew/lookup_type")
      this.$store.dispatch("paymentnew/lookup_banks")
      this.$store.dispatch("paymentnew/lookup_accounts")
      this.$store.dispatch("paymentnew/getlanguages")
    },
    computed: {
        urlprintnote: {
            get () {
                return this.$store.state.paymentnew.urlprintnote
            },
            set (v) {
                this.$store.commit('paymentnew/update_urlprintnote', v)
            }
        },
        setting_controll: {
            get () {
                return this.$store.state.paymentnew.setting_controll
            },
            set (v) {
                this.$store.commit('paymentnew/update_setting_controll', v)
            }
        },
        data_station_location : {
            get () {
                return this.$store.state.paymentnew.data_station_location
            },
            set (v) {
                this.$store.commit('paymentnew/update_data_station_location', v)
            }
        },
        languages: {
            get() {
                return this.$store.state.paymentnew.languages
            },
            set(val) {
                this.$store.commit("paymentnew/update_languages",val)
            }
        },
        selected_language: {
            get() {
                return this.$store.state.paymentnew.selected_language
            },
            set(val) {
                this.$store.commit("paymentnew/update_selected_language",val)
                if(val.code == 'ID'){
                    this.pembayaran_atas_nama = this.$store.state.patient.selected_patient.M_PatientName
                }else{
                    this.pembayaran_atas_nama = this.$store.state.patient.selected_patient.M_PatientName_eng
                }
            }
        },
        opendialoginfo: {
            get() {
                return this.$store.state.paymentnew.open_dialog_info
            },
            set(val) {
                this.$store.commit("paymentnew/update_open_dialog_info",false)
            }
        },
        msginfo: {
            get() {
                return this.$store.state.paymentnew.msg_info
            },
            set(val) {
                this.$store.commit("paymentnew/update_msg_info",false)
            }
        },
        save_process(){
            return this.$store.state.paymentnew.save_process
        },
        cards(){
            return this.$store.state.paymentnew.cards
        },
        banks(){
            return this.$store.state.paymentnew.banks
        },
        accounts(){
            return this.$store.state.paymentnew.accounts
        },
        xmindp_percent(){
            if(this.$store.state.patient.selected_patient){
                return this.$store.state.patient.selected_patient.mindp_percent
            }
            else{
                return 0
            }
        },
        xmindp_amount(){
            if(this.$store.state.patient.selected_patient){
                return this.$store.state.patient.selected_patient.mindp_amount
            }
            else{
                return 0
            }
        },
        notes(){
            return this.$store.state.paymentnew.notes
        },
        xdialogpaysuccess(){
            return this.$store.state.paymentnew.dialog_pay_success
        },
        xmsgpaysuccess(){
            return this.$store.state.paymentnew.paynumber
        },
        types() {
            return this.$store.state.paymentnew.types
        },
        totpay() {
            return this.$store.state.paymentnew.total_payment
        },
        restbill(){
            if(this.$store.state.patient.patients){
                if(this.$store.state.patient.selected_patient && this.$store.state.patient.selected_patient.unpaid)
                    return this.$store.state.patient.selected_patient.unpaid
                else
                    return 0
            }
            else{
                return 0
            }

        },
        xdialogdelete:{
            get() {
                return this.$store.state.paymentnew.dialog_delete
            },
            set(val) {
                this.$store.commit("paymentnew/update_dialog_delete",val)
            }
         },
         xmsgdelete(){
             return this.$store.state.paymentnew.msg_delete
         },
         xnotadelete(){
             return this.$store.state.paymentnew.nota_delete
         },
         xnotedelete:{
            get() {
                return this.$store.state.paymentnew.note_delete
            },
            set(val) {
                this.$store.commit("paymentnew/update_note_delete",val)
            }
         },
         openprintnote: {
            get() {
                return this.$store.state.paymentnew.open_print_note
            },
            set(val) {
                this.$store.commit("paymentnew/update_open_print_note",false)
            }
        },
        openprintnotered: {
            get() {
                return this.$store.state.paymentnew.open_print_note
            },
            set(val) {
                this.$store.commit("paymentnew/update_open_print_note",false)
            }
        },
        selectedpatient(){
            return this.$store.state.patient.selected_patient
        },
        xdisable_btn_pay: {
            get() {
                return this.$store.state.paymentnew.disable_btn_pay
            },
            set(val) {
                this.$store.commit("paymentnew/update_disable_btn_pay",val)
            }
        },

    },
    methods : {
        PrintControlCard(){
            this.printwidth = 800
             this.printtitle = ""
            this.$store.dispatch('paymentnew/save_control', {data:this.data_station_location})
        },
        openSettingControl(){
            this.$store.dispatch("paymentnew/getLocations");
        },
        typePrintNow(){
            var selected_x = this.selected_typeprint
            if(selected_x.id === 'inv1')
            this.printInvoice()

            if(selected_x.id === 'inv2')
            this.printInvoice2()

            if(selected_x.id === 'bp')
            this.printBP()

            if(selected_x.id === 'bp1')
            this.printBP1()

            if(selected_x.id === 'bp2')
            this.printBP2()

            if(selected_x.id === 'kw1')
            this.printKw('v1')

            if(selected_x.id === 'kw2')
            this.printKw('v2')

            if(selected_x.id === 'kkp')
            this.printKkp()

            if(selected_x.id === 'ic')
            this.printIc()
        },
        changeCard(type,value,idx){
            var types = this.$store.state.paymentnew.types
            if(type === 'card')
                types[idx].selected_card = value

            if(type === 'edc')
                types[idx].selected_edc = value

            if(type === 'account')
                types[idx].selected_account = value


            this.$store.commit("paymentnew/update_types",{records:types,total:types.length})
            
            this.updateTotal()
        },
        colorbginp(value){
            if(value.chex === false){
                return 'grey'
            }
            else{
                return 'white'
            }
        },
        convertMoney(money){
            return one_money(money)
        },
        closeDialogPaySuccess(){
            let arrpatient = this.$store.state.patient.patients
            var idx = _.findIndex(arrpatient, item => item.T_OrderHeaderID === this.$store.state.patient.selected_patient.T_OrderHeaderID)
            this.$store.commit("paymentnew/update_dialog_pay_success",false)
            this.$store.commit("patient/update_selected_patient",{})
            console.log(this.$store.state.paymentnew.types)
            this.$store.dispatch("patient/search",{
                startdate:this.$store.state.patient.start_date,
                enddate:this.$store.state.patient.end_date,
                search: this.$store.state.patient.search,
                status: this.$store.state.patient.selected_status.value,
                current_page:this.$store.state.patient.current_page,
                lastidx:idx
            })
        },
        updateTotal(){
            var xval = this.$store.state.paymentnew.types
            let xcash = _.filter(xval, {code: 'CASH'})
            let xother = _.filter(xval, type => type.code !== 'CASH')
            var valother = 0

            xother.forEach(function(obj){
                valother += parseInt(obj.leftvalue)
            })
            //console.log('val other '+valother)
            let restother = this.restbill - valother

            let xchange = parseInt(xcash[0].leftvalue) - restother
            xcash[0].rightvalue = Math.max(0, xchange)
            let idxcash = _.findIndex(xval, item => item.code === 'CASH')
            xval[idxcash] = xcash[0]

            this.$store.commit("paymentnew/update_types",{records :xval,total:xval.length })
            let totpaid = valother + ( parseInt(xcash[0].leftvalue) - Math.max(0, xchange) )
            this.$store.commit("paymentnew/update_total_payment",totpaid)
        },
        pay(){
            this.$store.commit("paymentnew/update_save_process",true)
            var go_pay = true
            let xval = this.$store.state.paymentnew.types
            var valpay = 0
            var xmsginfo = ''
            let xcash = _.filter(xval, {code: 'CASH'})
            xval.forEach(function(obj){
                valpay += parseInt(obj.leftvalue)
            })
            let selected_payments = _.filter(this.$store.state.paymentnew.types, {chex: true})
            //console.log(selected_payments)
            var numberpay = 0
            selected_payments.forEach(function(xobj){
                var obj = JSON.parse(JSON.stringify(xobj))
                if(obj.code === 'CASH' && parseInt(obj.leftvalue) === 0){
                    go_pay = false
                    xmsginfo += "<p>Mau bayar pake daun ?</p>"
                }   
                else if(obj.code === 'DEBIT' && ( parseInt(obj.leftvalue) === 0 || parseInt(obj.selected_card.id) === 0 ||  parseInt(obj.selected_edc.id) === 0 )){
                    console.log(obj.code)
                    go_pay = false
                    xmsginfo += "<p>Bawain pacar bibit buah kedongdong, Kalo bayar pake debit diisi yang lengkap dong</p>"
                }   
                else if(obj.code === 'CREDIT' && ( parseInt(obj.leftvalue) === 0 || parseInt(obj.selected_card.id) === 0 ||  parseInt(obj.selected_edc.id) === 0 )){
                    go_pay = false
                    xmsginfo += "<p>Pergi berlayar pake odong-odong, Kalo bayar pake kredit diisi yang lengkap dong</p>"
                }
                else if(obj.code === 'TRANSFER' && ( parseInt(obj.leftvalue) === 0 || parseInt(obj.selected_account.id) === 0  || obj.selected_account === '')){
                    go_pay = false
                    xmsginfo += "<p>Salah bermanuver kecebur selokan dong, Kalo mau transfer diisi yang lengkap dong</p>"
                }

                if(parseInt(obj.leftvalue) !== 0){
                    numberpay = numberpay + parseInt(obj.leftvalue)
                }
                    

            })
            if(selected_payments.length > 0 && go_pay){
                console.log('sisa tagihan : '+this.restbill)
                if(parseInt(this.restbill) === 0 && numberpay > 0){
                    //console.log("klo 0 ke sini")
                    xmsginfo += "<p>Yuk kita ke penghulu, jangan kawin lari ... hayooo!!! diretur dulu, kalau mau bayar lagi</p>"

                    this.$store.commit("paymentnew/update_msg_info",xmsginfo)
                    this.$store.commit("paymentnew/update_open_dialog_info",true)
                    this.$store.commit("paymentnew/update_save_process",false)
                }
                else{
                    let prm = {coba:0,orderid:this.$store.state.patient.selected_patient.T_OrderHeaderID,payments:this.$store.state.paymentnew.types}
                    this.$store.dispatch("paymentnew/pay",prm)
                }
            }
            else{
                if(selected_payments.length == 0)
                    xmsginfo += "<p>Mau bayar pake daun ?</p>"

                this.$store.commit("paymentnew/update_msg_info",xmsginfo)
                this.$store.commit("paymentnew/update_open_dialog_info",true)
                this.$store.commit("paymentnew/update_save_process",false)
               // console.log(this.opendialoginfo)
            }
            
        },
        deleteNote(note,idx){
            this.$store.commit("paymentnew/update_note_delete","")
            this.$store.commit("paymentnew/update_nota_delete",note)
            let xmsg = "Yakin , mau hapus nota nomor <span style='color:red'>"+note.note_number+"</span> ?"
            this.$store.commit("paymentnew/update_msg_delete",xmsg)
            this.$store.commit("paymentnew/update_dialog_delete",true)
        },
        doDeleteNote(){
            let prm = {catatan:this.$store.state.paymentnew.note_delete,nota:this.$store.state.paymentnew.nota_delete}
            this.$store.dispatch("paymentnew/delete_note",prm)
        },
        closeDialogDelete(){
            let arrpatient = this.$store.state.patient.patients
            var idx = _.findIndex(arrpatient, item => item.T_OrderHeaderID === this.$store.state.patient.selected_patient.T_OrderHeaderID)
            this.$store.commit("paymentnew/update_dialog_delete",false)
            this.$store.commit("patient/update_selected_patient",{})
            this.$store.dispatch("patient/search",{
                startdate:this.$store.state.patient.start_date,
                enddate:this.$store.state.patient.end_date,
                search: this.$store.state.patient.search,
                status: this.$store.state.patient.selected_status.value,
                lastidx:idx
            })
        },
        updateChx(val,idx){
            var selpat = this.$store.state.patient.selected_patient
            var firstamount = 0
            if(selpat.unpaid)
                firstamount = selpat.unpaid
            let xobj = this.$store.state.paymentnew.types

            if((xobj[idx].code === 'DEBIT' || xobj[idx].code === 'CREDIT' || xobj[idx].code === 'TRANSFER') && val.chex === false){
                xobj[idx].selected_card = {id:0,name:''}
                xobj[idx].selected_edc = {id:0,name:''}
                xobj[idx].selected_account = {id:0,name:''}
            }

            var valother = 0
            xobj.forEach(function(obj){
                if(obj.chex && ( obj.code != val.code))
                    valother += parseInt(obj.leftvalue)
            })

            if(!val.chex){
                xobj[idx].leftvalue = 0
                var x = "Y"
                this.xdisable_btn_pay = x
                    console.log('disable '+this.xdisable_btn_pay )
            }
            else
                xobj[idx].leftvalue = firstamount - valother

            this.$store.commit("paymentnew/update_types",{records :xobj,total:xobj.length })
            this.updateTotal()
        },
        check_disable_btn_status(){
            this.xdisable_btn_pay = 'N'
            var payments = this.$store.state.paymentnew.types
            let selected_payments = _.filter(payments, {chex: true})
            console.log(this.xdisable_btn_pay)
            selected_payments.forEach(function(obj){
                if(obj.leftvalue === 0){
                    this.xdisable_btn_pay = 'Y'
                    console.log('disable '+this.xdisable_btn_pay )
                }
                if(obj.code === 'DEBIT' || obj.code === 'CREDIT' || obj.code === 'TRANSFER'){
                    console.log(obj)
                    if(obj.selected_card.id === '0' || obj.selected_card.id === 0 || obj.selected_edc.id === '0' || obj.selected_edc.id === 0 || obj.selected_account.id === '0' || obj.selected_account.id === 0){
                        this.xdisable_btn_pay = 'Y'
                        console.log('oye')
                        console.log(this.xdisable_btn_pay)
                    }
                }
            })
        },
        selectedLanguage(){
            if(this.type_dialog_lang == 'nota'){
                this.printNoteAfterLang()
            }
            if(this.type_dialog_lang == 'invoice'){
                this.printInvoiceAfterLang()
            }
        },
        printNote(val,idx){
            this.dialog_languages = true
            this.data_dialog_language = val
            this.type_dialog_lang = 'nota'
            this.selected_language = {id:'1',code:'ID',name:'Bahasa Indonesia'}
        },
        printNoteAfterLang(){
            this.dialog_languages = false
            var val = this.data_dialog_language
            //console.log(val)
            this.printtitle = ""
            let user = one_user()
            var rpt = 'rpt_t_003'
            if(val.note_amount < 0)
                rpt = 'rpt_t_004'

            if(this.selected_language.code == 'EN'){
                rpt = rpt+'_eng'
            }
            this.urlprintnote = "/birt/run?__report=report/one/fo/"+rpt+".rptdesign&__format=pdf&username="+user.M_StaffName+"&PID="+val.note_id
            this.$store.commit("paymentnew/update_open_print_note",true)
        },

        printNotered(val,idx){

            this.printtitle = ""
            let user = one_user()
            var rpt = 'rpt_kwitansi_001'
            if(val.note_amount < 0)
                rpt = 'rpt_t_004'



            this.urlprintnote = "/birt/run?__report=report/one/fo/"+rpt+".rptdesign&__format=pdf&username="+user.M_StaffName+"&PID="+val.note_id
            this.$store.commit("paymentnew/update_open_print_note",true)

        },
        doPrint(){
            this.printtitle = ""
            this.closeDialogPaySuccess()
            let user = one_user()
            let payments = this.$store.state.paymentnew.last_payments
            console.log(payments)
            //let xcash = _.filter(payments, {chex: true})
            let xcash = _.filter(payments, function(o) { return o.leftvalue < 0})
            var rpt = 'rpt_t_003'
            if(xcash.length > 0)
                rpt = 'rpt_t_004'

            console.log(xcash)
            console.log(rpt)
            this.urlprintnote = "/birt/run?__report=report/one/fo/"+rpt+".rptdesign&__format=pdf&username="+user.M_StaffName+"&PID="+this.$store.state.paymentnew.idx
            this.$store.commit("paymentnew/update_open_print_note",true)
        },
        printKw(act) {
            this.type_dialog_lang = 'kwitansi'
            this.selected_language = {id:'1',code:'ID',name:'Bahasa Indonesia'}
            this.act_printkw = act
           this.pembayaran_atas_nama = this.$store.state.patient.selected_patient.M_PatientName
           this.xdialogatasnama = true
           
        },
        printKwV2(){
            this.printwidth = 800
            this.printtitle = ""
            let idx = this.$store.state.patient.selected_patient.T_OrderHeaderID
            let user = one_user()
            let an_name = "&an=" + encodeURIComponent(this.pembayaran_atas_nama )
            var rpt = ''
            if(this.act_printkw == 'v1'){
                rpt = 'rpt_t_002'
            }
            else{
                rpt = 'rpt_t_002i'
            }
            if(this.selected_language.code == 'EN'){
                rpt = rpt+'_eng'
            }

            this.urlprintnote = "/birt/run?__report=report/one/fo/"+rpt+".rptdesign&__format=pdf&username="+user.M_StaffName+"&PID="+idx + an_name
            this.$store.commit("paymentnew/update_open_print_note",true)
        },
        printBP(){
            this.printwidth = 800
            this.printtitle = ""
            let idx = this.$store.state.patient.selected_patient.T_OrderHeaderID
            let user = one_user()
            let an_name = "&an=" + encodeURIComponent(this.pembayaran_atas_nama )
            this.urlprintnote = "/birt/run?__report=report/one/fo/rpt_t_006.rptdesign&__format=pdf&username="+user.M_StaffName+"&PID="+idx + an_name
            this.$store.commit("paymentnew/update_open_print_note",true)
        },
        printBP1(){
            this.printwidth = 800
            this.printtitle = ""
            let idx = this.$store.state.patient.selected_patient.T_OrderHeaderID
            let user = one_user()
            let an_name = "&an=" + encodeURIComponent(this.pembayaran_atas_nama )
            this.urlprintnote = "/birt/run?__report=report/one/fo/rpt_t_006i.rptdesign&__format=pdf&username="+user.M_StaffName+"&PID="+idx + an_name
            this.$store.commit("paymentnew/update_open_print_note",true)
        },
        printBP2(){
              this.printwidth = 800
              this.printtitle = ""
              let idx = this.$store.state.patient.selected_patient.T_OrderHeaderID
              let user = one_user()
              let an_name = "&an=" + encodeURIComponent(this.pembayaran_atas_nama )
              this.urlprintnote = "/birt/run?__report=report/one/fo/rpt_t_006ii.rptdesign&__format=pdf&username="+user.M_StaffName+"&PID="+idx + an_name
              this.$store.commit("paymentnew/update_open_print_note",true)
          },
        printInvoice(){
            this.dialog_languages = true
            this.type_dialog_lang = 'invoice'
            this.selected_language = {id:'1',code:'ID',name:'Bahasa Indonesia'}
        },
        printInvoiceAfterLang(){
            this.dialog_languages = false
            this.printwidth = 800
            this.printtitle = ""
            let idx = this.$store.state.patient.selected_patient.T_OrderHeaderID
            let user = one_user()
            var rpt = 'rpt_t_001'
            if(this.selected_language.code == 'EN'){
                rpt = rpt+'_eng'
            }
            this.urlprintnote = "/birt/run?__report=report/one/fo/"+rpt+".rptdesign&__format=pdf&username="+user.M_StaffName+"&PID="+idx
            this.$store.commit("paymentnew/update_open_print_note",true)
        },
         printInvoice2(){
          this.printwidth = 800
          this.printtitle = ""
          let idx = this.$store.state.patient.selected_patient.T_OrderHeaderID
          let user = one_user()
          this.urlprintnote = "/birt/run?__report=report/one/fo/rpt_t_001i.rptdesign&__format=pdf&username="+user.M_StaffName+"&PID="+idx
          this.$store.commit("paymentnew/update_open_print_note",true)
      },
                printKkp(){
                    this.printwidth = 800
                    this.printtitle = ""
                    let idx = this.$store.state.patient.selected_patient.T_OrderHeaderID
                    let user = one_user()
                    this.urlprintnote = "/birt/run?__report=report/one/lab/rpt_fo_001.rptdesign&__format=pdf&username="+user.M_StaffName+"&PID="+idx
                    this.$store.commit("paymentnew/update_open_print_note",true)
                },
                printIc(){
                    this.printwidth = 800
                    this.printtitle = ""
                    let idx = this.$store.state.patient.selected_patient.T_OrderHeaderID
                    let user = one_user()
                    this.urlprintnote = "/birt/run?__report=report/one/fo/rpt_t_inform_concern.rptdesign&__format=pdf&username="+user.M_StaffName+"&PID="+idx
                    this.$store.commit("paymentnew/update_open_print_note",true)
                }
   }
}
</script>
