<script lang="ts" setup>
import type { iEstimate, iPageable, iPrcs, iPrcsPageable, iPbAtt, iEstimateList } from '/@src/utils/types'
import dateFormat from "/@src/utils/dateFormat.ts";

const notyf = useNotyf()
const router = useRouter()

const page = ref(1)
const total = ref(1)
const length = ref(0)
const select = ref<iPrcs>({})
const saveModal = ref(false)
const viewModal = ref(false)
const estimates = ref<iEstimate[]>([])
const pbAtts = ref<iPbAtt[]>([])

const params = ref({
  cateCd: '',
  title: '',
  regNm: '',
  regSdt: '',
  regEdt: '',
  page: 1,
  row: 10,
})

const prcsList = ref<iPrcsPageable>()

onMounted(() => {
  api.getPrcs(params.value).then((res: iPrcsPageable) => {
    console.log(res)
    prcsList.value = res
    page.value = prcsList.value.totlaPages || 0
    total.value = prcsList.value.totalElements || 0
    length.value = prcsList.value.content.length || 0
    params.value.page = 1
  })
})

function onTrClick(data: iPrcs) {
  select.value = data
  if(isSave(select.value)) {
    saveModal.value = true
  } else {
    viewModal.value = true
  }

}

const isSave = (prcs: iPrcs): boolean => {
  const nowDt = dateFormat.now()
  const now = new Date(nowDt).getTime()
  const sdt = new Date(prcs.regSdat).getTime()
  const edt = new Date(prcs.regEdat).getTime()
  return (sdt <= now && edt >= now)
}

function onSearch(data: any) {
  // console.log(data)
  params.value = structuredClone(data)
}

const onEstimateSave = () => {
  console.log('Form submitted!')
  /* kcb 인증후 추가 */
  notyf.dismissAll()
  api.saveEstimate(<iEstimateList>{ estimates: estimates.value, pbAtts: pbAtts.value }).then(() => {
    notyf.primary('저장 되었습니다.')
    router.push('/navbar/layouts/est-list')
  }).catch((res) => {
    notyf.error(res.response._data.body)
  })
}

const setData = (data: iEstimate[]) => {
  console.log(data)
  estimates.value = data
}
const setAttData = (data: iPbAtt[]) => {
  pbAtts.value = data
}

watch(params, (newValue) => {
  api.getPrcs(newValue).then((res: iPageable) => {
    prcsList.value = res
    page.value = prcsList.value.totlaPages
    total.value = prcsList.value.totalElements
    length.value = prcsList.value.content.length
    params.value.page = 1
  }).catch((res) => {
    notyf.error(res.response._data.body)
  })
})

</script>

<template>
  <div>
    <ContractList
      v-if="prcsList !== undefined"
      :length="length"
      :page="page"
      :row="params.row"
      :prcs-list="prcsList"
      :total="total"
      @on-search="onSearch"
      @on-tr-click="onTrClick"
    />
    <VModal
      is="form"
      novalidate
      :open="saveModal"
      title="견적 등록"
      actions="center"
      cancel-label="취소"
      size="full"
      @close="saveModal = false"
    >
      <template #content>
        <ContractSave
          :prcs="select"
          @setData="setData"
          @setAttData="setAttData"
        />
      </template>
      <template #action>
        <VButton
          color="primary"
          raised
          @click="onEstimateSave"
        >
          등록
        </VButton>
      </template>
    </VModal>
    <VModal
      is="form"
      novalidate
      :open="viewModal"
      title="견적 확인"
      actions="center"
      cancel-label="취소"
      size="full"
      @close="viewModal = false"
    >
      <template #content>
        <ContractView
          :prcs="select"
          @setData="setData"
          @setAttData="setAttData"
        />
      </template>
    </VModal>
  </div>
</template>

<style lang="scss">
.text-right {
  text-align:right !important;
}
.text-center {
  text-align:center !important;
}

.modal-card-body {
  height: 50vh;
}
</style>
