<template>
  <div class="mtp-footer-main">
    <component :is="currentComponent.component" :parent-component="currentComponent" v-if="currentComponent"></component>
    <mtp-tabbar class="mtp-tabbar--bottom" :data-source="components" label="title" @change="componentChange">
      <!-- 议价单 -->
      <div class="goods-apply" v-if="showBargainRadio">
        <a-radio-group class="conditionCommonRadioGroup" v-model:value="bargainValue">
          <a-radio @focus="changeBargain(ApplyType.my)" :value="ApplyType.my">我的申请</a-radio>
          <a-radio @focus="changeBargain(ApplyType.counterpart)" :value="ApplyType.counterpart">对方申请</a-radio>
        </a-radio-group>
      </div>
    </mtp-tabbar>
  </div>
</template>

<script lang="ts">
import { defineComponent, nextTick, PropType, ref, computed, onBeforeUnmount } from 'vue'
import { useDynamicComponent } from '@/@next/hooks/common'
import { DynamicComponent } from '@/@next/hooks/common/interface'
import { ApplyType } from '@/common/constants/enumCommon';
import MtpTabbar from '@/@next/components/base/tabbar/index.vue'
import Bus from '@/utils/eventBus/index'

export default defineComponent({
  props: {
    parentComponent: {
      type: Object as PropType<DynamicComponent>,
      required: true
    }
  },
  components: {
    MtpTabbar,
  },
  setup(props) {
    const { components, currentComponent, componentChange } = useDynamicComponent(props.parentComponent.code);
    const bargainValue = ref<ApplyType>(ApplyType.my);

    const showBargainRadio = computed(() => {
      if (currentComponent.value) {
        return ['bottom_swap_commodity_contract_bargain', 'bottom_commodity_contract_bargain'].includes(currentComponent.value.code)
      }
      return false;
    })

    // 组件重载
    function componentReload() {
      const tmpComponent = currentComponent.value;
      currentComponent.value = undefined;
      nextTick(() => {
        currentComponent.value = tmpComponent;
      })
    }

    // 切换 我的
    function changeBargain(value: ApplyType) {
      Bus.$emit('bargain', value);
    }

    // 注册账户变化事件，待通知调用
    const stopNotify = Bus.$on('taAccountChangedNtf', () => {
      componentReload();
    })

    onBeforeUnmount(() => {
      stopNotify();
    })

    return {
      components,
      currentComponent,
      componentChange,
      changeBargain,
      ApplyType,
      bargainValue,
      showBargainRadio,
    }
  }
})
</script>

<style lang="less">
@import './index.less';
</style>