<template>
  <div>
    <HeaderTools></HeaderTools>
    <TiptapEditor :accordionData="accordionItems"></TiptapEditor>
  </div>
</template>

<script setup>
import { ref, watch, onMounted } from "vue";
import { useElpService } from "~/composables/useElpService";

const { uepGetConflictDoc } = useElpService();

const props = defineProps({
  listConflicts: {
    type: Array,
    default: () => [],
  },
  conflict_id: {
    type: String,
    default: () => "",
  },
});

let accordionItems = ref([
  {
    id: 1,
    tag: "",
    title: "عنوان",
    link_label: "منبع",
    link_url: "",
    content: "متن",
    isOpen: true,
    children: [],
  },
]);

const setDataEditor = async () => {
  let response = await uepGetConflictDoc(props.conflict_id);
  console.log("setDataEditor ", response);

  let _source = response._source;

  let base_url = window.location.origin;
  base_url = base_url.replace("localhost:3007", "localhost:8082");
  base_url = base_url.replace("jedit.tavasi.ir", "majles.tavasi.ir");
  base_url = base_url.replace("192.168.23.112:3000", "majles.tavasi.ir");

  base_url += "/entity/show/view/qasection/{{qanon_id}}?jump_id={{section_id}}";

  accordionItems.value = [];
  let node = {};

  /////-----------------------------------------
  if (_source.previous_info) {
    let item = _source.previous_info;
    node = {
      background:"bcolor1",
      isOpen: false,
      id: item.section_mom_id,
      title:
        `<a class="label #bcolor# " href="#">قانون مقدم : </a>` +
        item.qanon_title +
        " - " +
        item.full_path,
      tag: item.state_etebar,
      content: _source.content1,
      link_label: "«منبع»",
      link_url: base_url
        .replace("{{qanon_id}}", item.qanon_id)
        .replace("{{section_id}}", item.section_mom_id),
      children: [],
    };

    if (item.group_section_ids?.length > 1)
      node["children"].push({
        isOpen: false,
        id: item.context_id,
        title: "متن کامل با اجزاء دیگر مورد توجه : ",
        content: _source.context1,
      });

    accordionItems.value.push(node);

    let unrepeat_id = {}
    item.rule_ids.forEach((rule_id) => {
      if(unrepeat_id[rule_id])
        return
      unrepeat_id[rule_id] = 1
      let content = _source.rules_content[rule_id] ?? "";
      accordionItems.value.push({
        isOpen: false,
        id: rule_id,
        title: "حکم مورد استناد طرف مقدم  با شناسه : " + rule_id,
        content: content,
      });
    });
  }

  /////-----------------------------------------
  if (_source.next_info) {
    let item = _source.next_info;
    node = {
      background:"bcolor2",
      isOpen: false,
      id: item.section_mom_id,
      title:
        `<a class="label #bcolor# " href="#">قانون موخر : </a>` +
        item.qanon_title +
        " - " +
        item.full_path,
      tag: item.state_etebar,
      content: _source.content2,
      link_label: "«منبع»",
      link_url: base_url
        .replace("{{qanon_id}}", item.qanon_id)
        .replace("{{section_id}}", item.section_mom_id),
      children: [],
    };

    if (item.group_section_ids?.length > 1)
      node["children"].push({
        isOpen: false,
        id: item.context_id,
        title: "متن کامل با اجزاء دیگر مورد توجه : ",
        content: _source.context1,
      });

    accordionItems.value.push(node);

    let unrepeat_id = {}
    item.rule_ids.forEach((rule_id) => {
      if(unrepeat_id[rule_id])
        return
      unrepeat_id[rule_id] = 1

      let content = _source.rules_content[rule_id] ?? "";
      accordionItems.value.push({
        isOpen: false,
        id: rule_id,
        title: "حکم مورد استناد طرف موخر  با شناسه : " + rule_id,
        content: content,
      });
    });
  }

  /////-------------وحدت موضوع----------------------
  node = {
    background:"bcolor3",
    isOpen: true,
    id: 11,
    title: "توضیح هوشمند برای وجود وحدت موضوع بین احکام استنادی ",
    content: _source.subject_unity.reason,
    children: [],
  };

  if (_source.subject_unity.description) {
    node["children"].push({
      isOpen: false,
      id: _source.context_id,
      title: "شرط اعتبار وحدت موضوع : ",
      content: _source.subject_unity.description,
    });
  }

  accordionItems.value.push(node);
  /////-------------توضیح تخالف----------------------
  node = {
    isOpen: true,
    id: 11,
    title: "توضیح وجود تخالف بین دو ماده  ",
    content: _source.conflict_detection.description,
    children: [],
  };

  accordionItems.value.push(node);
  /////-------------بیان نوع تخالف----------------------
  node = {
    isOpen: true,
    id: 11,
    title:
      "بیان نوع تخالف بین دو ماده  : " +
      _source.conflict_relation_identification.main_type,
    content: _source.conflict_relation_identification.description,
    children: [],
  };

  accordionItems.value.push(node);
  /////-----------------------------------

  console.log("accordionItems", accordionItems.value);
};

// #region Lifecycle
onMounted(() => {
  setDataEditor();
});
// #endregion
</script>

<style></style>
