<template>
  <div>
    <div class="block">
      <p v-html="content"></p>
    </div>
    <div class="is-flex">
      <div class="buttons mb-0">
        <button
          class="button is-danger"
          @click="remove"
        >
          Đồng ý
        </button>
        <button
          class="button is-white"
          @click="cancel"
        >
          Hủy
        </button>
      </div>
      <div
        class="control"
        v-if="duration"
      >
        <CountDown
          :duration="duration"
          @close="cancel"
        />
      </div>
    </div>
  </div>
</template>
<script>
export default {
  props: ["content", "duration", "vbind", "setdeleted"],
  methods: {
    cancel() {
      this.$emit("close");
    },
    async remove() {
      let pagename = this.vbind.pagename;
      let pagedata = this.$store[pagename];
      let name = pagedata.origin_api.name || this.vbind.api;
      let id = this.vbind.row.id;
      let result;
      if (this.setdeleted) {
        let record = await this.$getdata(name, { first: true, filter: { id } });
        record.deleted = 1;
        result = await this.$patchapi(name, record);
      } else result = await this.$deleteapi(name, id);
      if (result === "error") return this.$dialog("Đã xảy ra lỗi, xóa dữ liệu thất bại", "Lỗi", "Error");
      this.$snackbar("Dữ liệu đã được xoá khỏi hệ thống", "Success");
      let arr = Array.isArray(id) ? id : [{ id: id }];
      let copy = this.$copy(this.$store[pagename].data);
      arr.map((x) => {
        let index = copy.findIndex((v) => v.id === x.id);
        index >= 0 ? this.$delete(copy, index) : false;
      });
      this.$store.commit("updateState", {
        name: pagename,
        key: "update",
        data: { data: copy },
      });
      this.cancel();
    },
  },
};
</script>
