<template>
  <page-header-wrapper>
    <a-card v-show=" vdata.tableType == 'table' " class="table-card">
      <div class="div-top">
        <div class="div-top-title">解决方案</div>
        <div class="div-top-line"></div>
        <div  class="div-top-status">
          <div class="div-top-status-title">产品分类</div>
          <div class="div-top-status-title-list">
            <span   :class="[vdata.tab_index == -1?'div-top-status-title-span  color-blue':'div-top-status-title-span']"  @click="getTab(-1,0)">全部</span>
            <span  v-for="(item, index) in vdata.tabList" :key="index" :class="[vdata.tab_index == index?'div-top-status-title-span  color-blue':'div-top-status-title-span']" @click="getTab(index,0)">{{item.typeName}}</span>
          </div>
        </div>
<!--        <div  class="div-top-status">-->
<!--          <div class="div-top-status-title">产品状态</div>-->
<!--          <div class="div-top-status-title-list">-->
<!--            <span   :class="[vdata.state_index == -1?'div-top-status-title-span  color-blue':'div-top-status-title-span']"  @click="getTab(-1,1)">全部</span>-->
<!--            <span  v-for="(item, index) in vdata.stateList" :key="index" :class="[vdata.state_index == index?'div-top-status-title-span  color-blue':'div-top-status-title-span']" @click="getTab(index,1)">{{item}}</span>-->
<!--          </div>-->
<!--        </div>-->
      </div>

    </a-card>

    <a-card v-show="vdata.tableType == 'table' " class="table-card table-card-bottom" v-for="(item, index) in vdata.filteredPackageList" :key="index"  >

      <div class="product">
        <div class="product-title">{{ item.typeName }}</div>
        <div class="div-top-line"></div>
        <div  class="product-list">
          <div class="product-list-li"  v-for="(value, key) in item.children" :key="key" @click="editFunc(value.packageId)" >
            <div class="product-list-li-title" >
              <span class="product-list-li-span1">{{value.name}}</span>
              <a-badge v-if="value.state == 0 || value.state == 4" status="default" text="未开通" />
              <a-badge v-if="value.state == 1" status="warning" text="审核中" />
              <a-badge v-if="value.state == 3" status="success" text="已开通" />
              <a-badge v-if="value.state == 2" status="error" text="驳回待修改" />

            </div>
            <div  class="product-list-li-desc">
              <span class="product-list-li-span3">{{value.packageDesc}}</span>
            </div>
          </div>
        </div>
      </div>
    </a-card>


    <a-card v-show=" vdata.tableType == 'detail' " style="background: #f0f2f5; ">

      <SybMarketsDeatil ref="nextPackageDeatilRef" :configMode="props.configMode" @itemRender="itemRenderSave" />
    </a-card>

    <a-card v-show=" vdata.tableType == 'sign' " style="background: #f0f2f5; ">

      <SybMarketsSign ref="nextPackageSignRef" :configMode="props.configMode" @itemRender="itemRenderSave"/>
    </a-card>

    <a-card v-show=" vdata.tableType == 'productDetail' "   style="background: #f0f2f5; ">

      <SybProductDeatil ref="nextProductDeatilRef" :configMode="props.configMode" @itemRender="itemRenderSave" />
    </a-card>
  </page-header-wrapper>
</template>

<script setup lang="ts">
import { API_URL_PACKAGE_LIST,API_URL_PACKAGE_TYPE_LIST, req ,API_URL_PRODUCT_START_LIST} from '@/api/manage'
import {ref, onMounted, reactive, getCurrentInstance,nextTick} from 'vue'
import MchAppAddOrEdit from './Detail.vue'
import { useRouter, useRoute } from 'vue-router'
import { dataTool } from 'echarts'

const router = useRouter() //这是全部路由
const { $infoBox, $access } = getCurrentInstance()!.appContext.config.globalProperties
const nextPackageDeatilRef = ref();
const nextPackageSignRef = ref();
const nextProductDeatilRef = ref();
const props = defineProps({
  configMode: { type: String, default: '' },   // 模式
})
const vdata =  reactive({
  tableType:'table',
  packageList: [] as any, // 产品列表
  filteredPackageList: [] as any, // 产品列表
  filteredTabList: [] as any, // 分类列表
  tab_index:-1,
  tabList: [] as any, // 分类列表
  state_index:-1,
  stateList: ['未开通','审核中','审核驳回','已开通','已关联应用'] as any, // 状态
})
req.list(API_URL_PACKAGE_TYPE_LIST, { pageNumber: 1, pageSize: 100 }).then(res => {
  vdata.tabList  = res
  vdata.filteredTabList = res

  getPackageList()

})


async function getPackageList(){
  var data = {
    pageNumber:1,
    pageSize:100,
    packageType:'' as any,
    state:'' as any,
  }
  if(vdata.tab_index !== -1){
    data.packageType = vdata.tabList[vdata.tab_index].typeId
    vdata.filteredPackageList = [vdata.tabList[vdata.tab_index]];
  }else{
    vdata.filteredPackageList = vdata.tabList;
  }
  if(vdata.state_index !== -1){
    data.state = vdata.state_index
  }
  let result = await req.list(API_URL_PACKAGE_LIST, data);
  var list  = result.records
  vdata.packageList = list
  for (let i=0;i<vdata.filteredPackageList.length;i++) {
    vdata.filteredPackageList[i]['children'] = [];
    for (let j=0;j<list.length;j++){
      if (list[j].packageType == vdata.filteredPackageList[i].typeId) {
        if (!vdata.filteredPackageList[i]['children']) {
          vdata.filteredPackageList[i]['children'] = [];
        } else {
          vdata.filteredPackageList[i]['children'].push(list[j]);

          vdata.filteredPackageList[i]['children'].sort(function (a, b) {
            return b.packageSort - a.packageSort
          })
        }
      }
    }
  }
}

//  console.log(result.records,'listlist')
function getTab(index,type){
  if(type == 0){
    vdata.tab_index = index
    getPackageList()
  }else{
    vdata.state_index = index
    getPackageList()
  }
}


function editFunc (recordId) { // 业务通用【修改】 函数
                               // router.push({ name: 'productDetail', path: '@/views/package/productDetail.vue' })
  vdata.tableType = "detail"
  nextPackageDeatilRef.value.show(recordId)
  // router.push({
  //   path:'/package/productDeatil',
  //   query:{productId:recordId}
  // })
}
function  itemRenderSave(e){
  if(e.type == 1){
    vdata.tableType = e.table
    nextPackageSignRef.value.show(e.packageId)
  }
  if(e.type == 2){
    vdata.tableType = e.table
    nextPackageSignRef.value.show(e.packageId)
  }

  if(e.type == 6){
    vdata.tableType = e.table
    nextPackageDeatilRef.value.show(e.packageId)
    return false;
  }
  if(e.type == 7){
    vdata.tableType = e.table
    nextProductDeatilRef.value.show(e.packageId)
    return false;
  }

  if(e.type == 3){
    req.add(API_URL_PRODUCT_START_LIST,e).then(data => {
      $infoBox.message.success(data)
      setTimeout(() => {
        router.push({
          path:'/productLog'
        })
      }, 1000)
      // vdata.tableType = e.table
      // getPackageList()
    })
  }
  if(e.type == 4){
    // window.location.href=  "/apps"
    router.push({
      path:'/apps'
    })
  }
}
</script>

<style scoped>
.div-top{
  background: #ffffff;
  padding: 20px 20px;
  border-radius: 10px;
}
.div-top-title{
  font-size: 17px;
  font-weight: 600;
  position: relative;
  margin-bottom: 10px;
}
.div-top-des{
  color: #2C6EF6;
  margin-top: 15px;
  margin-bottom: 30px;
}
.div-top-line{
  border-bottom: 1px solid #EDEDED;
}
.div-top-status{
  display: flex;
  margin: 18px 0;
  font-size: 13px;
}
.div-top-status-title{
  padding-right:10px;
  color:#767676;
}
.div-top-status-title-span{
  padding: 0 10px;
  font-weight: 600;
}
.div-top-active{
  color: #2C6EF6;
}



/*列表 */
.product{
  background: #ffffff;
  padding: 0 20px;
  border-radius: 10px;
  margin-top: 20px;;
}
.product-title{
  font-size: 17px;
  font-weight: 600;
  position: relative;
  margin-bottom: 10px;
}
.product-list{
  width: 100%;
  margin: 10px 0;
  display: flex;
  flex-wrap: wrap;
}
.product-list-li{
  background: #F9FAFA;
  width: 30%;
  padding: 20px;
  border-radius:10px;
  margin: 10px;
}
.product-list-li-span1{
  font-size: 16px;
  font-weight: 600;
  padding-right: 10px;
}
.product-list-li-span2{
  font-size: 13px;
  color: #2C6EF6;
  padding-left: 15px;
}
.product-list-li-desc{
  margin-top: 20px;
}
.product-list-li-span3{
  font-size: 13px;
  color: #767676;
  overflow: hidden;
  text-overflow: ellipsis; /* 在超出时显示省略号 */
  display: -webkit-box;
  -webkit-line-clamp: 2; /* 限制显示的行数为2行 */
  -webkit-box-orient: vertical;
}
.color-e3{
  color: #767676;
}
.color-yellow{
  color: rgb(250, 118, 0);
}
.color-red{
  color: #FF0000;
}
.color-blue{
  color: #2C6EF6;
}

.color-cancel{
  color: #767676;
}
.table-card-bottom{
  margin-top: 10px !important;
}
</style>
