<template>
  <div>
    <div class="configure_page">
      <div class="delete_btn">
        <el-button type="danger" @click="emit('delete')">
          <Icon icon="ep:delete" />
          {{t('mp.deleteCurrentMenu')}}
        </el-button>
      </div>
      <div>
        <span>{{t('mp.menuName')}}：</span>
        <el-input
          class="input_width"
          v-model="menu.name"
          :placeholder="t('mp.pleaseEnterMenuName')"
          :maxlength="isParent ? 4 : 7"
          clearable
        />
      </div>
      <div v-if="isLeave">
        <div class="menu_content">
          <span>{{t('mp.menuIdentifier')}}：</span>
          <el-input
            class="input_width"
            v-model="menu.menuKey"
            :placeholder="t('mp.pleaseEnterMenuKey')"
            clearable
          />
        </div>
        <div class="menu_content">
          <span>{{t('mp.menuContent')}}：</span>
          <el-select v-model="menu.type" clearable :placeholder="t('common.selectText')" class="menu_option">
            <el-option
              v-for="item in menuOptions"
              :label="item.label"
              :value="item.value"
              :key="item.value"
            />
          </el-select>
        </div>
        <div class="configur_content" v-if="menu.type === 'view'">
          <span>{{t('mp.jumpLink')}}：</span>
          <el-input class="input_width" v-model="menu.url" :placeholder="t('mp.pleaseEnterLink')" clearable />
        </div>
        <div class="configur_content" v-if="menu.type === 'miniprogram'">
          <div class="applet">
            <span>{{t('mp.miniProgramAppId')}} ：</span>
            <el-input
              class="input_width"
              v-model="menu.miniProgramAppId"
              :placeholder="t('mp.pleaseEnterMiniProgramAppId')"
              clearable
            />
          </div>
          <div class="applet">
            <span>{{t('mp.miniProgramPagePath')}}：</span>
            <el-input
              class="input_width"
              v-model="menu.miniProgramPagePath"
              :placeholder="t('mp.pleaseEnterMiniProgramPagePathEgPagesIndex')"
              clearable
            />
          </div>
          <div class="applet">
            <span>{{t('mp.miniProgramBackupWebpage')}}：</span>
            <el-input
              class="input_width"
              v-model="menu.url"
              :placeholder="t('mp.OlderVersionsOfTheClientThatDoNotSupportMiniProgramsWillOpenThisWebpage')"
              clearable
            />
          </div>
          <p class="blue">{{t('mp.tipsYouNeedToAssociateWithTheOfficialAccountToBindTheMiniProgramToTheWeChatMenu')}}</p>
        </div>
        <div class="configur_content" v-if="menu.type === 'article_view_limited'">
          <el-row>
            <div class="select-item" v-if="menu && menu.replyArticles">
              <WxNews :articles="menu.replyArticles" />
              <el-row class="ope-row" justify="center" align="middle">
                <el-button type="danger" circle @click="deleteMaterial">
                  <icon icon="ep:delete" />
                </el-button>
              </el-row>
            </div>
            <div v-else>
              <el-row justify="center">
                <el-col :span="24" style="text-align: center">
                  <el-button type="success" @click="showNewsDialog = true">
                    {{t('mp.selectFromMaterialLibrary')}}
                    <Icon icon="ep:circle-check" />
                  </el-button>
                </el-col>
              </el-row>
            </div>
            <el-dialog :title="t('mp.selectGraphic')" v-model="showNewsDialog" width="80%" destroy-on-close>
              <WxMaterialSelect
                type="news"
                :account-id="props.accountId"
                @select-material="selectMaterial"
              />
            </el-dialog>
          </el-row>
        </div>
        <div
          class="configur_content"
          v-if="menu.type === 'click' || menu.type === 'scancode_waitmsg'"
        >
          <WxReplySelect v-if="hackResetWxReplySelect" v-model="menu.reply" />
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import WxReplySelect from '@/views/mp/components/wx-reply'
import WxNews from '@/views/mp/components/wx-news'
import WxMaterialSelect from '@/views/mp/components/wx-material-select'
import menuOptions from './menuOptions'
const { t } = useI18n()

const message = useMessage()

const props = defineProps<{
  accountId: number
  modelValue: any
  isParent: boolean
}>()

const emit = defineEmits<{
  (e: 'delete', v: void)
  (e: 'update:modelValue', v: any)
}>()

const menu = computed({
  get() {
    return props.modelValue
  },
  set(val) {
    emit('update:modelValue', val)
  }
})
const showNewsDialog = ref(false)
const hackResetWxReplySelect = ref(false)
const isLeave = computed<boolean>(() => !(menu.value.children?.length > 0))

watch(menu, () => {
  hackResetWxReplySelect.value = false // 销毁组件
  nextTick(() => {
    hackResetWxReplySelect.value = true // 重建组件
  })
})

// ======================== 菜单编辑（素材选择） ========================
const selectMaterial = (item: any) => {
  const articleId = item.articleId
  const articles = item.content.newsItem
  // 提示，针对多图文
  if (articles.length > 1) {
    message.alertWarning(t('mp.YouHaveSelectedAMultiGraphicItWillDefaultToJumpToTheFirstOne'))
  }
  showNewsDialog.value = false

  // 设置菜单的回复
  menu.value.articleId = articleId
  menu.value.replyArticles = []
  articles.forEach((article) => {
    menu.value.replyArticles.push({
      title: article.title,
      description: article.digest,
      picUrl: article.picUrl,
      url: article.url
    })
  })
}

const deleteMaterial = () => {
  delete menu.value['articleId']
  delete menu.value['replyArticles']
}
</script>

<style lang="scss" scoped>
.el-input {
  width: 70%;
  margin-right: 2%;
}

.configure_page {
  .delete_btn {
    margin-bottom: 15px;
    text-align: right;
  }

  .menu_content {
    margin-top: 20px;
  }

  .configur_content {
    padding: 20px 10px;
    margin-top: 20px;
    background-color: #fff;
    border-radius: 5px;

    .select-item {
      width: 280px;
      padding: 10px;
      margin: 0 auto 10px;
      border: 1px solid #eaeaea;

      .ope-row {
        padding-top: 10px;
        text-align: center;
      }
    }
  }

  .blue {
    margin-top: 10px;
    color: #29b6f6;
  }

  .applet {
    margin-bottom: 20px;

    span {
      width: 20%;
    }
  }

  .input_width {
    width: 40%;
  }

  .material {
    .input_width {
      width: 30%;
    }

    .el-textarea {
      width: 80%;
    }
  }
}
</style>
