<template>
  <div class="gyq_container">
    <div class="gyq_content">
      <HeaderCard name="全民股东" intro="用户成为股东，可促进消费" icon="xffx" showSwitch v-model:isOpen="form.isEnable">
      </HeaderCard>
      <div class="tips">
        <el-alert title="请记得前往《分销明细》充值余额，余额不足时用户收益将无法正常入账" type="primary" show-icon :closable="false" />
      </div>
      <div class="row mt14">
        <tabHeader v-model="tabActiveIndex" :list="tabList" />
      </div>
      <div class="row mt14">
        <!-- 基础设置 -->
        <setting key="setting" v-if="tabActiveIndex == 0" />
        <!-- 分销员列表 -->
        <distributor key="distributor" v-if="tabActiveIndex == 1" />
        <!-- 开通记录 -->
        <activation_record key="activationRecord" v-if="tabActiveIndex == 2" />
        <!-- 分销明细 -->
        <distribution_details key="distributionDetails" v-if="tabActiveIndex == 3" />
        <!-- 股东管理群 -->
        <groupSetting name="groupSetting" key="groupSetting" v-if="tabActiveIndex == 4" />
      </div>
    </div>
  </div>
</template>

<script setup>
import HeaderCard from "../components/headerCard.vue";
import tabHeader from "../components/tabHeader.vue";
import setting from "./components/setting.vue";
import distributor from "./components/distributor.vue";
import activation_record from "./components/activation_record.vue";
import distribution_details from "./components/distribution_details.vue";
import groupSetting from "./components/groupSetting.vue";
import { ref, onMounted, watch } from 'vue'
import { distributionGet, distributionPut } from '@/api/coupon'

const shopInfo = ref(JSON.parse(localStorage.getItem('userInfo')))

const tabActiveIndex = ref(0)
const tabList = ref([
  {
    label: '基础设置',
    value: 0
  },
  {
    label: '分销员列表',
    value: 1
  },
  {
    label: '开通记录',
    value: 2
  },
  {
    label: '分销明细',
    value: 3
  },
  {
    label: '股东管理群',
    value: 3
  },
])

const form = ref({
  isEnable: 1
})


watch(
  () => form.value.isEnable,
  (newVal) => {
    if (!loading.value) {
      distributionPutAjax();
    }
  }
);

// 更新开关状态
async function distributionPutAjax() {
  try {
    await distributionPut(form.value)
  } catch (error) {
    console.log(error);
  }
}

// 获取开关数据
const loading = ref(true)
async function distributionGetAjax() {
  try {
    const res = await distributionGet()
    form.value.isEnable = res.isEnable
  } catch (error) {
    console.log(error);
  }
  setTimeout(() => {
    loading.value = false
  }, 300);
}

onMounted(() => {
  distributionGetAjax()
})

</script>

<style scoped lang="scss">
.gyq_container {
  padding: 14px;

  .gyq_content {
    padding: 14px;
    background-color: #fff;
    border-radius: 8px;
  }
}

.row {
  &.mt14 {
    margin-top: 14px;
  }
}

.tips {
  margin-top: 14px;
}
</style>