<template>
	<div>
		<div style="margin: 12px;color:#999">
			设置说明：此功能为各个预约渠道设置默认数量百分比，用于为“预约计划”按比例自动分配数量，此次设置各个渠道的数值总数应为100，如果不想给某个渠道分配可设置为0
		</div>
		<div style="display: flex" v-loading="loading">
			
		    <div style="width: 400px;margin-right: 40px">
				
		        <el-form :model="ratioInfo" label-width="auto" style="max-width: 600px">
		            <el-form-item v-for="(item,index) in ratioInfo" :key="index" :label="item.name">
						<div style="display: flex;">
							<el-input size="large" type="number"   min="0" max="100"  v-model="ratioInfo[index].ratio"
							          style="margin-left: 10px;width: 250px;"  />
									  <div style="margin-left: 10px;">%</div>
						</div>
		                
		            </el-form-item>
		        </el-form>
		    </div>
		    <div style="display: flex;justify-content: center; align-items: center;">
		        <el-button type="primary" @click="Save" style="height: 70px;width: 100px">
		            确定
		        </el-button>
		    </div>
		</div>
		
	</div>
    
</template>
<script setup>
import {
    ref,
    onMounted
} from 'vue'
import {
    GetAppointmentRatio,SaveAppointmentRatio
} from '@/api/api.js'
import { ElMessage } from 'element-plus'
let ratioInfo = ref([])
let loading = ref(false)
//获取当前科室各个渠道可预约百分比
const GetRatio = () => {
    loading.value = true
    GetAppointmentRatio({}).then(res => {
        loading.value = false
        if (res.status) {
            ratioInfo.value=res.data
        } else {
            ElMessage.error(res.msg)
        }

    })
}
//保存
const Save=()=>{
    let status=false
    let zong=0
    ratioInfo.value.forEach((v,i)=>{
       if(v.ratio>=0 && v.ratio<=100){
            zong=zong+Number(v.ratio)
       }else{
           ElMessage.error("第"+(i+1)+"个渠道数据不正确")
       }
    })
    if( zong ==100){
        status=true
    }else{
        status=false
        ElMessage.error("各渠道总和应为100，当前为"+zong)
    }
    if(!status) return false
    loading.value = true
    SaveAppointmentRatio({ratioInfo:ratioInfo.value}).then(res => {
        loading.value = false
        if (res.status) {
            ElMessage({
                message: '保存成功',
                type: 'success',
            })
            GetRatio()
        } else {
            ElMessage.error(res.msg)
        }
    })
}
onMounted(() => {
    GetRatio()
})
</script>
<style scoped>

</style>