<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 xs12>
              <v-layout>
                <v-flex
                    xs3 pr-2
                  >
                    <!--<v-text-field
                      label="Tanggal Transaksi"
                      v-model="xdate"
                      outline
                      @change="updateDate(xdate)"
                    ></v-text-field>-->
                    <v-menu
                          v-model="menufilterdate"
                          :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
                              v-model="filterComputedDateFormatted"
                              label="Tanggal Transaksi"
                              outline
                              readonly
                              v-on="on"
                              @blur="date = deFormatedDate(filterComputedDateFormatted)"
                              ></v-text-field>
                          </template>
                          <v-date-picker v-model="xdate" no-title @input="menufilterdate = false"></v-date-picker>
                      </v-menu>
                  </v-flex>
                  <v-flex xs3 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 xs3 pl-2>
                    <v-select
                      item-text="name"
                      return-object
                      :items="types" 
                      v-model="xtype"
                      label="Tipe" outline   hide-details
                    ></v-select>
                  </v-flex>
                <v-flex xs3 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 xs4>
            <v-btn
                color="warning"
                depressed
                style="display:none"
              >
                Unduh Data
              </v-btn>
              <v-btn
                color="info"
                depressed
                style="display:none"
                @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:16%" class="rTableHead">STAFF</div>
                <div style="width:16%" class="rTableHead">DIKIRIM KE</div>
                <div style="width:12%" class="rTableHead">STATUS</div>
                <div style="width:10%" class="rTableHead">RETRY</div>
                <div class="rTableHead">AKSI</div>
              </div>
            </div>
            <div v-if="xorders.length === 0" class="rTable pt-2">
              <div class="rTableRow">
                <div style="width:100%;text-align:center" class="rTableCell">Belum ada data</div>
              </div>
            </div>
            <div v-if="xorders.length > 0" 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.typeCode}}</div>
                    <div style="width:16%" class="rTableCell">{{vo.M_PatientName}}</div>
                    <div style="width:16%" class="rTableCell">{{vo.M_StaffName}}</div>
                    <div style="width:16%" class="rTableCell">{{vo.outboxRecipient}}</div>
                    <div style="width:12%" class="rTableCell">
                      <v-btn v-if="vo.outboxIsSent === 'N'" class="error">Belum Terkirim</v-btn>
                     <v-btn v-if="vo.outboxIsSent === 'Y'" class="success">Terkirim</v-btn>
                    </div>
                    <div style="width:10%" class="rTableCell">
                      {{vo.outboxRetry}}
                    </div>
                    <div class="rTableCell">
                      <v-chip style="cursor:pointer;" v-if="vo.outboxRetry >= 3" @click="resend(vo)" label color="warning" text-color="white">
                        <v-icon dark left>email</v-icon> kirim ulang
                      </v-chip>
                      <v-chip style="cursor:no-drop" v-if="vo.outboxRetry < 3" label color="gray" text-color="white">
                        <v-icon dark left>email</v-icon> kirim ulang
                      </v-chip>
                    </div>
                  </div>
                </div>
                
              </div>
            </div>

          </v-flex>
        </v-layout>
      </v-card>
    </v-flex>
    <template>
  
        <v-dialog
        v-model="xdialogordernumber"
    
        max-width="30%"
        >
            <v-card>
                <v-card-title
                    class="headline success pt-2 pb-2"
                    primary-title
                >
                <h4 style="color:#FFEBEE" v-html="xtitleordernumber"></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="xmsgordernumber" 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="success"
                        flat
                        v-if="xbtnconfirm === 'show'"
                        @click="doConfirm()"
                    >
                    Ok, Confirm !
                    </v-btn>
                    <v-btn
                        color="error"
                        flat
                        @click="closeDialogOrderNumber(false)"
                    >
                    Tutup
                    </v-btn>
                </v-card-actions>
            </v-card>
        </v-dialog>
    
    </template>
  </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:'',
        menufilterdate:false,
        date: new Date().toISOString().substr(0, 10)
      }
    },
    mounted() {

      this.$store.dispatch("type/lookup")
      this.$store.dispatch("form/titsexreg")
      console.log(this.$store.state.type.selected_type)
      var prm = {fdate:this.xdate,fstatus:this.$store.state.status.selected_status.id,ftype:this.$store.state.type.selected_type.id}
      this.$store.dispatch("order/search",prm)

   },
    computed: {
      xdialogordernumber(){
            return this.$store.state.form.dialog_ordernumber
        },
      statuses(){
        return this.$store.state.status.statuses
      },
      xtitleordernumber(){
            return this.$store.state.form.title_ordernumber
        },
        xmsgordernumber(){
            return this.$store.state.form.msg_ordernumber
        },
        xbtnconfirm(){
            return this.$store.state.form.btn_confirm
        },
      xstatus : {
        get() {
          return this.$store.state.status.selected_status
        },
        set(val) {
          this.$store.commit("status/update_selected_status",val)
        }
      },
      types(){
        return this.$store.state.type.types
      },
      xtype : {
        get() {
          return this.$store.state.type.selected_type
        },
        set(val) {
          this.$store.commit("type/update_selected_type",val)
        }
      },
      xdate : {
        get() {
          return this.$store.state.order.fdate
        },
        set(val) {
          this.$store.commit("order/update_fdate",val)
        }
      },
      filterComputedDateFormatted () {
        return this.formatDate(this.xdate)
      },
      xorders(){
        return this.$store.state.order.orders
      }
    },
    methods: {
      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')}`
      },
      filterOrder(){
        var prm = {fdate:this.xdate,fstatus:this.$store.state.status.selected_status.id,ftype:this.$store.state.type.selected_type.id}
        this.$store.dispatch("order/search",prm)
        //this.$store.dispatch("order/lookup_staff",prm)
      },
      doubletorp(val){
        return one_money(val)
      },
      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>'
      },
      resend(val){
        //this.xdialogwarningdelete = false
        this.$store.commit("form/update_orderid",val.outboxID)
        var params = {
          orderid:this.$store.state.form.orderid,
          fdate:this.$store.state.order.fdate,
          fstatus:this.$store.state.status.selected_status.id,
          ftype:this.$store.state.type.selected_type.id
        }
        this.$store.dispatch("order/resend",params)
      },
      closeDialogOrderNumber(val){
            this.$store.commit("form/update_dialog_ordernumber",val)
            var prm = {fdate:this.$store.state.order.fdate,fstatus:this.$store.state.status.selected_status.id,ftype:this.$store.state.type.selected_type.id}
            this.$store.dispatch("order/search",prm)
            //this.$store.dispatch("order/lookup_staff",prm)
        }
    }
  }
</script>