<template>
  <div style="width: 100%">
    <!-- SNACKBAR -->
    <v-snackbar
      v-model="snackbar"
      :timeout="5000"
      :multi-line="false"
      :vertical="false"
      :top="true"
    >
      {{ msgSnackbar }}
      <v-btn flat @click="updateAlertSuccess(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 -->

    <!-- ERROR DIALOG -->
    <v-dialog v-model="dialogerrordata" 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">{{ msgErrors }}</div>
        <v-divider></v-divider>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="primary" flat @click="dialogerrordata = 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"
        >TRANSAKSI PENGELUARAN BARANG</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 wrap class="px-2 py-2">
        <v-flex xs3>
          <v-menu
            v-model="menuStartDate"
            :close-on-content-click="false"
            transition="scale-transition"
            :nudge-right="40"
            lazy
            offset-y
            max-width="290px"
            min-width="290px"
          >
            <template v-slot:activator="{ on }">
              <v-text-field
                v-model="startDateFormatted"
                label="Tanggal Awal"
                class="mr-2"
                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 xs3>
          <v-menu
            v-model="menuEndDate"
            :close-on-content-click="false"
            transition="scale-transition"
            :nudge-right="40"
            lazy
            offset-y
            max-width="290px"
            min-width="290px"
          >
            <template v-slot:activator="{ on }">
              <v-text-field
                v-model="endDateFormatted"
                label="Tanggal Akhir"
                class="mr-2"
                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 xs2>
          <v-autocomplete
            v-model="fStatus"
            menu-icon="mdi-chevron-down"
            :items="statusOptions"
            item-text="title"
            return-object
            label="Status"
            class="mr-2"
            outline
            hide-details
          ></v-autocomplete>
        </v-flex>
        <v-flex xs2>
          <v-text-field
            v-model="fSearch"
            label="Cari..."
            placeholder="NO."
            class="mr-2"
            outline
            hide-details
          ></v-text-field>
        </v-flex>
          <v-btn
            dark
            color="primary"
            style="min-width: 50px; height: 50px; margin: 0"
            @click="mainTableSearch()"
          >
            <v-icon>search</v-icon>
          </v-btn>
      </v-layout>
    </v-card>
    <v-card class="pa-2 mt-2">
      <v-layout row style="max-height: 600px; overflow: auto">
        <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
          <v-data-table
            :headers="headers"
            :items="mainTable"
            :loading="mainTableLoading"
            hide-actions
            class="elevation-1"
          >
            <template slot="items" slot-scope="props">
              <td
                class="text-xs-center pa-2"
                v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                @click="selectMe(props.item)"
              >
                <div
                  style="
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                  "
                >
                  <p class="mb-0 font-weight-bold" style="color:#000000">{{ props.item.T_ItemOutNumber }}</p>
                </div>
              </td>
              <td
                class="text-xs-left pa-2"
                v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                @click="selectMe(props.item)"
              >
                {{ deFormatedDate(props.item.T_ItemOutDate) }}
              </td>
              <td
                class="text-xs-left pa-2"
                v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                @click="selectMe(props.item)"
              >
                  {{ props.item.T_ItemOutNote }}
              </td>
              <td
                class="text-xs-left pa-2"
                v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                @click="selectMe(props.item)"
              >
                  {{ props.item.DivisionName }}
              </td>
              <td
                class="text-xs-left pa-2"
                v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                @click="selectMe(props.item)"
              >
                <v-chip
                  small
                  :text-color="
                    props.item.T_ItemOutStatus === 'Draft'
                      ? 'black'
                      : 'white'
                  "
                  :color="
                    getChipStatusColor(
                      props.item.T_ItemOutStatus
                    )
                  "
                >
                  {{ props.item.T_ItemOutStatus }}
                </v-chip>
              </td>
              <td
                class="text-xs-center pa-2"
                v-bind:class="{
                  'amber lighten-4': isSelected(props.item),
                }"
                @click="selectMe(props.item)"
              >
                <span v-if="props.item.T_ItemOutStatus == 'Draft'">

                  <v-btn 
                    :disabled="mainTableLoading || approveLevelID == '2' || props.item.T_ItemOutApproveID != null"
                    v-if="props.item.T_ItemOutIsConfirm == 'Y'"
                    round depressed color="primary" small :outline="props.item.T_ItemOutApproveID == null"
                    @click="openApprove(props.item, )"
                  > 
                    Approve (Manager) 
                  </v-btn>

                  <v-btn
                    style="min-width: 25px; height: 25px; margin: 0;"
                    v-if="props.item.T_ItemOutApproveID != null && approveLevelID == '2'"
                    small
                    color="blue" title="Proses transaksi"
                    @click="openProcess(props.item)"
                  >
                    <v-icon small color="white">check_circle</v-icon>
                  </v-btn>

                  <v-btn
                    v-if="props.item.T_ItemOutIsConfirm == 'N' && approveLevelID == '2' && props.item.T_ItemOutApproveID == null"
                    style="min-width: 25px; height: 25px; margin: 0;"
                    small
                    color="primary" title="Konfirmasi"
                    @click="openConfirm(props.item)"
                  >
                    <v-icon small color="white">check_circle_outline</v-icon>
                  </v-btn>
                  <!-- <v-btn
                    v-if="props.item.T_ItemOutIsConfirm == 'N' && approveLevelID == '2' && props.item.T_ItemOutApproveID == null"
                    style="min-width: 25px; height: 25px; margin: 0;"
                    small
                    color="black" title="Edit"
                    @click="updateRequest(props.item)"
                  >
                    <v-icon small color="white">edit</v-icon>
                  </v-btn>
                  <v-btn
                    v-if="props.item.T_ItemOutIsConfirm == 'N' && approveLevelID == '2' && props.item.T_ItemOutApproveID == null"
                    style="min-width: 25px; height: 25px; margin: 0;"
                    small
                    color="red" title="Hapus"
                    @click="deleteRequest(props.item)"
                  >
                    <v-icon small color="white">delete</v-icon>
                  </v-btn> -->
                </span>
                <span v-if="props.item.T_ItemOutStatus == 'Process' || props.item.T_ItemOutStatus == 'Rejected'">
                  <v-btn
                    style="min-width: 25px; height: 25px; margin: 0;"
                    small
                    color="green" title="Preview"
                    @click="openPreview(props.item)"
                  >
                    <v-icon small color="white">receipt</v-icon>
                  </v-btn>
                </span>
              </td>
            </template>
          </v-data-table>
        </v-flex>
      </v-layout>
      <v-divider></v-divider>
      <v-pagination
        style="margin-top: 10px; margin-bottom: 10px"
        v-model="mainTableCurrentPage"
        :length="mainTableTotalPage"
      ></v-pagination>
    </v-card>

    <v-dialog v-model="dialogAlertDeleteRequest" max-width="30%">
      <v-card>
        <v-card-title class="headline grey lighten-2 pt-2 pb-2" primary-title>
          Peringatan !
        </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 xs12>
                      {{ msgAlertDeleteRequest }}
                    </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="primary" flat @click="dialogAlertDeleteRequest = false">
            Tutup
          </v-btn>
          <v-btn color="primary" flat @click="closeAndDeleteRequest()">
            Yakin lah
          </v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>

    <v-dialog v-model="dialogAlertDeleteDetail" max-width="30%">
      <v-card>
        <v-card-title class="headline grey lighten-2 pt-2 pb-2" primary-title>
          Peringatan !
        </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 xs12>
                      {{ msgAlertDeleteRequest }}
                    </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="primary" flat @click="dialogAlertDeleteDetail = false">
            Tutup
          </v-btn>
          <v-btn color="primary" flat @click="closeAndDeleteDetail()">
            Yakin lah
          </v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>

    <add-item-out-dialog></add-item-out-dialog>
    <add-item-detail-dialog></add-item-detail-dialog>
    <dialog-item-approve></dialog-item-approve>
    <dialog-item-process></dialog-item-process>
    <dialog-item-preview></dialog-item-preview>
    <dialog-item-confirm></dialog-item-confirm>

  </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: {
    "add-item-out-dialog": httpVueLoader(
      "./addItemOutDialog.vue"
    ),
    "add-item-detail-dialog": httpVueLoader("./addItemDetailDialog.vue"),
    "dialog-item-approve": httpVueLoader("./dialogItemApprove.vue"),
    "dialog-item-process": httpVueLoader("./dialogItemProcess.vue"),
    "dialog-item-preview": httpVueLoader("./dialogItemPreview.vue"),
    "dialog-item-confirm": httpVueLoader("./dialogItemConfirm.vue"),
    "one-dialog-alert": httpVueLoader("../../common/oneDialogAlert.vue"),
    "one-dialog-error": httpVueLoader("../../common/oneDialogError.vue"),
  },
  data() {
    return {
      msgAlertDeleteRequest: "",
      dialogAlertDeleteRequest: false,
      dialogAlertDeleteDetail: false,
      xDescription: "",
      xRIOID: 0,
      xRIODID: 0,
      headers: [
        {
          text: "NO",
          align: "center",
          sortable: false,
          value: "no",
          width: "15%",
          class: "blue lighten-3 white--text",
        },
        {
          text: "TANGGAL",
          align: "left",
          sortable: false,
          value: "date",
          width: "15%",
          class: "blue lighten-3 white--text",
        },
        {
          text: "CATATAN",
          align: "left",
          sortable: false,
          value: "warehouse",
          width: "25%",
          class: "blue lighten-3 white--text",
        },
        {
          text: "DIVISI",
          align: "left",
          sortable: false,
          value: "warehouse",
          width: "15%",
          class: "blue lighten-3 white--text",
        },
        {
          text: "STATUS",
          align: "left",
          sortable: false,
          value: "status",
          width: "15%",
          class: "blue lighten-3 white--text",
        },
        {
          text: "AKSI",
          align: "center",
          sortable: false,
          value: "action",
          width: "15%",
          class: "blue lighten-3 white--text",
        },
      ],
    };
  },
  mounted() {
    this.$store.dispatch("requester/getlevel");
    this.$store.dispatch("requester/search");
  },
  computed: {
    msgSnackbar() {
      return this.$store.state.requester.successMessage;
    },
    snackbar: {
      get() {
        return this.$store.state.requester.alertSuccess;
      },
      set(val) {
        this.$store.commit("requester/updateAlertSuccess", val);
      },
    },
    dialog_error: {
      get() {
        return this.$store.state.requester.alertError;
      },
      set(val) {
        this.$store.commit("requester/updateAlertError", val);
      },
    },
    dialogerrordata: {
      get() {
        return this.$store.state.requester.dialogErrors;
      },
      set(val) {
        this.$store.commit("requester/updateDialogErrors", val);
      },
    },
    msgError() {
      return this.$store.state.requester.errMessage;
    },
    msgErrors() {
      return this.$store.state.requester.errors;
    },
    mainTable() {
      return this.$store.state.requester.mainTable;
    },
    detailTable() {
      return this.$store.state.requester.detailTable;
    },
    mainTableLoading() {
      return this.$store.state.requester.mainTableLoadingStatus === 1;
    },
    detailTableLoading() {
      return this.$store.state.requester.detailTableLoadingStatus === 1;
    },
    mainTableTotalPage: {
      get() {
        return this.$store.state.requester.countMainTable;
      },
      set(val) {
        this.$store.commit("requester/updateCountMainTable", val);
      },
    },
    mainTableCurrentPage: {
      get() {
        return this.$store.state.requester.mainTableCurrentPage;
      },
      set(val) {
        this.$store.commit("requester/updateMainTableCurrentPage", val);
        this.$store.commit("requester/updateMainTableLastId", -1);
        this.$store.dispatch("requester/search");
      },
    },
    selectedMainTable: {
      get() {
        return this.$store.state.requester.selectedMainTable;
      },
      set(val) {
        this.$store.commit("updateSelectedMainTable", val);
      },
    },
    menuStartDate: {
      get() {
        return this.$store.state.requester.menuStartDate;
      },
      set(val) {
        this.$store.commit("requester/updateMenuStartDate", val);
      },
    },
    menuEndDate: {
      get() {
        return this.$store.state.requester.menuEndDate;
      },
      set(val) {
        this.$store.commit("requester/updateMenuEndDate", val);
      },
    },
    startDateFormatted() {
      return this.formatDate(this.fStartDate);
    },
    endDateFormatted() {
      return this.formatDate(this.fEndDate);
    },
    dStartDate: {
      get() {
        return this.$store.state.requester.dStartDate;
      },
      set(val) {
        this.$store.commit("requester/updateDStartDate", val);
      },
    },
    dEndDate: {
      get() {
        return this.$store.state.requester.dEndDate;
      },
      set(val) {
        this.$store.commit("requester/updateDEndDate", val);
      },
    },
    fStartDate: {
      get() {
        return this.$store.state.requester.fStartDate;
      },
      set(val) {
        this.$store.commit("requester/updateFStartDate", val);
      },
    },
    fEndDate: {
      get() {
        return this.$store.state.requester.fEndDate;
      },
      set(val) {
        this.$store.commit("requester/updateFEndDate", val);
      },
    },
    fStatus: {
      get() {
        return this.$store.state.requester.fStatus;
      },
      set(val) {
        this.$store.commit("requester/updateFStatus", val);
      },
    },
    statusOptions() {
      return this.$store.state.requester.statusOptions;
    },
    fSearch: {
      get() {
        return this.$store.state.requester.fSearch;
      },
      set(val) {
        this.$store.commit("requester/updateFSearch", val);
      },
    },
    xNumber: {
      get() {
        return this.$store.state.requester.xNumber;
      },
      set(val) {
        this.$store.commit("requester/updateXNumber", val);
      },
    },
    xIOID: {
      get() {
        return this.$store.state.requester.xIOID;
      },
      set(val) {
        this.$store.commit("requester/updateXIOID", val);
      },
    },
    xIODID: {
      get() {
        return this.$store.state.requester.xIODID;
      },
      set(val) {
        this.$store.commit("requester/updateXIODID", val);
      },
    },
    xDescriptionDetail() {
      return this.$store.state.requester.xDescriptionDetail;
    },
    selectedItems: {
      get() {
        return this.$store.state.requester.selectedItems;
      },
      set(val) {
        this.$store.commit("requester/updateSelectedItems", val);
      }
    },
    approveLevelID: {
      get() {
        return this.$store.state.requester.approveLevelID;
      },
      set(val) {
        this.$store.commit("requester/updateApproveLevelID", val);
      },
    },
  },
  methods: {
    convertMoney(money){
        return one_money(money)
    },
    isSelected(p) {
      return (
        p.T_ItemOutID ==
        this.$store.state.requester.selectedMainTable.T_ItemOutID
      );
    },
    selectMe(spr) {
      this.$store.commit("requester/updateSelectedMainTable", spr);
    },
    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")}`;
    },
    getChipStatusColor(status) {
      switch (status) {
        case "Process":
          return "success";
        case "Rejected":
          return "error";
        default:
          return "";
      }
    },
    getChipStatusDetailColor(status) {
      switch (status) {
        case "Pending":
          return "warning";
        case "Process":
          return "success";
        case "Read":
          return "info";
        case "Process":
          return "warning";
        default:
          return "";
      }
    },
    mainTableSearch() {
      this.$store.dispatch("requester/search");
      this.$store.commit("requester/updateSelectedMainTable", {});
      this.$store.commit("requester/updateDetailTable", []);
    },
    resetItemTable() {
      this.$store.commit("requester/updateSelectedItems", []);
      this.$store.commit("requester/updateItems", []);
      this.$store.commit("requester/updateSearchItem", "");
      this.$store.commit("requester/updateXWarehouse", "");
      this.$store.commit("requester/updateLoadingItem", false);
    },
    openDialogRequest() {
      this.resetItemTable()
      this.$store.dispatch("requester/getWarehouse").then(() => {
        const warehouseId = this.$store.state.requester.xWarehouse.WarehouseID;
        const divisonId = this.$store.state.requester.selectedDivision.DivisionID;
        this.$store.dispatch("requester/getItemRequest", {
          search: "",
          warehouseID: warehouseId,
          divisionID: divisonId
        });
      })
      this.$store.dispatch("requester/getDivisionByUser",{
        act: 'new',
        DivisionID: ''
      }).then(() => {
        const warehouseId = this.$store.state.requester.xWarehouse.WarehouseID;
        const divisonId = this.$store.state.requester.selectedDivision.DivisionID;
        this.$store.dispatch("requester/getItemRequest", {
          search: "",
          warehouseID: warehouseId,
          divisionID: divisonId
        });
      })
      this.$store.commit(
        "requester/updateXDateUse",
        moment(new Date()).format("YYYY-MM-DD")
      );
      this.$store.commit("requester/updateXNote", "");
      this.$store.commit("requester/updateAct", "new");
      this.$store.commit("requester/updateMainTableLastId", -1);
      this.$store.commit("requester/updateDialogRequest", true);
    },
    isbranch() {
      if (this.$store.state.requester.user.M_BranchID != 0) {
        return this.$store.state.requester.user.M_BranchCode
      } else  {
        return ''
      }
    },
    updateRequest(val) {
      this.$store.dispatch("requester/getWarehouse").then(() => {
        const warehouseId = this.$store.state.requester.xWarehouse.WarehouseID;
        const divisonId = this.$store.state.requester.selectedDivision.DivisionID;
        this.$store.dispatch("requester/getItemRequest", {
          search: "",
          warehouseID: warehouseId,
          divisionID: divisonId
        });
      })
      this.$store.dispatch("requester/getDivisionByUser", {
        act: 'edit',
        DivisionID: val.DivisionID
      }).then(() => {
        const warehouseId = this.$store.state.requester.xWarehouse.WarehouseID;
        const divisonId = this.$store.state.requester.selectedDivision.DivisionID;
        this.$store.dispatch("requester/getItemRequest", {
          search: "",
          warehouseID: warehouseId,
          divisionID: divisonId
        });
      })
      this.$store.commit(
        "requester/updateXDateUse",
        val.T_ItemOutDate
      );
      this.$store.commit(
        "requester/updateXNote",
        val.T_ItemOutNote
      );
      this.$store.commit(
        "requester/updateXWarehouse",
        val.WarehouseID
      );
      this.$store.dispatch("requester/getDetailItemOut",{
        itemoutID: val.T_ItemOutID
      });
      
      this.$store.commit("requester/updateXIOID", val.T_ItemOutID);
      this.$store.commit("requester/updateAct", "edit");
      this.$store.commit("requester/updateDialogRequest", true);
    },
    deleteRequest(data) {
      console.log(data);
      this.xIOID = data.T_ItemOutID;
      this.xNumber = data.T_ItemOutNumber;
      this.xRIOID = data.T_ItemOutDetailRequestItemOutID,
      this.xRIODID = data.T_ItemOutDetailRequestItemOutDetailID,

      this.$store.dispatch("requester/getDetailItemOut",{
        itemoutID: data.T_ItemOutID
      });

      this.msgAlertDeleteRequest = `Yakin, mau hapus pengeluaran barang dengan nomor [${data.T_ItemOutNumber}] ?`;
      this.dialogAlertDeleteRequest = true;
    },
    closeAndDeleteRequest() {
      this.$store.dispatch("requester/deleteRequest", {
        IOID: this.xIOID,
        Number: this.xNumber,
        RIOID: this.xRIOID,
        RIODID: this.xRIODID
        // items: this.selectedItems
      });
      this.dialogAlertDeleteRequest = false;
    },
    openDialogDetail() {
      this.$store.commit("requester/updateSelectedItem", {});
      this.$store.commit("requester/updateSelectedItemUnit", {});
      this.$store.commit("requester/updateItemUnits", [])
      this.$store.commit("requester/updateXQty", "");
      this.$store.commit("requester/updateXPrice", "");
      this.$store.commit("requester/updateXIsCito", false);
      this.$store.commit("requester/updateAct", "new");
      this.$store.commit("requester/updateDialogDetail", true);
      this.$store.commit("requester/updateErrorsform", [])
      this.$store.commit("requester/updateTempBatchNumber", {
        records: [],
        total: 0
      });
    },
    updateDetail(val) {
      this.$store.commit("requester/updateErrorsform", [])
      this.$store.commit(
        "requester/updateItemList", [{
          M_ItemID: val.M_ItemID, 
          M_ItemDesc: val.M_ItemDesc
        }]);
      this.$store.commit(
        "requester/updateSelectedItem", {
          M_ItemID: val.M_ItemID, 
          M_ItemDesc: val.M_ItemDesc
        });
      this.$store.commit(
        "requester/updateXQty",
        val.T_ItemOutDetailQty
      );
      this.$store.commit(
        "requester/updateItemUnits", [{
          ItemUnitID: val.ItemUnitID,
          ItemUnitName: val.ItemUnitName
        }]
      );
      this.$store.commit(
        "requester/updateSelectedItemUnit", {
          ItemUnitID: val.ItemUnitID,
          ItemUnitName: val.ItemUnitName
        }
      );
      this.$store.commit("requester/updateSelectedDetailTable", val);
      this.$store.commit("requester/updateAct", "edit");
      this.$store.commit("requester/updateDialogDetail", true);
    },
    deleteDetail(data) {
      this.xIODID = data.T_ItemOutDetailID;
      this.xDescription = data.M_ItemDesc;
      this.msgAlertDeleteRequest = `Yakin, mau hapus Request [${data.M_ItemDesc}] ?`;
      this.dialogAlertDeleteDetail = true;
    },
    closeAndDeleteDetail() {
      this.$store.dispatch("requester/deleteDetail", {
        IODID: this.xIODID,
        itemDescription: this.xDescription
      });
      this.dialogAlertDeleteDetail = false;
    },
    orderConfirm(id, status) {
      if (status === "Draft") {
        this.$store.dispatch("requester/orderConfirm", {
          itemoutID: id,
        });
      }
    },
    thrSearchDate: _.debounce(function () {
        this.$store.dispatch("requester/search")
    }, 1000),
    thrSearch: _.debounce(function () {
        this.$store.dispatch("requester/search")
    }, 1000),
    openConfirm(val) {
      this.$store.commit("requester/updateDialogConfirm", true);
      this.xIOID = val.T_ItemOutID;
      this.$store.dispatch("requester/getItemOuts", {
        ioID: val.T_ItemOutID
      });
    },
    openApprove(val) {
      this.$store.commit("requester/updateDialogApprove", true);
      this.xIOID = val.T_ItemOutID;
      this.$store.dispatch("requester/getItemOuts", {
        ioID: val.T_ItemOutID
      });
    },
    openProcess(val) {
      this.$store.commit("requester/updateDialogProcess", true);
      this.xIOID = val.T_ItemOutID;
      this.$store.dispatch("requester/getItemOuts", {
        ioID: val.T_ItemOutID
      });
    },
    openPreview(val) {
      this.$store.commit("requester/updateDialogPreview", true);
      this.xIOID = val.T_ItemOutID;
      this.$store.dispatch("requester/getItemOuts", {
        ioID: val.T_ItemOutID
      });
    }
  },

  watch: {
    // fStartDate(val, old) {
    //   this.fStartDate = val
    //   this.thrSearchDate()
    // },
    // fEndDate(val, old) {
    //   this.fEndDate = val
    //   this.thrSearchDate()
    // },
    // fSearch(val, old) {
    //   this.fSearch = val
    //   this.thrSearch()
    // }
  }
};
</script>
