<template>
  <view class="min-page u-font-28">
    <view class="container first">
      <view class="title">升级条件</view>
      <view class="u-m-t-16">
        <up-radio-group v-model="form.upgradeType" placement="row">
          <up-radio
            v-for="item in distributionStore.upgradeTypes"
            :key="item.value"
            :name="item.value"
            :label="item.label"
          >
            <template #label>
              <text>
                {{ item.label }}
              </text>
            </template>
          </up-radio>
        </up-radio-group>
      </view>
    </view>
    <view class="container" v-for="(item,index) in form.levelConfigList">
      <view class="u-flex u-row-between">
        <text class="font-bold color-333 u-font-32">{{index+1}}级：{{ item.name }}</text>
        <view class="u-flex" @click="toggle(index)">
            <text class="color-main u-m-r-14">展开</text>
            <view class="u-flex arrow" :class="[!showDetailListSwitch[index]?'rotate' : '']">
                <up-icon name="arrow-down" color="#318AFE" ></up-icon>
            </view>
        </view>
      </view>
      <template v-if="showDetailListSwitch[index]">
      <view class="u-m-t-60">
        <view class="title">名称</view>
        <view class="u-p-t-16 u-p-b-24">
            <input placeholder="请输入名称" placeholder-class="color-999 u-font-28" v-model="item.name" @input="checkName($event, index)"></input>
        </view>
        <up-line></up-line>
        <view class="title u-m-t-24">分成比例</view>
        <view class="u-m-t-16 u-p-b-24">
            <view class="u-flex u-m-t-16">
            <input
              class="number-box"
              placeholder="请输入"
              placeholder-class="color-999 u-font-28"
              type="digit"
              @input="checkNumberCommission($event,index)"
              v-model="item.commission"
            />
            <view class="unit">%</view>
          </view>
        </view>
        <template v-if="form.upgradeType!='not_upgrade'">
          <up-line></up-line>
        <template v-if="form.upgradeType!='cost'">
          <view class="title u-m-t-24">有效人数</view>
        <view class="u-m-t-16 u-p-b-8">
            <view class="u-flex u-m-t-16">
            <input
              :disabled="index==0?true:false"
              :class="{'disabled':index==0?true:false}"

              class="number-box"
              placeholder="请输入"
              placeholder-class="color-999 u-font-28"
              type="number"
              v-model="item.inviteCount"
            />
            <view class="unit">人</view>
          </view>
        </view>

        <view class="color-999 u-font-24">有效人数：被邀请人在店铺消费过，即有一笔订单完成</view>

        </template>
        <template v-else>
          <view class="title u-m-t-24">消费金额达</view>
        <view class="u-m-t-16 u-p-b-8">
            <view class="u-flex u-m-t-16">
            <input
              class="number-box"
              :disabled="index==0?true:false"
              :class="{'disabled':index==0?true:false}"
              placeholder="请输入"
              placeholder-class="color-999 u-font-28"
              type="digit"
              v-model="item.costAmount"
            />
            <view class="unit">元</view>
          </view>
        </view>
        <view class="color-999 u-font-24">消费金额：分销员和被邀请人在店铺订单消费总金额（不包含退款）</view>

        </template>
        </template>

   
        <view class="u-m-t-24 u-flex u-row-right" style="gap: 40rpx;">
            <view class="u-flex" v-if="index==0||index==form.levelConfigList.length-1" @click="addLevelConfig">
                <up-icon name="plus-circle-fill" color="#318AFE" ></up-icon>
                <text class="u-m-l-16">{{index==0? '添加':'继续添加'}}</text>
            </view>
            <view class="u-flex" @click="remove(index)" v-if="index!=0">
                <up-icon name="minus-circle-fill" color="#EB4F4F" ></up-icon>
                <text class="u-m-l-16">删除</text>
            </view>
        </view>
      </view>
    </template>
    </view>


    <my-bottom-btn-group @save="save" @cancel="cancel"></my-bottom-btn-group>

  </view>
</template>
<script setup>
import {
  onLoad,
  onReady,
  onShow,
  onPageScroll,
  onReachBottom,
  onBackPress,
} from "@dcloudio/uni-app";

import { ref, onMounted, watch, reactive , computed} from "vue";
import { useDistributionStore } from "@/store/market.js";

const distributionStore = useDistributionStore();

const form = reactive({
  upgradeType: "",
  levelConfigList:[]
});

const showDetailListSwitch=ref([true])

function toggle(index) {
  showDetailListSwitch.value[index] = !showDetailListSwitch.value[index];
}

//现在全部的分成比例总和
const allCommission=()=>{
    let sum=0
    for(let item of form.levelConfigList){
        sum+=parseFloat(item.commission)
    }
    return sum
}


let timer=null
//全部的分成比例加起来不能超过100%
function checkNumberCommission(e,index){
    const value=e.detail.value
    clearInterval(timer)
    
    if(value>100){
        uni.showToast({
            title:'分成比例不能超过100',
            icon:'none'
        })
        timer= setTimeout(()=>{
                 form.levelConfigList[index].commission=''
            },30) 
       
        return false
    }
    //输入最多两位小数，两位小数点后的去除掉不让输入
    if(value.indexOf('.')!=-1){
        const arr=value.split('.')
        if(arr[1].length>2){
            timer= setTimeout(()=>{
                form.levelConfigList[index].commission=arr[0]+'.'+arr[1].substring(0,2)
            },30)
        }
    }
 
}

function checkName(e,index){
    const value=e.detail.value
    if(value.length>10){
        uni.showToast({
            title:'名称最多10个字符',
            icon:'none'
        })
        timer= setTimeout(()=>{
          form.levelConfigList[index].name=value.substring(0,10)

            },30)
        return false
    }
}
function remove(index){
    form.levelConfigList.splice(index,1)
    showDetailListSwitch.value.splice(index,1)
}
function addLevelConfig(){
    form.levelConfigList.push({
        name:'',
        commission:'',
        inviteCount:'',
        costAmount:''
    })
    showDetailListSwitch.value.push(true)
}
  //判断输入每一项是否为空
  function checkInput(){ 
    for(let item of form.levelConfigList){
        if(!item.name){
            uni.showToast({
                title:'请输入名称',
                icon:'none'

            })
            return false
        }
        if(!item.commission){
            uni.showToast({
                title:'请输入分成比例',
                icon:'none'

            })
            return false
        }
        if(form.upgradeType==='not_upgrade'){
            continue
        }
        if(form.upgradeType==='cost'){
            if(item.costAmount===''||item.costAmount===null){
                uni.showToast({
                    title:'请输入消费金额',
                    icon:'none'
                })
                return false
            }
        }else{
            if(item.inviteCount===''||item.inviteCount===null){
                uni.showToast({
                    title:'请输入有效人数',
                    icon:'none'
                })
                return false
            }

        }
        
    }
    return true
  }
async function save(){
    const isPas=checkInput()
    if(!isPas){
        return
    }

    await distributionStore.editConfig(form)
    uni.showToast({
        title:'保存成功',
        icon:'none'
    })
    setTimeout(() => {
        uni.navigateBack({
        delta:1
    })
    }, 1500);
   
   
}
function cancel(){
    uni.navigateBack({
        delta:1
    })
}
onLoad(()=>{
    showDetailListSwitch.value=distributionStore.config.levelConfigList.map(()=>true)
    const levelConfigList=[...distributionStore.config.levelConfigList||[]]
    form.levelConfigList=levelConfigList.length?levelConfigList:[
    {   name:'',
        commission:'',
        inviteCount:0,
        costAmount:0}
    ]
    form.upgradeType=distributionStore.config.upgradeType
})
</script>
<style lang="scss" scoped>
.min-page {
  background: #f7f7f7;
  padding: 56rpx 28rpx;
}
.container {
  background: #fff;
  padding: 32rpx 24rpx;
  margin-top: 32rpx;
  border-radius: 16rpx;
  overflow: hidden;
}
.x-padding {
  padding: 0 24rpx;
}
$height: 70rpx;
.number-box {
  font-size: 28rpx;
  padding: 0 26rpx;
  border-radius: 6rpx 0 0 6rpx;
  border-top: 2rpx solid #d9d9d9;
  border-bottom: 2rpx solid #d9d9d9;
  border-left: 2rpx solid #d9d9d9;
  background: #fff;
  box-sizing: border-box;
  height: $height;
  flex: 1;
  line-height: $height;
  &.disabled{
    background-color: #f7f7fa;
    color: #999999;
  }
}
.unit {
  display: flex;
  padding: 0 38rpx;
  height: $height;
  line-height: $height;
  align-items: center;
  border-radius: 0 6rpx 6rpx 0;
  border: 2rpx solid #d9d9d9;
  background: #f7f7fa;
  font-size: 28rpx;
  color: #999999;
}

.my-btn {
  font-size: 28rpx;
  line-height: 36rpx;
  padding: 8rpx 32rpx;
  border-radius: 12rpx;
  margin: 0;
}

.edit-btn {
  background: #e6f0ff;
  color: $my-main-color;
}

.delete-btn {
  background: #ffe7e6;
  color: #ff1c1c;
}
.title {
  font-weight: 700;
  color: #333;
  font-size: 28rpx;
}
.first {
  border-bottom: 2rpx solid #e5e5e5;
}
.arrow{
    transition: all 0.3s ease-in-out;
    &.rotate{
        transform: rotate(180deg);
    }
}
</style>
