<script lang="ts" setup>
import { closeToast, showLoadingToast, showToast } from 'vant'
import { AiAnswerProvideKey } from '@/components/AiAnswer/type'
import { fetchGetFeedbackConfig, fetchSubmitFeedback } from '@/api/answer'

interface Props {
  recordId: string
}

const props = defineProps<Props>()

const emits = defineEmits(['onAddUnLike', 'onDelUnLike'])
const isUnLike = ref(false)
const isShowPopup = ref(false)

const unLikeFlag = inject(AiAnswerProvideKey)?.unLikeFlag

const formData = ref<any>({})

async function onAddUnLike() {
  const res = await fetchGetFeedbackConfig()
  if (res.data) {
    const objData = JSON.parse(res.data.content)
    formData.value = objData
  }
  isShowPopup.value = true
}

function onClickOptionItem(item: any) {
  item.answerValue = item.answerValue === '1' ? '0' : '1'
}

function toggleFalse() {
  isUnLike.value = false
}

const disabledBtn = computed(() => formData.value.questionList[0].answerList.every((item: any) => item.answerValue === '0') && formData.value.questionList[1].answerValue.trim() === '')

async function onSubmit() {
  if (disabledBtn.value)
    return

  const params = {
    recordId: props.recordId,
    feedbackStatus: '02',
    content: JSON.stringify(formData.value),
  }
  showLoadingToast({
    message: '提交中...',
    duration: 0,
  })

  const res = await fetchSubmitFeedback(params)
  if (res) {
    closeToast()
    showToast('感谢您的反馈')
    isShowPopup.value = false
    isUnLike.value = true
    isShowPopup.value = false
    emits('onAddUnLike')
  }
}

async function onDelUnLike() {
  const res = await fetchSubmitFeedback({
    recordId: props.recordId,
    feedbackStatus: '00',
    content: '',
  })
  if (res) {
    isUnLike.value = false
    emits('onDelUnLike')
    showToast('已取消反馈')
  }
}

watch(() => unLikeFlag?.value, (val) => {
  isUnLike.value = val as boolean
}, {
  immediate: true,
  deep: true,
})

defineExpose({
  toggleFalse,
})
</script>

<template>
  <div class="unlike-btn-wrap">
    <SvgIcon v-if="!isUnLike" size="22px" name="unlikeG" @click="onAddUnLike" />
    <SvgIcon v-else size="22px" name="unlikeL" @click="onDelUnLike" />

    <van-popup
      v-model:show="isShowPopup"
      teleport="body"
      style="height: 500px;"
      round
      position="bottom"
    >
      <div class="model-desc-pop-inner">
        <img class="close" src="@/assets/images/close-b.png" alt="" @click="isShowPopup = false">
        <div class="title">
          {{ formData.title }}
        </div>
        <div class="content">
          <div v-if="formData?.questionList[0].questionCode === '1001'" class="options-wrap">
            <div v-for="item in formData.questionList[0].answerList" :key="item.answerCode" class="options-item" :class="{ active: item.answerValue === '1' }" @click="onClickOptionItem(item)">
              {{ item.answerName }}
            </div>
          </div>
          <div v-if="formData?.questionList[1].questionCode === '1099'" class="ipt-wrap">
            <SvgIcon style="transform: translateY(2px);" size="18px" name="edit" />
            <van-field
              v-model="formData.questionList[1].answerValue"
              style="background-color: #f6f7f9; padding: 0; margin-left: 8px;"
              rows="2"
              type="textarea"
              autosize
              :maxlength="100"
              placeholder="留下更多反馈"
            />
          </div>
        </div>

        <div class="footer-wrap">
          <div class="submit-btn" :class="{ disabled: disabledBtn }" @click="onSubmit">
            提交
          </div>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<style lang="scss" scoped>
.footer-wrap {
  width: 100%;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  .submit-btn {
    &.disabled {
      opacity: 0.5;
    }
    width: 280px;
    height: 44px;
    background: #29b6fd;
    border-radius: 22px;
    font-family:
      PingFangSC,
      PingFang SC;
    font-weight: 500;
    font-size: 15px;
    color: #ffffff;
    line-height: 22px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.content {
  flex: 1;
  box-sizing: border-box;
  padding: 0 20px;
  overflow-x: hidden;
  overflow-y: scroll;
  margin-top: 8px;
  padding-bottom: 24px;
}
.ipt-wrap {
  width: 335px;
  background: #f6f7f9;
  border-radius: 4px;
  display: flex;
  font-size: 14px;
  box-sizing: border-box;
  padding: 14px;
  margin-top: 24px;
}
.options-wrap {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;

  margin-top: 8px;
  .options-item {
    width: 164px;
    height: 32px;
    background: #f6f7f9;
    border-radius: 18px;
    font-family: AlibabaPuHuiTi_2_55_Regular;
    font-size: 14px;
    color: #586462;
    line-height: 18px;
    text-align: center;
    font-style: normal;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 16px;
    box-sizing: border-box;
    &.active {
      color: #29b6fd;
      background: #f6fcff;
      border-radius: 18px;
      border: 1px solid rgba(41, 182, 253, 0.3);
    }
  }
}
.model-desc-pop-inner {
  width: 375px;
  height: 100%;
  background-size: 375px 148px;
  background-repeat: no-repeat;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-sizing: border-box;
  padding-top: 48px;
  position: relative;
  .close {
    position: absolute;
    width: 16px;
    height: 16px;
    top: 16px;
    right: 16px;
  }
  .title {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-family:
      PingFangSC,
      PingFang SC;
    font-weight: 500;
    font-size: 16px;
    color: #333333;
    line-height: 22px;
    text-align: center;
    font-style: normal;
    position: relative;
  }
}
</style>
