<script setup lang="ts">
import { onMounted, ref } from "vue";
import { useQuasar } from "quasar";

import { useCounterMixin } from "@/stores/mixin";
import http from "@/plugins/http";
import config from "@/app.config";

import type { Permission } from "@/components/Workflow/interface/index/Main";
import type { DataListState } from "@/components/Workflow/interface/response/Main";

import DialogSelectPerson from "@/components/Workflow/DialogSelectPerson.vue";

const $q = useQuasar();
const { dialogConfirm, showLoader, hideLoader, messageError } =
  useCounterMixin();

const { id, sysName } = defineProps({
  id: { type: String, require: true },
  sysName: { type: String, require: true },
});

const stateId = ref<string>(""); //id state ปัจุบัน
const state = ref<number>(1); //state ปัจุบัน
const isPermission = ref<boolean>(true); //การเข้าถึง Workflow

const permission = ref<Permission>({
  isChangeState: false, //ส่งไปยังผู้บังคับบัญชา/ผู้มีอำนาจ
  isOperate: false, //เปลี่ยนสถานะ (state) เอกสารได้
  isView: false, //ดูเอกสารได้
  isUpdate: false, //แก้ไขเอกสารได้
  isDelete: false, //ลบเอกสารได้ (ถ้ามี)
  isCancel: false, //ลบเอกสารได้ (ถ้ามี)
});

const modalSelectPerson = ref<boolean>(false); //เลือกรายชื่อ ส่งไปผู้บังคับบัญชา/ผู้มีอำนาจ
const itemState = ref<DataListState[]>([]);

/** ฟังก์ชันเรียกข้อมูล Workflow ที่อยู่ปัจุบัน*/
async function fetchCheckState() {
  await http
    .post(config.API.workflow + `check-user-now`, {
      refId: id,
      system: sysName,
    })
    .then(async (res) => {
      await fetchData();
      const data = await res.data.result;
      stateId.value = data.stateId;
      state.value =
        data.stateNo === itemState.value.length
          ? data.stateNo + 1
          : data.stateNo;
      permission.value = {
        isChangeState: data.can_change_state,
        isOperate: data.can_operate,
        isView: data.can_view,
        isUpdate: data.can_update,
        isDelete: data.can_delete,
        isCancel: data.can_cancel,
      };
    })
    .catch(() => {
      isPermission.value = false;
    });
}

/** ฟังก์ชันเรียกข้อมูล Workflow ทั้งหมด*/
async function fetchData() {
  await http
    .post(config.API.workflow + `check-state-all`, {
      refId: id,
      system: sysName,
    })
    .then(async (res) => {
      const data = await res.data.result;
      itemState.value = data;
    })
    .catch((err) => {
      messageError($q, err);
    });
}

/** ฟังก์ชันยืนยันการ NextStep*/
function onChangeState() {
  dialogConfirm($q, async () => {
    showLoader();
    await http
      .post(config.API.workflow + `state-next`, {
        refId: id,
        system: sysName,
      })
      .then(async () => {
        await fetchCheckState();
      })
      .catch((err) => {
        messageError($q, err);
      })
      .finally(() => {
        hideLoader();
      });
  });
}

onMounted(async () => {
  await fetchCheckState();
});

defineExpose({
  permission,
});
</script>

<template>
  <q-card bordered class="row col-12" v-if="isPermission">
    <div class="bg-grey-1 q-pa-sm col-12 row items-center">
      <div class="q-pl-sm text-weight-bold text-dark">Workflow</div>
    </div>
    <div class="col-12"><q-separator /></div>
    <q-card-section>
      <div class="q-px-lg q-py-md">
        <q-timeline color="primary">
          <q-timeline-entry
            v-for="(step, index) in itemState"
            :key="index"
            :title="step.stateName"
            :icon="
              state === index + 1
                ? 'mdi-pencil'
                : state > index + 1
                ? 'done'
                : `mdi-numeric-${index + 1}`
            "
            :color="state < index + 1 ? 'grey-4' : ''"
          >
            <!-- Darft -->
            <div class="row q-col-gutter-sm" v-if="state === 1 && index === 0">
              <div>
                <q-btn
                  v-if="permission.isChangeState"
                  @click.prevent="onChangeState"
                  label="Next Step"
                  color="primary"
                />
              </div>
            </div>

            <!-- Operate -->
            <div
              class="q-col-gutter-sm"
              v-if="index > 0 && index < itemState.length - 1 && state > index"
            >
              <div v-if="step.stateUserComments.length !== 0">
                <q-list bordered separator style="min-width: 20vw">
                  <q-item
                    v-for="(item, index) in step.stateUserComments"
                    :key="index"
                  >
                    <q-item-section>
                      <q-item-label
                        >{{
                          `${item.prefix}${item.firstName} ${item.lastName}`
                        }}
                      </q-item-label>
                      <q-item-label caption lines="2">{{
                        item.isReasonSetting ? item.reason : ""
                      }}</q-item-label>
                    </q-item-section>

                    <q-item-section side top>
                      <q-item-label
                        :class="!item.isApprove ? 'text-red' : 'text-green'"
                      >
                        {{
                          item.isAcceptSetting
                            ? item.isAccept
                              ? "รับทราบ"
                              : ""
                            : item.isApproveSetting
                            ? item.isApprove === true
                              ? "อนุมัติ"
                              : item.isApprove === false
                              ? "ไม่อนุมัติ"
                              : ""
                            : ""
                        }}
                      </q-item-label>
                    </q-item-section>
                  </q-item>
                </q-list>
              </div>

              <div v-if="permission.isOperate && state === index + 1">
                <q-btn
                  @click.prevent="modalSelectPerson = true"
                  label="ส่งไปผู้บังคับบัญชา/ผู้มีอำนาจ"
                  color="public"
                />
              </div>

              <div v-if="permission.isChangeState && state === index + 1">
                <q-btn
                  @click.prevent="onChangeState"
                  label="Next Step"
                  color="primary"
                />
              </div>
            </div>
          </q-timeline-entry>
        </q-timeline>
      </div>
    </q-card-section>
  </q-card>

  <DialogSelectPerson
    v-model:modal="modalSelectPerson"
    :state-id="stateId"
    :fetch-data="fetchCheckState"
  />
</template>
