<template>
  <div>
    <!-- main layout -->
    <v-toolbar color="primary" dark>
      <v-toolbar-title class="white--text"
        > {{ user.M_UserM_ApproveLevelID == '1' ? 'DAFTAR PENDING VERIFIKASI' : 'DAFTAR PENDING APPROVE'}} </v-toolbar-title
      >
      <v-spacer></v-spacer>
    </v-toolbar>
    <v-card class="pa-2 mt-2">
      <div>
        <v-data-table
          :headers="headers"
          :items="listdata"
          :loading="loading"
          hide-actions
          class="elevation-1"
        >
          <template v-slot:items="props">
            <td class="text-xs-center">{{ props.item.type }}</td>
            <td class="text-xs-center">{{ user.M_UserM_ApproveLevelID == '1' ? 'Jumlah pending verifikasi : ' : 'Jumlah pending approve : '}}<span class="font-weight-bold">{{ props.item.total }}</span></td>
            <td class="text-xs-center">{{ props.item.number }}</td>
            <td class="text-xs-center">{{ deFormatedDate(props.item.date) }}</td>
            <td class="text-xs-center">
                <v-btn v-if="user.M_UserM_ApproveLevelID == '1'" color="blue" @click="openApprove(props.item)" title="Menuju ke halaman verifikasi" dark>Verifikasi 
                    <v-icon dark right>forward</v-icon>
                </v-btn>
                <v-btn v-else-if="user.M_UserM_ApproveLevelID == '2'" color="blue" @click="openApprove(props.item)" title="Menuju ke halaman approve" dark>Approve
                    <v-icon dark right>forward</v-icon>
                </v-btn>
                <v-btn v-else color="blue" @click="openApprove(props.item)" title="Menuju ke halaman approve" dark>Button
                    <v-icon dark right>forward</v-icon>
                </v-btn>
            </td>
          </template>
        </v-data-table>
      </div>
    </v-card>
  </div>
</template>

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

  .flex-items-center {
    display: flex;
    align-items: center;
    gap: 0.25rem;
  }

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

  .table.v-table thead tr {
    height: 40px;
  }
</style>

<script>
  module.exports = {
    components: {},
    data() {
      return {
        menuSelectedDate: false,
        menuSelectedEndDate: false,
        headers: [
          {
            text: "NAMA MENU",
            align: "center",
            sortable: false,
            value: "no",
            width: "20%",
            class: "blue lighten-3 white--text",
          },
          {
            text: "JUMLAH TRANSAKSI",
            align: "center",
            sortable: false,
            value: "no",
            width: "20%",
            class: "blue lighten-3 white--text",
          },
          {
            text: "INFO NUMBER",
            align: "center",
            sortable: false,
            value: "no",
            width: "30%",
            class: "blue lighten-3 white--text",
          },
          {
            text: "TANGGAL AWAL",
            align: "center",
            sortable: false,
            value: "no",
            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("approve/search")
    },
    computed: {
      user() {
        return this.$store.state.approve.user;
      },
      listdata() {
        return this.$store.state.approve.listdata;
      },
      loading: {
        get() {
          return this.$store.state.approve.loading;
        },
        set(val) {
          this.$store.commit("approve/update_loading", val);
        },
      },
      current_page: {
        get() {
          return this.$store.state.approve.current_page;
        },
        set(val) {
          this.$store.commit("approve/update_current_page", val);
        },
      },
      total_page: {
        get() {
          return this.$store.state.approve.total_page;
        },
        set(val) {
          this.$store.commit("approve/update_total_page", val);
        },
      },
      xsearch: {
        get() {
          return this.$store.state.approve.xsearch;
        },
        set(val) {
          this.$store.commit("approve/update_xsearch", val);
        },
      }
    },
    methods: {
    //   isSelected(p) {
    //     return (
    //       p.PurchaseRequestDirectNumber ==
    //       this.$store.state.prkacab.selectedMainTable
    //         .PurchaseRequestDirectNumber
    //     );
    //   },
      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")}`;
      },
      openApprove(val) {
        let status = val.status;
        let date = val.date

        if (val.flag === 'p') {
          location.href = "/one-ui/test/vuex/accone-purchase-request-kacab/" + "?flag=G&status=" + status + "&startdate=" + date 
        } else if (val.flag === 'np') {
          location.href = "/one-ui/test/vuex/acc-one-purchase-request-po-np-approved/" + "?flag=G&status=" + status + "&startdate=" + date
        } else if (val.flag === 'po') {
          location.href = "/one-ui/test/vuex/acc-one-approve-po/" + "?flag=G&status=" + status + "&startdate=" + date
        } else if (val.flag === 'r') {
          location.href = "/one-ui/test/vuex/acc-one-cashier-v2/" + "?status=" + status + "&startdate=" + date
        } else if (val.flag === 'pa') {
          location.href = "/one-ui/test/vuex/acc-one-supplier-payment-approved-v3/" + "?flag=G&status=" + status + "&startdate=" + date
        } else if (val.flag === 'prd') {
          location.href = "/one-ui/test/vuex/acc-one-purchase-request-direct-approved/" + "?flag=G&status=" + status + "&startdate=" + date
        } else if (val.flag === 'rio') {
          location.href = "/one-ui/test/vuex/acc-one-request-item-out/" + "?flag=G&status=" + status + "&startdate=" + date
        }
      }
    },
    watch: {

    },
  };
</script>
