<template>
  <div>
    <h3 class="content-block main-title">Penyusunan DRP</h3>
    <p class="content-block">Tahun DRP</p>
    <div class="content-block">
      <div class="dx-card responsive-paddings">
        <div id="app-container">
          <DxPopup
            v-model:visible="isPopupUploadDokumenPendukung"
            :drag-enabled="false"
            :hide-on-outside-click="true"
            :show-close-button="true"
            :show-title="true"
            @showing="onShowing"
            title="Upload Dokumen Pendukung"
          >
            <template #content>
              <PopUpUploadDokumenPendukung :key="id" :drpId="selectedDrpId" :drpTahun="selectedDrpTahun"/>
            </template>
            <DxToolbarItem 
              widget="dxButton"
              :options="saveButtonOptions"
              location="after"
              toolbar="bottom"
            />
            <DxToolbarItem 
              widget="dxButton" 
              :options="cancelButtonOptions" 
              location="after"
              toolbar="bottom"
            />
          </DxPopup>
          <DxDataGrid
            ref="currDataGrid"
            :data-source="dataSource"
            :allow-column-reordering="true"
            :column-auto-width="true"
          >
            <DxEditing
              :allow-adding="true"
              :allow-updating="true"
              :allow-deleting="true"
              :use-icons="true"
              mode="popup"
            >
              <DxTexts
                add-row="Tambah"
                edit-row="Ubah"
                delete-row="Hapus"
                confirm-delete-message="Apakah anda yakin untuk menghapus data ini?"
                save-row-changes="Simpan"
                cancel-row-changes="Batal"
              ></DxTexts>
              <DxForm label-location="top" :col-count="1">
                <DxItem dataField="tahun">
                  <DxRequiredRule message="Tahun harus diisi" />
                </DxItem>
              </DxForm>
            </DxEditing>
            <DxToolbar>
              <DxItem name="groupPanel" />
              <DxItem name="searchPanel" location="before" />
              <DxItem name="addRowButton" show-text="always" css-class="">
                <DxTexts add-row="Tambah"></DxTexts>
              </DxItem>
              <DxItem name="exportButton" />
              <DxItem name="columnChooserButton" />
            </DxToolbar>
            <DxPaging :page-size="5" />
            <DxPager
              :visible="true"
              :allowed-page-sizes="[5, 10, 50]"
              display-mode="compact"
              :show-page-size-selector="true"
              :show-info="true"
              :show-navigation-buttons="true"
              info-text="Hal {0} dari {1} ({2} data)"
            />
            <DxFilterRow :visible="false" />
            <DxColumn
              cell-template="row-cell-template"
              caption="No"
              :width="45"
            ></DxColumn>
            <DxColumn
              data-field="tahun"
              caption="Tahun DRP"
              alignment="left"
            ></DxColumn>
            <DxColumn
              data-field="approveStatus"
              caption="Status Approve"
              alignment="left"
            ></DxColumn>
            <DxColumn
              data-field="approveDate"
              caption="Tanggal Approve"
              data-type="date"
            ></DxColumn>
            <DxColumn type="buttons" caption="Aksi">
              <DxButton
                text="Dokumen"
                icon="attach"
                hint="Dokumen Pendukung DRP"
                :on-click="linkDokumen"
              />
              <DxButton
                text="Detil"
                icon="search"
                hint="Detil/Konten DRP"
                :on-click="linkDetail"
              />
              <DxButton
                text="Kirim"
                icon="movetofolder"
                hint="Kirim DRP"
                :on-click="linkKirim"
              />
            </DxColumn>
            <DxColumn type="adaptive" :width="50">
              <DxButton hint="detail" icon="copy" />
            </DxColumn>
            <template #row-cell-template="{ data }">
              {{ data.rowIndex + 1 }}
            </template>
            <DxSearchPanel
              :visible="true"
              :highlight-case-sensitive="true"
              :width="300"
              placeholder="Cari Penyusunan DRP..."
            />
          </DxDataGrid>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import DxDataGrid, {
  DxEditing,
  DxItem,
  DxForm,
  DxColumn,
  DxFilterRow,
  DxPager,
  DxPaging,
  DxSearchPanel,
  DxToolbar,
  DxTexts,
  DxButton,
  DxRequiredRule,
  // DxLookup,
} from "devextreme-vue/data-grid";
import CustomStore from "devextreme/data/custom_store";
import DxPopup, { DxToolbarItem } from 'devextreme-vue/popup';

import PopUpUploadDokumenPendukung from './drp-upload-dokumen-pendukung.vue'

const approveStatus = [
  { name: "Penyusunan", value: 0 },
  { name: "Approval VP", value: 1 },
  { name: "Rekomendasi Komite", value: 2 },
  { name: "Approval DIRUT", value: 3 },
  { name: "Approved", value: 4 },
  { name: "Revisi DRP", value: 1 },
];

const URL = process.env.VUE_APP_ROOT_API + "/drp";

const dataSource = new CustomStore({
  key: "id",

  load: () => {
    return fetch(URL + process.env.VUE_APP_PAGE_SIZE)
      .then((response) => response.json())
      .then((response) => {
        return {
          data: response.data,
          totalCount: response.pagination.totalRecords,
        };
      })
      .catch(() => {
        throw new Error("Terdapat kesalahan memuat data");
      });
  },

  insert: (values) => {
    return fetch(URL+'/tahun', {
      method: "POST",
      body: JSON.stringify(values),
      headers: {
        "Content-Type": "application/json",
      },
    })
  },

  update: (key, values) => {
    return fetch(URL + "/" + encodeURIComponent(key), {
      method: "PUT",
      body: JSON.stringify(values),
      headers: {
        "Content-Type": "application/json",
      },
    });
  },

  remove: (key) => {
    return fetch(URL + "/" + encodeURIComponent(key), {
      method: "DELETE",
    });
  },
});

// const dataDrpDokumen = new CustomStore({
//   key: "id",
//   load: (key) => {
//     return fetch(URL + "/" + encodeURIComponent(key) + "?size=100")
//       .then((response) => response.json())
//       .then((response) => {
//         console.log("res", response.dataDrpDokumen);
//         console.log("key: ", key);
//         return {
//           data: response.dataDrpDokumen,
//         };
//       })
//       .catch(() => {
//         throw new Error("Terdapat kesalahan memuat data");
//       });
//   },

//   insert: (values) => {
//     return fetch(URL, {
//       method: "POST",
//       body: JSON.stringify(values),
//       headers: {
//         "Content-Type": "application/json",
//       },
//     });
//   },

//   update: (key, values) => {
//     return fetch(URL + "/" + encodeURIComponent(key), {
//       method: "PUT",
//       body: JSON.stringify(values),
//       headers: {
//         "Content-Type": "applicatoin/json",
//       },
//     });
//   },

//   remove: (key) => {
//     return fetch(URL + "/" + encodeURIComponent(key), {
//       method: "DELETE",
//     });
//   },
// });
let dataDrpDokumen = [];
let dataDrpDokumenPendukung = [];

export default {
  setup() {
    return {
      approveStatus,
    };
  },
  components: {
    DxDataGrid,
    DxEditing,
    DxItem,
    DxForm,
    DxColumn,
    DxFilterRow,
    DxPager,
    DxPaging,
    DxSearchPanel,
    DxToolbar,
    DxTexts,
    DxButton,
    DxRequiredRule,
    // DxLookup,
    DxPopup,
    DxToolbarItem,
    PopUpUploadDokumenPendukung,
  },
  methods: {
    linkDokumen(e) {
      this.isPopupUploadDokumenPendukung = !this.isPopupUploadDokumenPendukung;
      this.selectedDrpId = e.row.data.id;
      this.selectedDrpTahun = e.row.data.tahun;
    },
    linkDetail(e) {
      location.href='#/drp/drp-pengadaan?drpid='+e.row.data.id;
    },
    linkKirim($event) {
      console.log("event: ", $event.row.values);
      this.dataDrp = $event.row.values;
    },
    onChangeUploadFile($event) {
      this.fileNames = $event.value;
      console.log("file", this.fileNames);
    },
    onShowing(e, d) {
      console.log(e, d)
    },
  },
  data() {
    return {
      //jsonUrl: URL,
      dataSource,
      dropdown: "",
      dataDrpDokumen,
      dataDrpDokumenPendukung,
      isPopupUploadDokumenPendukung: false,
      selectedDrpId: null,
      selectedDrpTahun: null,
      fileNames: [],
      saveButtonOptions: {
        text: 'Simpan',
        onClick: function(e) { 
          console.log(e);
        }
      },
      cancelButtonOptions: {
        text: 'Batalkan',
        onClick: () => {
          this.isPopupUploadDokumenPendukung = false;
        }
      },
    };
  },
};
</script>

<style lang="scss">
.dx-link.dx-icon-attach.dx-link-icon {
  color: #dbd203;
}
.dx-link.dx-icon-search.dx-link-icon {
  color: #ff9a62;
}
.dx-link.dx-icon-movetofolder.dx-link-icon {
  color: #0996c2;
}
.dx-scrollable {
  padding-bottom: 50px;
}
.dx-field {
  display: flex;
  flex-direction: column;
}
.dx-fieldset-header {
  position: relative;
  margin-bottom: -30px;
  z-index: 20;
  width: fit-content;
}
.fileuploader-container {
  border: solid 1px #dbd203;
  padding: 0;
}
.dx-fileuploader-wrapper {
  padding: 0;
}
</style>
