<template>
  <div>
    <p v-html="content"></p>
    <p class="border-bottom mt-4 mb-5"></p>
    <div class="field is-grouped">
      <div class="control is-expanded">
        <button
          class="button is-danger"
          @click="remove()"
        >
          Đồng ý
        </button>
        <button
          class="button is-dark ml-5"
          @click="cancel()"
        >
          Hủy bỏ
        </button>
      </div>
      <div
        class="control"
        v-if="duration"
      >
        <CountDown
          v-bind="{ duration: duration }"
          @close="cancel()"
        ></CountDown>
      </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.state[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, { id: id }, undefined, true);
        record.deleted = 1;
        result = await this.$updateapi(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.state[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>
