<template>
  <div style="width: 100%">
    <!-- snackbar -->
    <v-snackbar
        :color="snackbar.color" v-model="snackbar.isOpen"
        :timeout="3000" multi-line top
    >
        {{ snackbar.msg }}
        <v-btn class="ml-2" flat @click="snackbar.isOpen = false">Tutup</v-btn>
    </v-snackbar>

    <!-- ERROR DIALOG -->
    <!-- <v-dialog v-model="dialog_error" max-width="500px">
      <v-card>
        <v-card-title>
          <span>ERROR !</span>
          <v-spacer></v-spacer>
        </v-card-title>
        <v-divider></v-divider>
        <div class="ma-3 red--text">{{ msgError }}</div>
        <v-divider></v-divider>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="primary" flat @click="dialog_error = false"
            >Tutup</v-btn
          >
        </v-card-actions>
      </v-card>
    </v-dialog> -->
    <!-- END ERROR DIALOG -->


    <v-toolbar color="primary" dark>
      <v-toolbar-title class="white--text"
        >RECEIVE MUTASI ANTAR CABANG</v-toolbar-title
      >
      <v-spacer></v-spacer>
      <!-- <v-btn icon @click="openDialogRequest()">
        <v-icon>add_box</v-icon>
      </v-btn> -->
    </v-toolbar>
    <v-card class="pa-2">
      <v-layout row align-center>
        <v-flex pl-2 xs2>
          <v-menu
            v-model="menuStartDate"
            :close-on-content-click="false"
            transition="scale-transition"
            :nudge-right="40"
            lazy
            offset-y
            full-width
            max-width="290px"
            min-width="290px"
          >
            <template v-slot:activator="{ on }">
              <v-text-field
                :value="startDateFormatted"
                label="Tanggal Awal"
                readonly
                outline
                hide-details
                v-on="on"
                @blur="dStartDate = deFormatedDate(startDateFormatted)"
              ></v-text-field>
            </template>
            <v-date-picker
              no-title
              v-model="fStartDate"
              @input="menuStartDate = false"
            ></v-date-picker>
          </v-menu>
        </v-flex>
        <v-flex pl-2 xs2>
          <v-menu
          v-model="menuEndDate"
          :close-on-content-click="false"
          transition="scale-transition"
          :nudge-right="40"
          lazy
          offset-y
          full-width
          max-width="290px"
          min-width="290px"
        >
          <template v-slot:activator="{ on }">
            <v-text-field
              :value="endDateFormatted"
              label="Tanggal Akhir"
              readonly
              outline
              hide-details
              v-on="on"
              @blur="dEndDate = deFormatedDate(endDateFormatted)"
            ></v-text-field>
          </template>
          <v-date-picker
            no-title
            v-model="fEndDate"
            @input="menuEndDate = false"
          ></v-date-picker>
        </v-menu>
        </v-flex>
        <v-flex pl-2 xs2>
          <v-autocomplete
          v-model="fStatus"
          menu-icon="mdi-chevron-down"
          :items="statusOptions"
          item-text="text"
          item-value="value"
          label="Status"
          outline
          hide-details
        ></v-autocomplete>
        </v-flex>
        <v-flex pl-2 xs2>
          <v-text-field
          v-model="fSearch"
          label="Cari..."
          placeholder="NO. "
          outline
          hide-details
        ></v-text-field>
        </v-flex>
        <v-flex pl-2>
          <v-btn
          dark
          color="primary"
          style="min-width: 50px; height: 50px; margin: 0"
          @click="mainSearch()"
        >
          <v-icon>search</v-icon>
        </v-btn>
        </v-flex>

      <v-spacer></v-spacer>
      </v-layout>
      
    </v-card>
    <v-card class="pa-2 mt-2">
      <div style="overflow-y: scroll;">
        <v-data-table
          :headers="headers"
          :items="listdata"
          :loading="isLoading"
          hide-actions
          class="elevation-1"
        >
          <template slot="items" slot-scope="props">
          <tr>
            <td
              class="text-xs-center"
              v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
              @click="selectMe(props.item)"
            >
              {{ props.item.MutasiHandoverNumber }}
            </td>
            <td
              class="text-xs-center"
              v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
              @click="selectMe(props.item)"
            >
                {{
                  deFormatedDate(props.item.MutasiHandoverDate)
                }}
            </td>
            <td
              class="text-xs-left"
              v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
              @click="selectMe(props.item)"
            >
              <p class="mb-0 info--text mono font-weight-bold caption">Tanggal : {{deFormatedDate(props.item.MutasiHandoverReceiveDate)}}</p>
              <p class="mb-0 red--text mono font-weight-bold caption">User : {{props.item.M_UserUsername}}</p>
            </td>
            <td
              class="text-xs-center"
              v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
              @click="selectMe(props.item)"
            >
                {{ props.item.itemCategoryName }}
            </td>
            <td
              class="text-xs-center"
              v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
              @click="selectMe(props.item)"
            >
                {{ props.item.branch_asal_name }}
            </td>
            <td
              class="text-xs-center"
              v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
              @click="selectMe(props.item)"
            >
                {{ props.item.branch_tujuan_name }}
            </td>
            <td
              class="text-xs-center"
              v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
              @click="selectMe(props.item)"
            >
                {{ props.item.MutasiHandoverNote }}
            </td>
            <td
              class="text-xs-center"
              v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
              @click="selectMe(props.item)"
            >
              <v-chip
                small
                :text-color="
                  props.item.MutasiHandoverStatus === 'Draft'
                    ? 'black'
                    : 'white'
                "
                :color="
                  getChipStatusColor(
                    props.item.MutasiHandoverStatus
                  )
                "
              >
                {{ props.item.MutasiHandoverStatus }}
              </v-chip>
            </td>
            
            <td
              class="text-xs-center pa-2"
              style="white-space: nowrap;"
              v-bind:class="{
                'amber lighten-4': isSelected(props.item),
              }"
            >
              <div v-show="props.item.MutasiHandoverStatus != 'Sent'">
                <v-tooltip bottom>
                    <template v-slot:activator="{ on }">
                        <v-btn 
                            style="min-width: 30px; height: 30px; margin: 0;" outline
                            small @click="openBarcode(props.item)" color="black lighten-2"
                            class="mt-1"
                        >
                            <v-icon small color="black" v-on="on">print</v-icon>
                        </v-btn>
                    </template>
                    <span>Print Barcode</span>
                </v-tooltip>
                <v-tooltip bottom>
                    <template v-slot:activator="{ on }">
                        <v-btn 
                            style="min-width: 30px; height: 30px; margin: 0;" outline
                            small @click="openView(props.item)" color="blue lighten-2"
                            class="mt-1 mb-1"
                        >
                            <v-icon small color="blue" v-on="on">visibility</v-icon>
                        </v-btn>
                    </template>
                    <span>Lihat Detail</span>
                </v-tooltip>
                <v-tooltip bottom>
                    <template v-slot:activator="{ on }">
                        <v-btn 
                            style="min-width: 30px; height: 30px; margin: 0;"
                            small @click="printout(props.item)" color="primary"
                            class="mt-1 mb-1"
                        >
                            <v-icon small color="white" v-on="on">print</v-icon>
                        </v-btn>
                    </template>
                    <span>Cetak PDF</span>
                </v-tooltip>
              </div>
              <div v-show="props.item.MutasiHandoverStatus == 'Sent'">
                <v-tooltip bottom>
                    <template v-slot:activator="{ on }">
                        <v-btn 
                            style="min-width: 30px; height: 30px; margin: 0;" outline
                            small @click="openReceive(props.item)" color="teal"
                            class="mt-1 mb-1"
                        >
                            <v-icon small color="teal" v-on="on">move_to_inbox</v-icon>
                        </v-btn>
                    </template>
                    <span>Receive Mutasi</span>
                </v-tooltip>
              </div>
            </td>
          </tr>
          </template>
        </v-data-table>
      </div>
      <v-divider></v-divider>
      <v-pagination
        style="margin-top: 10px; margin-bottom: 10px"
        v-model="currentPage"
        :length="totalPages"
      ></v-pagination>
    </v-card>

    <add-request-mutasi></add-request-mutasi>
    <one-dialog-item-mutasi></one-dialog-item-mutasi>
    <one-dialog-receive-layout></one-dialog-receive-layout>
    <one-dialog-view-layout></one-dialog-view-layout>
    <one-dialog-mutasi></one-dialog-mutasi>
    <one-dialog-print
        :title="printtitle"
        :width="printwidth"
        :height="600"
        :status="openprint"
        :urlprint="urlprint"
        @close-dialog-print="openprint = false"
    ></one-dialog-print>

  </div>
</template>

<style scoped>
.date-type-table {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.table.v-table tbody td,
table.v-table tbody th {
  height: 40px;
}

.table.v-table thead tr {
  height: 40px;
}

.scroll-container {
  scroll-padding: 50px 0 0 50px;
}
</style>

<script>
module.exports = {
  components: {
    "one-dialog-mutasi": httpVueLoader("./dialogPrintBarcode.vue"),
    "one-dialog-item-mutasi": httpVueLoader("./dialogSelectItem.vue"),
    "one-dialog-receive-layout": httpVueLoader("./dialogFormMutasiReceive.vue"),
    "add-request-mutasi": httpVueLoader("./dialogFormMutasi.vue"),
    "add-request-detail-dialog": httpVueLoader("./addRequestDetailDialog.vue"),
    "one-dialog-view-layout": httpVueLoader("./dialogFormMutasiView.vue"),
    "approve-dialog": httpVueLoader("./approveDialog.vue"),
    "one-dialog-alert": httpVueLoader("../../common/oneDialogAlert.vue"),
    "one-dialog-error": httpVueLoader("../../common/oneDialogError.vue"),
    "one-dialog-print": httpVueLoader("../../common/oneDialogPrintX.vue"),
  },
  data() {
    return {
      urlprint: "",
      printtitle: "",
      printwidth: "60%",
      openprint: false,
      msgAlertDeleteRequest: "",
      dialogAlertDeleteRequest: false,
      dialogAlertDeleteDetail: false,
      headers: [
        {
          text: "NO",
          align: "center",
          sortable: false,
          value: "no",
          width: "10%",
          class: "pa-2 blue lighten-3 white--text",
        },
        {
          text: "TANGGAL",
          align: "center",
          sortable: false,
          value: "requestDate",
          width: "10%",
          class: "pa-2 blue lighten-3 white--text",
        },
        {
          text: "RECEIVE",
          align: "center",
          sortable: false,
          value: "requestDate",
          width: "15%",
          class: "pa-2 blue lighten-3 white--text",
        },
        {
          text: "KATEGORI",
          align: "center",
          sortable: false,
          value: "keterangan",
          width: "10%",
          class: "pa-2 blue lighten-3 white--text",
        },
        {
          text: "CABANG ASAL",
          align: "center",
          sortable: false,
          value: "keterangan",
          width: "12%",
          class: "pa-2 blue lighten-3 white--text",
        },
        {
          text: "CABANG TUJUAN",
          align: "center",
          sortable: false,
          value: "keterangan",
          width: "13%",
          class: "pa-2 blue lighten-3 white--text",
        },
        {
          text: "KETERANGAN",
          align: "center",
          sortable: false,
          value: "keterangan",
          width: "15%",
          class: "pa-2 blue lighten-3 white--text",
        },
        {
          text: "STATUS",
          align: "center",
          sortable: false,
          value: "status",
          width: "10%",
          class: "pa-2 blue lighten-3 white--text",
        },
        {
          text: "AKSI",
          align: "center",
          sortable: false,
          value: "action",
          width: "25%",
          class: "pa-2 blue lighten-3 white--text",
        },
      ]
    };
  },
  mounted() {
    this.$store.dispatch("mutasi/search");
  },
  computed: {
    menuStartDate: {
      get() {
        return this.$store.state.mutasi.menu_start_date;
      },
      set(val) {
        this.$store.commit("mutasi/update_menu_start_date", val);
      },
    },
    menuEndDate: {
      get() {
        return this.$store.state.mutasi.menu_end_date;
      },
      set(val) {
        this.$store.commit("mutasi/update_menu_end_date", val);
      },
    },
    startDateFormatted() {
      return this.formatDate(this.fStartDate);
    },
    endDateFormatted() {
      return this.formatDate(this.fEndDate);
    },
    dStartDate: {
      get() {
        return this.$store.state.mutasi.d_start_date;
      },
      set(val) {
        this.$store.commit("mutasi/update_d_start_date", val);
      },
    },
    dEndDate: {
      get() {
        return this.$store.state.mutasi.d_end_date;
      },
      set(val) {
        this.$store.commit("mutasi/update_d_end_date", val);
      },
    },
    fStartDate: {
      get() {
        return this.$store.state.mutasi.f_start_date;
      },
      set(val) {
        this.$store.commit("mutasi/update_f_start_date", val);
      },
    },
    fEndDate: {
      get() {
        return this.$store.state.mutasi.f_end_date;
      },
      set(val) {
        this.$store.commit("mutasi/update_f_end_date", val);
      },
    },
    fStatus: {
      get() {
        return this.$store.state.mutasi.f_status;
      },
      set(val) {
        this.$store.commit("mutasi/update_f_status", val);
      },
    },
    statusOptions() {
      return this.$store.state.mutasi.status_options;
    },
    fSearch: {
      get() {
        return this.$store.state.mutasi.f_search;
      },
      set(val) {
        this.$store.commit("mutasi/update_f_search", val);
      },
    },
    listdata() {
      return this.$store.state.mutasi.list_data;
    },
    isLoading() {
      return this.$store.state.mutasi.isLoading === 1;
    },
    currentPage: {
      get() {
        return this.$store.state.mutasi.current_page;
      },
      set(val) {
        this.$store.commit("mutasi/update_current_page", val);
        this.$store.dispatch("mutasi/search");
      },
    },
    totalPages: {
      get() {
        return this.$store.state.mutasi.total_pages;
      },
      set(val) {
        this.$store.commit("mutasi/update_total_pages", val);
      },
    },
    snackbar: {
      get() {
          return this.$store.state.mutasi.snackbar;
      },
      set(val) {
          this.$store.commit("mutasi/update_snackbar", val);
      }
    },
    approve_level() {
      return this.$store.state.mutasi.approve_level
    }
  },
  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")}`;
    },
    isSelected(p) {
      return (
        p.MutasiHandoverID ==
        this.$store.state.mutasi.selected_list_data
          .MutasiHandoverID
      );
    },
    selectMe(spr) {
      this.$store.commit("mutasi/update_selected_list_data", spr);
    },
    getChipStatusColor(status) {
      switch (status) {
        case "Sent":
          return "yellow darken-2";
        case "Received":
          return "blue";
        default:
          return "";
      }
    },
    openDialogRequest() {
      this.$store.commit("form/update_dialog_form_mutasi", true);
      this.$store.dispatch("form/resetForm");
      this.$store.dispatch("form/getCategory");
      this.$store.dispatch("form/getBranchAsal");
      this.$store.dispatch("form/getBranchTujuan");
    },
    mainSearch() {
      this.$store.dispatch("mutasi/search");
    },
    deleteRM(data) {
      this.$store.commit("mutasi/update_selected_list_data", data);
      this.$store.commit("mutasi/update_dialog_delete", true);
    },
    verifMR(data) {
      this.$store.commit("form/update_dialog_form_mutasi_verif", true);
      this.$store.commit("mutasi/update_selected_list_data", data);
      this.$store.dispatch("mutasi/getItemMutasiUpdate");
      this.$store.dispatch("form/getCategory");
      this.$store.dispatch("form/getBranchAsal");
      this.$store.dispatch("form/getBranchTujuan");
    },
    approveMR(data) {
      this.$store.commit("form/update_dialog_form_mutasi_approve", true);
      this.$store.commit("mutasi/update_selected_list_data", data);
      this.$store.dispatch("mutasi/getItemMutasiUpdate");
      this.$store.dispatch("form/getCategory");
      this.$store.dispatch("form/getBranchAsal");
      this.$store.dispatch("form/getBranchTujuan");
    },
    isApproved(data, type) {
      if (!data) {
        return false
      }

      if (data.MutasiRequestStatus == 'Approved') {
        return false
      }

      if (type == 'M' && data.MutasiRequestIsVerif == 'N') {
        return true
      } else if (type == 'K' && data.MutasiRequestIsApproved == 'N') {
        return true
      } else {
        return false
      }
    },
    openReceive(data) {
      this.$store.commit("mutasi/update_dialog_form_mutasi_receive", true)
      this.$store.commit("mutasi/update_selected_list_data", data);
      this.$store.dispatch("mutasi/getItemDetail");
      this.$store.dispatch("form/getCategory");
      this.$store.dispatch("form/getBranchAsal");
      this.$store.dispatch("form/getBranchTujuan");
      this.$store.dispatch("form/getWarehouse");
      this.$store.dispatch("form/getRuangan");
    },
    openView(data) {
      this.$store.commit("mutasi/update_dialog_form_mutasi_view", true)
      this.$store.commit("mutasi/update_selected_list_data", data);
      this.$store.dispatch("mutasi/getItemDetail");
      this.$store.dispatch("form/getCategory");
      this.$store.dispatch("form/getBranchAsal");
      this.$store.dispatch("form/getBranchTujuan");
      this.$store.dispatch("form/getWarehouse");
      this.$store.dispatch("form/getRuangan");
    },
    openBarcode(data) {
      this.$store.commit("mutasi/update_selected_list_data", data);
      this.$store.commit("mutasi/update_dialog_barcode", true);
      this.$store.commit("mutasi/update_bar_chx_all", false);
      this.$store.commit("mutasi/update_indeterminatex", false);
      this.$store.commit("mutasi/update_selected_barcode", []);
      this.$store.dispatch("mutasi/getBarcodes");
    },
    printout(val) {
      this.printwidth = "60%";
      this.printtitle = "";
      let user = one_user();
      let username = user && user.M_StaffName ? user.M_StaffName : "";
      let tm = Date.now();
      this.urlprint = BASE_URL + "/one-api/report/rpt_penerimaan_mutasi/pdf?id=" + val.MutasiHandoverID + "&username=" + username + "&tm=" + tm;
      this.openprint = true;
    }
  },
};
</script>