<template>
<div class="container-fluid">
  <section class="content">
    <div class="header pb-2">
      <div class="header-body">
        <div class="row align-items-center py-4">
          <div class="col-lg-6 col-7">
            <nav
              aria-label="breadcrumb"
              class="d-none d-md-inline-block ml-md-4"
            >
              <ol class="breadcrumb breadcrumb-links">
                <li class="breadcrumb-item">
                  <a href="#"><i class="fas fa-home"></i></a>
                </li>
                <li class="breadcrumb-item">
                  <a href="#">Dashboards</a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">
                  ຍີ່ຫໍ້ ແລະຫົວໜ່ວຍ ສິນຄ້າ
                </li>
              </ol>
            </nav>
          </div>
          <div class="col-lg-6 col-5 text-right"></div>
        </div>
      </div>
    </div>
    <div class="row" v-if="isLoggedIn">


      <div class="col-md-6 col-lg-6">
        <div class="card">
            <div class="card-header border-0">
            <div class="row d-flex justify-content-between px-2">
              <div >
                <h3 class="mb-0">ຍີ່ຫໍ້ Brand</h3>
              </div>
              <div v-if="$store.getters['getroles'].includes('setting_brand_add')">
                  <a href="javascript:void(0)" v-if="ShFB" @click="SaveB" :class="CheckFormB" class="btn btn-sm btn-success btn-round btn-icon"><i class="fas fa-save"></i> ບັນທຶກ </a>
                <a href="javascript:void(0)" v-if="ShFB" @click="CancleFormB" class="btn btn-sm btn-danger btn-round btn-icon"><i class="fas fa-trash"></i > ຍົກເລີກ </a>
                <a href="javascript:void(0)" v-if="!ShFB" @click="AddFornB" class="btn btn-sm btn-info btn-round btn-icon"><i class="fas fa-plus"></i > ເພີ່ມໃໝ່ </a>
              </div>
            </div>
          </div>
          <div class=" card-body form-b" v-if="ShFB">

                <div class="row justify-content-center pt-6 pb-6">
                      <div class="col-lg-3 order-lg-2">
                        <div class="card-profile-image">
                          <a href="javascript:void(0)">
                            <img v-bind:src="imagePreviewB"
                              class="rounded-circle"
                              onclick="$('#simg').click()"
                              style="width: 140px; height: 140px;"
                            />
                          </a>
                          <input type="file" id="simg" name="file" style="display:none;"   @change="onSelectedB" accept="image/*">
                        </div>
                      </div>
                    </div>

                     <div class="text-center">
                      <div class="progress-wrapper" v-if="imageB.shupload">
                <div class="progress-info">
                  <div class="progress-label">
                    <span>ກຳລັງອັບໂຫຼດ</span>
                  </div>
                  <div class="progress-percentage">
                    <span>{{imageB.percenUp}}%</span>
                  </div>
                </div>
                <div class="progress">
                  <div class="progress-bar bg-primary" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" v-bind:style="{ width: imageB.percenUp + '%' }"></div>
                </div>
              </div>
                      <span class="text-red" v-if="imageB.upmax">ໄຟລ໌ທີ່ທ່ານອັບໂຫຼດມີຂະໜາດເກີນ 1 Mb</span>
                    </div>

                    <label class="form-control-label">ຊື່ຍີ່ຫໍ້(Brand): <span class="text-red">* </span></label>
                  <div class="input-group input-group-merge ">
                      <div class="input-group-prepend">
                          <span class="input-group-text"><i class="fas fa-file-signature"></i></span></div>
                          <input type="text" class="form-control" v-model="FormB.name" ref="b_name_focus" >
                    </div>

          </div>
          <div class="table-responsive" v-if="!ShFB">
              <table class="table align-items-center table-flush">
              <thead class="thead-light">
                <tr>
                 <!-- <th width="30">ID</th> -->
                  <th width="150" class="text-left text-center" >ຮູບ</th>
                  <th class="text-left text-center">ຊື່ຍີ່ຫໍ້</th>
                  <th width="80" class="text-center" v-if="$store.getters['getroles'].includes('setting_brand_edit') || $store.getters['getroles'].includes('setting_brand_remove')">ຈັດການ</th>
                </tr>
              </thead>
              <tbody>
                <tr v-if="LoadingB">
                    <td colspan="3" class="text-center">
                      <content-loader viewBox="-10 10 450 110" >
                            <rect x="0" y="0" rx="3" ry="3" width="350" height="5" />
                            <rect x="10" y="20" rx="3" ry="3" width="320" height="5" />
                            <rect x="0" y="40" rx="3" ry="3" width="370" height="5" />
                            <rect x="10" y="60" rx="3" ry="3" width="350" height="5" />
                            <rect x="0" y="80" rx="3" ry="3" width="300" height="5" />
                            <rect x="10" y="100" rx="3" ry="3" width="380" height="5" />
                      </content-loader>
                    </td>
                  </tr>
                  <tr v-if="Chbrand">
                    <td colspan="3" class="text-center">
                      ບໍ່ມີີຂໍ້ມູນ
                    </td>
                  </tr>
                    <tr v-for="brn in Brand.data" :key="brn.id">
                      <td class="text-center">
                        <img style="width: 150px; height: auto;" v-bind:src="'../images/avatars/'+brn.brand_image"
                                        class="avatar mr-3"
                                     v-if="brn.brand_image"/>
                                     <img style="width: 150px; height: auto;" v-bind:src="'../images/add_img_bt.png'"
                                        class="avatar mr-3"
                                     v-if="!brn.brand_image"/>
                      </td>
                      <td class="text-center"> <b> {{brn.brand_name}} </b></td>
                      <td v-if="$store.getters['getroles'].includes('setting_brand_edit') || $store.getters['getroles'].includes('setting_brand_remove')"><div class="col-auto">
                    <button type="button" class="btn btn-sm btn-neutral mr-0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" >
                      <i class="fas fa-ellipsis-h"></i>
                    </button>
                    <div class="dropdown-menu dropdown-menu-right" style="">
                      <a v-if="$store.getters['getroles'].includes('setting_brand_edit')" class="dropdown-item" href="javascript:void(0)" @click="EditBrand(brn.id)" ><i class="fas fa-user-edit text-info"></i >ແກ້ໄຂຂໍ້ມູນ</a >
                      <a v-if="$store.getters['getroles'].includes('setting_brand_remove')" class="dropdown-item" href="javascript:void(0)" @click="DelBrand(brn.id)"  ><i class="fas fa-trash text-red"></i>ລຶບຂໍ້ມູນ</a >
                    </div></div></td>
                  </tr>
              </tbody>
            </table>
            <vuePagination
              :pagination="Brand"
              @paginate="GetAllBrand()"
              :offset="4"
            ></vuePagination>
              </div>
        </div>
      </div>

      <div class="col-md-6 col-lg-6">
        <div class="card">
            <div class="card-header border-0">
            <div class="row d-flex justify-content-between px-2">
              <div >
                <h3 class="mb-0">ຫົວໜ່ວຍ Unit</h3>
              </div>
              <div  v-if="$store.getters['getroles'].includes('setting_unit_add')">
                  <a href="javascript:void(0)" v-if="ShFU" @click="SaveU" :class="CheckFormU"  class="btn btn-sm btn-success btn-round btn-icon"><i class="fas fa-save"></i> ບັນທຶກ </a>
                <a href="javascript:void(0)" v-if="ShFU" @click="CancleFormU" class="btn btn-sm btn-danger btn-round btn-icon"><i class="fas fa-trash"></i > ຍົກເລີກ </a>
                <a href="javascript:void(0)" v-if="!ShFU" @click="AddFormU" class="btn btn-sm btn-info btn-round btn-icon"><i class="fas fa-plus"></i > ເພີ່ມໃໝ່ </a>
              </div>
            </div>
          </div>
          <div class="card-body" v-if="ShFU">
              <label class="form-control-label">ຊື່ຫົວໜ່ວຍ: <span class="text-red">* </span></label>
                  <div class="input-group input-group-merge ">
                      <div class="input-group-prepend">
                          <span class="input-group-text"><i class="fas fa-file-signature"></i></span></div>
                          <input type="text" class="form-control" v-model="FormU.name" ref="u_name_focus" >
                    </div>
                <div class="row mt-2">
                    <div class="col-md-6">
                      <label class="form-control-label">ຫົວໜ່ວຍ: <span class="text-red">* </span></label>
                  <div class="input-group input-group-merge ">
                      <div class="input-group-prepend">
                          <span class="input-group-text"><i class="fas fa-file-signature"></i></span></div>
                          <input type="number" class="form-control" v-model="FormU.one" >
                    </div>
                    </div>
                    <div class="col-md-6">
                      <label class="form-control-label">ຍ່ອຍຕໍ່ຫົວໜ່ວຍ: <span class="text-red">* </span></label>
                  <div class="input-group input-group-merge ">
                      <div class="input-group-prepend">
                          <span class="input-group-text"><i class="fas fa-file-signature"></i></span></div>
                          <input type="number" class="form-control" v-model="FormU.to" >
                    </div>
                    </div>
                    <div class="col-md-12 mt-4">
                      <label class="form-control-label">ໝາຍເຫດ:</label> <small> ຫົວໜ່ວຍສາມາດ ກຳນົດໄດ້ບໍ່ຈຳກັດເຊັ່ນ: 1:1 ໝາຍຄວາມວ່າ 1 ໜ່ວຍ/1ແພັກ, 1:5 ໝາຍຄວາມວ່າ 1 ໜ່ວຍມີ 5 ຢ່າງ ຫລື 1 ແພັກມີ 5 ຢ່າງ </small>
                    </div>
                    
                </div>
                
          </div>
          <div class="table-responsive" v-if="!ShFU">
                  <table class="table align-items-center table-flush">
              <thead class="thead-light">
                <tr>
                 <!-- <th width="30">ID</th> -->
                  <th class="text-left " >ຊື່ຫົວໜ່ວຍ</th>
                  <th class="text-center ">ອັດຕາ</th>
                  <th width="80" class="text-center"  v-if="$store.getters['getroles'].includes('setting_unit_edit') || $store.getters['getroles'].includes('setting_unit_remove')">ຈັດການ</th>
                </tr>
              </thead>
              <tbody>
                <tr v-if="LoadingU">
                    <td colspan="3" class="text-center">
                      <content-loader viewBox="-10 10 450 110" >
                            <rect x="0" y="0" rx="3" ry="3" width="350" height="5" />
                            <rect x="10" y="20" rx="3" ry="3" width="320" height="5" />
                            <rect x="0" y="40" rx="3" ry="3" width="370" height="5" />
                            <rect x="10" y="60" rx="3" ry="3" width="350" height="5" />
                            <rect x="0" y="80" rx="3" ry="3" width="300" height="5" />
                            <rect x="10" y="100" rx="3" ry="3" width="380" height="5" />
                      </content-loader>
                    </td>
                  </tr>
                  <tr v-if="ChUnit">
                    <td colspan="3" class="text-center">
                      ບໍ່ມີີຂໍ້ມູນ
                    </td>
                  </tr>
                    <tr v-for="u in Unit.data" :key="u.id" >
                      <td class="text-left"><b>{{u.unit_name}}</b></td>
                      <td class="text-center">{{u.unit_one}} : {{u.unit_to}}</td>
                      <td v-if="$store.getters['getroles'].includes('setting_unit_edit') || $store.getters['getroles'].includes('setting_unit_remove')"><div class="col-auto">
                    <button type="button" class="btn btn-sm btn-neutral mr-0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" >
                      <i class="fas fa-ellipsis-h"></i>
                    </button>
                    <div class="dropdown-menu dropdown-menu-right" style="">
                      <a class="dropdown-item" v-if="$store.getters['getroles'].includes('setting_unit_edit')" href="javascript:void(0)" @click="EditUnit(u.id)" ><i class="fas fa-user-edit text-info"></i >ແກ້ໄຂຂໍ້ມູນ</a >
                      <a class="dropdown-item" v-if="$store.getters['getroles'].includes('setting_unit_remove')" href="javascript:void(0)" @click="DelUnit(u.id)" ><i class="fas fa-trash text-red"></i>ລຶບຂໍ້ມູນ</a >
                    </div></div></td>
                  </tr>
              </tbody>
            </table>
            <vuePagination
              :pagination="Unit"
              @paginate="GetAllUnit()"
              :offset="4"
            ></vuePagination>
              </div>
        </div>
      </div>
    </div>
  </section>
</div>
</template>

<script>

export default {
  name: "StoreSetting",
  components: {},
  directives: {},
  watch:{},
  data() {
    return {
      Brand:[],
      FormB:{
        name:'',
        image:''
      },
      FormBType:true,
      FormBID:'',
      ShFB:false,
      imageB:{
        file:null,
        max_size: 1000*1204,
        upmax:false,
        percenUp:0,
        shupload:false
      },
      LoadingB:false,
      imagePreviewB:window.location.origin+'/images/add_img_bt.png',
      daBT:true,
      polling: null,
      /// unit
      Unit:[],
      FormU:{
        name:'',
        one:1,
        to:1
      },
      FormUType:true,
      FormUID:'',
      ShFU:false,
      LoadingU:false,
      daBTU:true,
      isLoggedIn: false,
    };
  },

  mounted() {},
   computed:{
      CheckFormB(){
          if(this.FormB.name == '')
          {
            return 'disabled'
          } else {
            if(this.daBT){ return '' } else { return 'disabled' }
             }
      },
      Chbrand(){
        if(this.Brand == ''){ return true } else {
            if(this.Brand.data.length==0){ return true } else { return false }
        }
      },
       CheckFormU(){
          if(this.FormU.name == '' || this.FormU.one == '' || this.FormU.to == '')
          {
            return 'disabled'
          } else {
            if(this.daBTU){ return '' } else { return 'disabled' }
             }
      },
      ChUnit(){
        if(this.Unit == ''){ return true } else {
            if(this.Unit.data.length==0){ return true } else { return false }
        }
      }
  },
  methods: {
     SaveB(){
       this.daBT = false
       if(this.FormBType){
         if(this.imageB.file){
              this.UploadImg()
          } else {
            this.PostAddB()
          }
       } else {
          if(this.imageB.file){
              this.UploadImg()
          } else {
            this.PostUpdateB()
          }
       }
    },
    onSelectedB(event){
      this.imageB.file = event.target.files[0]
        let reader  = new FileReader();
              reader.addEventListener("load", function () {
                  this.imagePreviewB = reader.result;
                }.bind(this), false);
                if( this.imageB.file ){
                  if ( /\.(jpe?g|png|gif)$/i.test( this.imageB.file.name ) ) {
                    if(this.imageB.file.size>this.imageB.max_size){
                      this.imageB.upmax = true
                    } else {
                      this.imageB.upmax = false
                      reader.readAsDataURL( this.imageB.file );
                    }
                  }
                }
    },
 async UploadImg(){
      this.imageB.shupload = true
      let formData = new FormData();
      formData.append("file", this.imageB.file);
     await this.$axios.get("/sanctum/csrf-cookie").then((response) => {
       axios.post("/api/img/upload", formData, {
            headers: {"Content-Type": "multipart/form-data"},
            onUploadProgress: progressEvent => {
              this.imageB.percenUp = Math.round(progressEvent.loaded / progressEvent.total * 100)
              if(this.imageB.percenUp==100){ this.imageB.shupload = false; this.imageB.percenUp=0 }
            }
          }).then((response)=> {
             this.imagePreviewB = window.location.origin+'/images/add_img_bt.png'
             this.FormB.image = response.data.images;
            this.ShFB = false
             if(this.FormBType){
             this.PostAddB()
             } else {
               this.PostUpdateB()
             }
            }).catch((error)=> {
              this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+error.message }, 4000)
            });
         });
    },
    PostAddB(){
      this.LoadingB = true
      this.$Progress.start();
         this.$axios.get("/sanctum/csrf-cookie").then((response) => {
            axios.post("/api/store/brand/add", {
                brand_name: this.FormB.name,
                brand_image: this.FormB.image
              })
              .then((response) => {
                this.daBT = true
                if (response.data.success) {
                  this.LoadingB = false
                  this.ShFB = false
                  this.FormB.name = ''
                  this.FormB.image = ''
                  this.imageB.file = null
                  this.Brand = response.data.que
                  this.$Progress.finish();
                  this.$notify({ group: "sucess", title: "ສຳເລັດ!", text: " ການບັນທຶກຂໍ້ມູນ ສຳເລັດ! " }, 2000)
                } else {
                  this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+response.data.message }, 4000)
               
                  this.$Progress.fail();
                }
              })
              .catch((error)=> {
                this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+error.message }, 4000)
                this.$Progress.fail();
              });
            });
    },
    PostUpdateB(){
      this.LoadingB = true
      this.$Progress.start();
      this.$axios.get("/sanctum/csrf-cookie").then((response) => {
            axios.post(`/api/store/brand/update/${this.FormBID}`, {
                brand_name: this.FormB.name,
                brand_image: this.FormB.image
              })
              .then((response) => {
                if (response.data.success) {
                  this.LoadingB = false
                  this.daBT = true
                  this.FormBType = true
                  this.ShFB = false
                  this.FormBID = ''
                  this.FormB.name = ''
                  this.FormB.image = ''
                  this.imageB.file = null
                  this.Brand = response.data.que
                  this.$notify({ group: "sucess", title: "ສຳເລັດ!", text: " ການບັນທຶກຂໍ້ມູນ ສຳເລັດ! " }, 2000)
                  this.$Progress.finish();
                } else {
                  this.error = response.data.message;
                  console.error(error);
                  this.$Progress.fail();
                }
              })
              .catch( (error)=> {
                this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+error.message }, 4000)
      
              });
            });
    },

    GetAllBrand(bpage){
      this.LoadingB = true
            this.$Progress.start();
           if (typeof bpage == "undefined") {
            this.$axios.get("/sanctum/csrf-cookie").then((response) => {
              axios
                .get(`/api/store/brand?page=${this.Brand.current_page}`)
                .then((response) => {
                   this.LoadingB = false
                  if(response.data.message){
                    this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+response.data.message }, 4000)
                    this.$Progress.fail();
                  } else {
                      //console.log(response.data.all_que)
                      if (response.data.data.length < 1) {
                        let bp = this.Brand.current_page - 1;
                        if (bp >= 1) {
                          this.GetAllBrand(bp);
                        } else {
                          this.Brand = response.data
                        }
                      } else {

                        this.Brand = response.data
                        this.$Progress.finish();
                      }
                    }
                })
                .catch((error) => {
                  this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+error.message }, 4000)
                  this.LoadingB = false
                  this.Brand = ''
                  console.error(error);
                  this.$Progress.fail();

                });
            });
          } else {
            this.$axios.get("/sanctum/csrf-cookie").then((response) => {
              axios
                .get(`/api/store/brand?page=${bpage}`)
                .then((response) => {
                  this.LoadingB = false
                  this.Brand = response.data.que;
                  this.$Progress.finish();
                })
                .catch((error) => {
                  this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+error.message }, 4000)
                  this.LoadingB = false
                  this.$Progress.fail();
                });
            });
          }
    },
    AddFornB(){
      this.FormB.name = ''
      this.imagePreviewB = window.location.origin+'/images/add_img_bt.png'
      this.ShFB = true
      this.polling = setInterval(() => {
         this.$refs.b_name_focus.focus();
          clearInterval(this.polling);
        }, 900);
    },
    CancleFormB(){
      this.ShFB = false
      this.imageB.file = null
    },
    EditBrand(id){
      this.FormBID = id
      this.ShFB = true
      this.FormBType = false
      let item = this.Brand.data.find(i => i.id == id)
      this.FormB.name = item.brand_name
      if(item.brand_image=='' || item.brand_image == null){
        this.imagePreviewB = window.location.origin+'/images/add_img_bt.png'
      } else {
        this.imagePreviewB = window.location.origin+'/images/avatars/'+item.brand_image
      }
      
      this.polling = setInterval(() => {
         this.$refs.b_name_focus.focus();
          clearInterval(this.polling);
        }, 900);
    },
    DelBrand(id){
       this.$swal.fire({
            title: 'ທ່ານແນ່ໃຈບໍ່?',
            text: "ທີ່ຈະລຶບຂໍ້ມູນນີ້!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'ລຶບເລີຍ!',
            cancelButtonText: 'ຍົກເລີກ'
            }).then((result) => {
            if (result.isConfirmed) {
                this.$Progress.start()
                this.$axios.get('/sanctum/csrf-cookie').then(response => {
                    this.$axios.delete(`/api/store/brand/delete/${id}`)
                        .then(response => {
                             this.GetAllBrand()

                            if (response.data.success) {
                              this.$Progress.finish()
                            this.$notify({ group: "sucess", title: "ສຳເລັດ!", text: response.data.message }, 2000)
                            } else {
                               this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+response.data.message }, 4000)
                              this.$Progress.fail()
                            }
                        })
                        .catch( (error)=> {
                          this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+error.message }, 4000)
                            //console.error(error)
                            this.$Progress.fail()
                        });
                })
            }
            })
    },
    //------ Form U
    AddFormU(){
      this.ShFU = true
      this.FormU.name = ''
      this.FormU.one = 1
      this.FormU.to = 1
      this.polling = setInterval(() => {
         this.$refs.u_name_focus.focus();
          clearInterval(this.polling);
        }, 900);
    },
    CancleFormU(){
      this.ShFU = false
    },
    SaveU(){
      this.daBTU = false
      if(this.FormUType){
          this.$Progress.start();
          this.$axios.get("/sanctum/csrf-cookie").then((response) => {
            axios.post("/api/store/unit/add", {
                unit_name: this.FormU.name,
                unit_one: this.FormU.one,
                unit_to: this.FormU.to
              })
              .then((response) => {
                if (response.data.success) {
                  this.Unit = response.data.que
                  this.daBTU = true
                  this.FormUType = true
                  this.ShFU = false
                  this.FormU.name = ''
                  this.FormU.one = 1
                  this.FormU.to = 1
                  this.$Progress.finish();
                  this.$notify({ group: "sucess", title: "ສຳເລັດ!", text: " ຂໍ້ມູນໄດ້ຖຶກບັນທຶກແລ້ວ! " }, 2000)
          
                } else {
                  this.daBTU = true
                  this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+response.data.message }, 4000)
                  this.$Progress.fail();
                }
              })
              .catch((error)=> {
                this.daBTU = true
                 this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+error.message }, 4000)
               // console.error(error);
              });
            });
          }
          else
          {
            this.$axios.get("/sanctum/csrf-cookie").then((response) => {
            axios.post(`/api/store/unit/update/${this.FormUID}`, {
                unit_name: this.FormU.name,
                unit_one: this.FormU.one,
                unit_to: this.FormU.to
              })
              .then((response) => {
                if (response.data.success) {
                  this.Unit = response.data.que
                  this.daBTU = true
                  this.FormUType = true
                  this.ShFU = false
                  this.FormU.name = ''
                  this.FormU.one = 1
                  this.FormU.to = 1
                  this.$Progress.finish();
                  this.$notify({ group: "sucess", title: "ສຳເລັດ!", text: " ຂໍ້ມູນໄດ້ຖຶກອັບເດດແລ້ວ! " }, 2000)
                } else {
                  this.error = response.data.message;
                  this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+response.data.message }, 4000)
                  this.$Progress.fail();
                }
              })
              .catch((error)=> {
                this.daBTU = true
                this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+error.message }, 4000)
                //console.error(error);
              });
            });
          }
    },
    EditUnit(id){
      this.FormUID = id
      this.ShFU = true
      this.FormUType = false
      let item = this.Unit.data.find(i => i.id == id)
      this.FormU.name = item.unit_name
      this.FormU.one = item.unit_one
      this.FormU.to = item.unit_to
      this.polling = setInterval(() => {
         this.$refs.u_name_focus.focus();
          clearInterval(this.polling);
        }, 900);
    },
    DelUnit(id){
       this.$swal.fire({
            title: 'ທ່ານແນ່ໃຈບໍ່?',
            text: "ທີ່ຈະລຶບຂໍ້ມູນນີ້!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'ລຶບເລີຍ!',
            cancelButtonText: 'ຍົກເລີກ'
            }).then((result) => {
            if (result.isConfirmed) {
                this.$Progress.start()
                this.$axios.get('/sanctum/csrf-cookie').then(response => {
                    this.$axios.delete(`/api/store/unit/delete/${id}`)
                        .then(response => {
                             this.GetAllUnit()
                            if (response.data.success) {
                              this.$Progress.finish()
                            this.$notify({ group: "sucess", title: "ສຳເລັດ!", text: response.data.message }, 2000)
                            } else {
                               this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+response.data.message }, 4000)
                              this.$Progress.fail()
                            }
                        })
                        .catch( (error)=> {
                          this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+error.message }, 4000)
                            //console.error(error)
                            this.$Progress.fail()
                        });
                })
            }
            })
    },
     GetAllUnit(bpage){
      this.LoadingU = true
            this.$Progress.start();
           if (typeof bpage == "undefined") {
            this.$axios.get("/sanctum/csrf-cookie").then((response) => {
              axios
                .get(`/api/store/unit?page=${this.Unit.current_page}`)
                .then((response) => {
                  this.LoadingU = false
                  //console.log(response.data.all_que)
                  if (response.data.data.length < 1) {
                    let bp = this.Unit.current_page - 1;
                    if (bp >= 1) {
                      this.GetAllUnit(bp);
                    } else {
                      this.Unit = response.data
                    }
                  } else {

                    this.Unit = response.data
                    this.$Progress.finish();
                  }
                })
                .catch((error) => {
                  this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+error.message }, 4000)
                  this.LoadingU = false
                  this.Unit = ''
                  this.$Progress.fail();
                });
            });
          } else {
            this.$axios.get("/sanctum/csrf-cookie").then((response) => {
              axios
                .get(`/api/store/unit?page=${bpage}`)
                .then((response) => {
                  this.LoadingU = false
                  this.Unit = response.data.que;
                  this.$Progress.finish();
                })
                .catch((error) => {
                  this.$notify({ group: "error", title: "ບໍ່ສຳເລັດ!", text: 'ຜິດຜາດ: '+error.message }, 4000)
                  this.LoadingU = false
                  this.$Progress.fail();
                });
            });
          }
    },
  },
  created() {
    this.$axios.get(`/api/users/checkauth`).then((response) => {
      if (!response.data.isLogin) {
        window.location.href = "/";
      }
    });
    this.GetAllBrand()
    this.GetAllUnit()

    if(window.Laravel.isLoggedin){
           this.isLoggedIn = true
        }
  },

  beforeRouteEnter(to, from, next) {
    if (!window.Laravel.isLoggedin) {
      window.location.href = "/";
    }
    next();
  },
};
</script>

<style lang="scss" scoped>
</style>