<template>
  <div>
  <v-layout>
    <v-flex xs12>
      <v-card style="height:100px" class="mb-2" color="white">
        <v-card-text>
          <v-layout>
            <v-flex  pa-2 xs6>
              <v-layout>
                <v-flex
                    xs4 pr-2
                  >
                    <v-text-field
                      label="Tanggal Transaksi"
                      v-model="xdate"
                      outline
                      @change="updateDate(xdate)"
                    ></v-text-field>
                  </v-flex>
                  <v-flex xs4 pl-2>
                    <v-select
                      item-text="name"
                      return-object
                      :items="statuses" 
                      v-model="xstatus"
                      label="Status" outline   hide-details
                    ></v-select>
                </v-flex>
                <v-flex xs4 pl-4>
                    <v-btn
                color="secondary"
                outline
                large
                @click="filterOrder()"
              >
                Filter
              </v-btn>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex class="text-xs-right" pt-2 xs6>
              <v-btn
                color="primary"
                depressed
              >
                PEMERIKSAAAN
              </v-btn>
              <v-btn
                color="primary"
                depressed
              >
                PENGIRIMAN
              </v-btn>
              <v-btn
                color="primary"
                depressed
              >
                Staf
              </v-btn>
              <v-btn
                color="info"
                depressed
                @click="openForm(true)"
              >
                Baru
              </v-btn>
            </v-flex>
          </v-layout>
        </v-card-text>
      </v-card>
    </v-flex>
  </v-layout>

  <v-layout>
    <v-flex xs12>
      <v-card class="mb-2" color="white">
        <v-layout row wrap>
          <v-flex class="border-bottom-dashed" xs12 pt-2 pl-4 pr-4 pb-4>
            <div class="rTable">
              <div class="rTableHeading">
                <div style="width:10%;" class="rTableHead">JAM</div>
                <div style="width:10%" class="rTableHead">TIPE</div>
                <div style="width:16%" class="rTableHead">PASIEN</div>
                <div style="width:22%" class="rTableHead">ALAMAT</div>
                <div style="width:12%" class="rTableHead">TELEPON</div>
                <div style="width:10%" class="rTableHead">STATUS</div>
                <div class="rTableHead">AKSI</div>
              </div>
            </div>

            <div v-for="(vo,idx) in xorders" :key="vo.PreOrder_HeaderID">
              <div class="rTable">
                <div class="rTableGroup">
                  <div class="rTableRow">
                    <div style="width:10%" class="rTableCell rfirstCell">{{vo.PreOrder_HeaderTime}}</div>
                    <div style="width:10%" class="rTableCell">{{vo.M_PreOrderTypeName}}</div>
                    <div style="width:16%" class="rTableCell">{{vo.M_PatientName}}</div>
                    <div style="width:22%" class="rTableCell">{{vo.M_PatientAddressDescription}}</div>
                    <div style="width:12%" class="rTableCell">{{vo.PreOrder_HeaderPhone}}</div>
                    <div style="width:10%" class="rTableCell">
                      <v-btn v-bind:class="vo.M_PreOrderStatusColor">{{vo.M_PreOrderStatusName}}</v-btn>
                    </div>
                    <div class="rTableCell">
                      <v-chip style="cursor:pointer" v-if="vo.PreOrder_HeaderM_PreOrderStatusID === '1'" @click="actCancel(vo)" label color="error" text-color="white">
                        <v-icon left>cancel</v-icon> batal
                      </v-chip>
                      <v-chip style="cursor:pointer" v-if="vo.PreOrder_HeaderM_PreOrderStatusID === '1' || vo.PreOrder_HeaderM_PreOrderStatusID === '2'" @click="openFormEdit(vo)" label color="warning" text-color="white">
                        <v-icon left>edit</v-icon> edit
                      </v-chip>
                   
                      <v-icon style="cursor:pointer;padding-top:12px;float:right" class="text-sm-right" @click="showDetail(idx)" color="blue darken-2">more_vert</v-icon>
                    </div>
                  </div>
                </div>
                <transition name="fade">
                  <div v-if="vo.show_detail === 'Y'" style="width:100%;border:1px solid black" class="caption">
                    <v-layout v-if="vo.deliveries.length > 0" row mb-2 wrap>
                      <v-flex v-for="(vd,didx) in vo.deliveries" :key="vd.PreOrder_DeliveryID" xs4>
                        <v-layout row wrap>
                          <v-flex xs4 class="label-delivery font-weight-bold">
                            {{vd.M_DeliveryName}}
                          </v-flex>
                          <v-flex xs8 class="text-delivery text-xs-right">
                            {{vd.PreOrder_DeliveryDestination}}
                          </v-flex>
                        </v-layout>
                      </v-flex>
                    </v-layout>
                    <v-layout row>
                        <table>
                            <tr>
                            <th class="text-md-center pt-2 pb-2"> NO. </th>
                            <th class="text-md-center pt-2 pb-2">PEMERIKSAAN</th>
                            <th class="text-md-center pt-2 pb-2">BRUTO</th>
                            <th class="text-md-center pt-2 pb-2">DISKON</th>
                            <th class="text-md-center pt-2 pb-2">TOTAL</th>

                            </tr>
                            <tr v-for="(vt,tidx) in vo.tests" :key="vt.PreOrder_TestID">
                              <td class="text-md-center"> {{tidx+1}} </td>
                              <td class="text-md-left pl-3">{{vt.PreOrder_TestT_TestName}}</td>
                              <td class="text-md-right pr-2">{{doubletorp(vt.PreOrder_TestPrice)}}</td>
                              <td class="text-md-right pr-2">{{vt.PreOrder_TestDisc}}</td>
                              <td class="text-md-right pr-2">{{doubletorp(vt.PreOrder_TestTotal)}}</td>

                            </tr>
                            
                            <!--<tfoot>
                            <tr>
                                <th style="background:#03a9f43d" colspan="3" class="text-md-right pr-2 pt-1 pb-1">SUB TOTAL</th>
                                <th style="background:#03a9f43d" class="text-md-right pr-2 pt-2 pb-2" colspan="2">300.000</th>

                            </tr>
                            <tr>
                                <th colspan="3"  class="text-md-right pr-2 pt-1 pb-1">DISKON PEMBULATAN</th>
                                <th class="text-md-right pr-2 pt-1 pb-1"  colspan="2">0</th>

                            </tr>
                            <tr>
                                <th style="background:#03a9f43d" colspan="3"  class="text-md-right pr-2 pt-2 pb-2">TOTAL</th>
                                <th style="background:#03a9f43d" class="text-md-right pr-2 pt-1 pb-1"  colspan="2">300.000</th>

                            </tr>
                            </tfoot>-->
                        </table>
                    </v-layout>
                  </div>
                </transition>
              </div>
            </div>

          </v-flex>
        </v-layout>
      </v-card>
    </v-flex>
  </v-layout>

  <template>
  
        <v-dialog
        v-model="xdialogwarningdelete"
    
        max-width="30%"
        >
            <v-card>
                <v-card-title
                    class="headline red darken-1 pt-2 pb-2"
                    primary-title
                >
                <h4 style="color:#FFEBEE">Peringatan !</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="xmsgwarningdelete" 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
                        color="error"
                        flat
                        @click="doCancel()"
                    >
                    OK, Batalkan !
                    </v-btn>

                    <v-btn
                        color="primary"
                        flat
                        @click="xdialogwarningdelete = false"
                    >
                    Tutup
                    </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
    
    </template>
  </div>
</template>

<style scoped>
  .fade-enter-active, .fade-leave-active {
    transition: opacity .5s;
  }
  .fade-enter, .fade-leave-to {
    opacity: 0;
  }
  .bounce-enter-active {
    animation: bounce-in .5s;
  }
  .bounce-leave-active {
    animation: bounce-in .5s reverse;
  }
  @keyframes bounce-in {
    0% {
      transform: scale(0);
    }
    25% {
      transform: scale(.50);
    }
    50% {
      transform: scale(.75);
    }
    75% {
      transform: scale(1.1);
    }
    100% {
      transform: scale(1);
    }
  }
  .rTable {
    display: table;
    width: 100%;
    border-spacing: 0 .2em;
  }

  .rTableGroup {
    display: table-row-group;
  }

  .rTableRow {
    display: table-row;
    font-size: 14px;

  }

  .rTableHeading {
    display: table-header-group;
    background-color: #fffff;
  }

  .rTableCell {
    display: table-cell;
    padding-left: 10px;
    padding-right: 10px;
    padding-top: 5px;
    padding-bottom: 5px;
    line-height: 2;
    border: 1px solid #2196f3;
    background-color: #fffff;
    border-spacing: 5px;
  }

  .rTableHead {
    display: table-cell;
    padding-left: 10px;
    padding-right: 10px;
    padding-top: 10px;
    padding-bottom: 0px;
    background-color: #fffff;
  }

  .rTableHeading {
    display: table-header-group;
    text-transform: uppercase;
    font-weight: 500;
  }

  .rTableFoot {
    display: table-footer-group;
    font-weight: bold;
    background-color: #ddd;
  }

  .rTableBody {
    display: table-row-group;
  }

  .rfirstCell {
    border-left: 10px solid #2196f3;
    border-top-left-radius: 3px;
    border-bottom-left-radius: 3px;
  }

  .caption {
    border: 1px solid grey;
    caption-side: bottom;
    display: table-caption;
    text-align: center;
    padding: 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;
  }

  .text-fajrihm {
    padding: 3px 20px 3px 0;
    text-align: left;
    font-size: 13px;
    font-family: open sans, tahoma, sans-serif;
  }

  .label-delivery {
    font-size: 11px;
    padding: 3px 10px 3px 0;
    font-family: open sans, tahoma, sans-serif;
  }

  .text-delivery {
    font-size: 12px;
    padding: 3px 10px 3px 0;
    font-family: open sans, tahoma, sans-serif;
  }

  .nota {
    font-size: 2em;
    font-weight: bold;
    text-align: left;
  }

  .total {
    min-height: 76px;
  }

  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);
  }
  .v-breadcrumbs__item{
    color:#fff!important;
}
</style>

<script>
  module.exports = {
    data() {
      return {
        xxx:0,
        xdialogwarningdelete:false,
        xmsgwarningdelete:''
      }
    },
    mounted() {
      this.$store.dispatch("status/lookup")
      this.$store.dispatch("form/titsexreg")
   },
    computed: {
      statuses(){
        return this.$store.state.status.statuses
      },
      xstatus : {
        get() {
          return this.$store.state.status.selected_status
        },
        set(val) {
          this.$store.commit("status/update_selected_status",val)
        }
      },
      xdate : {
        get() {
          return this.$store.state.order.fdate
        },
        set(val) {
          this.$store.commit("order/update_fdate",val)
        }
      },
      xorders(){
        return this.$store.state.order.orders
      }
    },
    methods: {
      filterOrder(){
        var prm = {fdate:this.xdate,fstatus:this.$store.state.status.selected_status.id}
        this.$store.dispatch("order/search",prm)
        this.$store.dispatch("order/lookup_staff",prm)
      },
      doubletorp(val){
        return one_money(val)
      },
      showDetail(idx){
        var arrOrders = this.$store.state.order.orders
        arrOrders[idx].show_detail = arrOrders[idx].show_detail === 'Y' ? 'N':'Y'
        this.$store.commit("order/update_orders_commit",arrOrders)
      },
      updateDate(val){
         this.$store.commit("order/update_fdate",val)
      },
      openForm(val){
         this.$store.commit("form/update_step",'order')
         this.$store.commit("form/update_disabled_register",false)
         this.$store.commit("form/update_dialog_form",val)
      },
      openFormEdit(val){
        this.$store.commit("form/update_step",'order')
        this.$store.commit("form/update_disabled_register",true)
        this.$store.commit("form/update_form_act",'edit')
        this.$store.commit("form/update_btn_confirm",'hide')
        this.$store.commit("form/update_deleted_ordertests",[])
        this.$store.commit("form/update_orderid",val.PreOrder_HeaderID)
        this.$store.commit("form/update_status_now",{id:val.M_PreOrderStatusID,name:val.M_PreOrderStatusName})
        
        var types = this.$store.state.form.ordertypes
        var idx_type = _.findIndex(types, function(o) { return o.M_PreOrderTypeID == val.PreOrder_HeaderM_PreOrderTypeID })
        var type = types[idx_type]
        this.$store.commit("form/update_ordertype",type)
        this.$store.commit("form/update_orderdate",val.PreOrder_HeaderDate+" "+val.PreOrder_HeaderTime)
        this.$store.commit("form/update_orderphone",val.PreOrder_HeaderPhone)
        this.$store.commit("form/update_orderpatients",[{M_PatientID:val.M_PatientID,M_PatientName:val.M_PatientName,M_PatientNoReg:val.M_PatientNoReg}])
        this.$store.commit("form/update_orderpatient",{M_PatientID:val.M_PatientID,M_PatientName:val.M_PatientName,M_PatientNoReg:val.M_PatientNoReg})
        var addresses = val.address
        var idx_addrs = _.findIndex(addresses, function(o) { return o.M_PatientAddressID == val.M_PatientAddressID })
        var address = addresses[idx_addrs]
        this.$store.commit("form/update_orderadresss",addresses)
        this.$store.commit("form/update_orderadress",address)
        this.$store.commit("form/update_ordertests",val.tests)
        var staffs = this.$store.state.form.staffs
        var idx_staff = _.findIndex(staffs, function(o) { return o.M_StaffID == val.M_StaffID })
        var staff = staffs[idx_staff]
        this.$store.commit("form/update_orderstaff",staff)
        var deliverytipes = this.$store.state.form.orderdeliverytipes
        var idx_deliverytype = _.findIndex(deliverytipes, function(o) { return parseInt(o.M_DeliveryM_DeliveryTypeID) == parseInt(val.deliveries[0].PreOrder_DeliveryM_DeliveryTypeID) })
        this.$store.commit("form/update_orderdeliverytype",deliverytipes[idx_deliverytype])
        if(parseInt(val.deliveries[0].M_DeliveryID) === 2){
          var delivery_idx_addrs = _.findIndex(addresses, { M_PatientAddressM_KelurahanID: val.deliveries[0].PreOrder_DeliveryM_KelurahanID, M_PatientAddressDescription: val.deliveries[0].PreOrder_DeliveryM_DeliveryTypeID })
          var patient_delivery_address = addresses[idx_addrs]
          this.$store.commit("form/update_orderpatientdeliveryaddress",patient_delivery_address)
          this.$store.commit("form/update_orderdeliveryaddress",val.deliveries[0].PreOrder_DeliveryDestination)
        }else{
          this.$store.commit("form/update_orderdeliveryaddress",val.deliveries[0].PreOrder_DeliveryDestination)
          if(parseInt(val.deliveries[0].M_DeliveryID) === 1){
            this.$store.commit("form/update_orderdeliveryaddress","")
          }
        }
        

        this.$store.commit("form/update_dialog_form",true)
      },
      actCancel(val){
        this.$store.commit("form/update_orderid",val.PreOrder_HeaderID)
        this.xdialogwarningdelete = true
        this.xmsgwarningdelete = '<p>Yakin, mau membatalkan order '+val.PreOrder_HeaderNumbering+' ?</p>'
      },
      doCancel(){
        this.xdialogwarningdelete = false
        var params = {
          orderid:this.$store.state.form.orderid,
          fdate:this.$store.state.order.fdate,
          fstatus:this.$store.state.status.selected_status.id
        }
        this.$store.dispatch("order/docancel",params)
      }
    }
  }
</script>