<template>
    <!-- Modal -->
    <a-modal
      v-model:visible="modalVisible"
      fullscreen
      :title="title"
      title-align="start"
      :footer="false"
      @cancel="handleClose"
    >
      <a-form :model="modalForm" ref="modalFormRefConfig" :auto-label-width="true" :rules="rules">
        <div class="titlebox" style="margin-bottom: 35px" >
          <div class="bar"></div>
          <div class="name">健康包配置信息</div>
        </div>
        <a-row :gutter="24" v-if="modalForm.health_package_id">
          <a-col :span="12" v-if="modalForm.health_package">
            <a-form-item field="service_count" label="总服务数：">
                <a-input-number
              v-model="modalForm.health_package.service_count"
              :style="{ width: '320px' }"
              placeholder="请输入总服务数"
              class="input-demo"
              disabled
              :step="1" :precision="0"
              :min="1"
            />
            </a-form-item>
          </a-col>
          <a-col :span="12"  v-if="modalForm.health_package_id">
            <a-form-item field="monthly_frequency" label="每月次数：" v-if="modalForm.health_package">
                <a-input-number
               v-model="modalForm.health_package.monthly_frequency"
              :style="{ width: '320px' }"
              placeholder="请输入每月次数"
              class="input-demo"
              disabled
              :step="1" :precision="0"
              :min="0"
            />
            </a-form-item>  
          </a-col>
        </a-row>
        <a-row :gutter="24" >
          <a-col :span="12"  v-if="modalForm.health_package_id">
            <a-form-item field="service_rate" label="服务费率(%)：" v-if="modalForm.health_package">
                <a-input
              v-model="modalForm.health_package.service_rate"
              disabled
              :style="{ width: '320px' }"
              placeholder="请选择服务费率"
              class="input-demo"
              :step="1" :precision="0"
              :min="1"
            />
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if="modalForm.health_package_id">
            <a-form-item field="effective_days" label="服务有效天数：" v-if="modalForm.health_package">
                <a-input
              v-model="modalForm.health_package.effective_days"
              :style="{ width: '320px' }"
              disabled
              placeholder="请输入服务有效天数"
              class="input-demo"
              :step="1" :precision="0"
              :min="0"
            />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item field="package_id" label="健康包：">
              <a-select  v-model="modalForm.package_id" placeholder="请选择健康包"  allow-search
             :loading="loading"  @search="healthConfig" :disabled="id?true:false" @change="changePackage">
              <a-option v-for="item in packageList" :key="item.package_id" :value="item.package_id" :label="item.package_id">
                {{ item.package_id }}
              </a-option>
             </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24" >
          <a-col :span="12">
            <a-form-item field="doctor_id" label="选择医生：">
              <a-select  v-model="modalForm.doctor_id" placeholder="请选择医生"  allow-search
             :loading="loading"  @search="handleGetDoctor"  @change="changeDoctor" :disabled="(id || !packagePrice)?true:false" @click="isSelectPackage">
              <a-option v-for="item in doctorList" :key="item.doctor_id" :value="item.doctor_id" :label="item.user_name">
                {{ item.user_name }}
              </a-option>
             </a-select>
            </a-form-item>
          </a-col>
         
        </a-row>
        <a-row :gutter="24" >
          <a-col :span="13">
            <a-form-item field="service_price" label="服务价格(元)：">
              <a-input-number
               disabled
               v-model="modalForm.service_price"
              :style="{ width: '320px' }"
              placeholder="请输入服务价格"
              class="input-demo"
              :step="1" :precision="0"
              :min="0"
            /><span class="tips" v-if="!id && modalForm.doctor_id">&nbsp;&nbsp;提示：<span v-if="tuwenPrice">问诊金额{{tuwenPrice*6*.5}}元，</span><span v-if="packagePrice">健康包金额{{packagePrice}}元</span></span>
            </a-form-item>
          </a-col>
         
        </a-row>
        <a-row :gutter="24" >
          <a-col :span="12">
            <a-form-item field="is_enable" label="状态：">
              <a-switch
              v-model="modalForm.is_enable" 
              :checked-value="1"
              :unchecked-value="0"
            />
            </a-form-item>
          </a-col>
         
        </a-row>
        <a-divider />
      </a-form>
      <div class="titlebox" >
          <div class="bar"></div>
          <div class="name">操作</div>
        </div>
        <a-row :gutter="24" style="margin-top: 35px;" >
          <a-col :span="24">
            <a-form-item field="" label="" no-style>
              <a-space >
                <a-button type="primary"  @click="handleSubmit" v-has="'admin:sysHealthconfigList:save'">保存</a-button>
              </a-space>
            </a-form-item>
          </a-col>
        </a-row> 
    </a-modal>
  </template>
  <script setup>
  import { ref,toRefs,watch,getCurrentInstance,reactive,defineExpose} from 'vue';
  import { addHealthConfig,updateHealthConfig,getDoctorList,getHealthConfigAddList} from '@/api/inquiry/service';
  import {getInquiryconfigDetail} from "@/api/inquiry/config"
  import { parseTime } from '@/utils/parseTime';
  const { proxy } = getCurrentInstance();
  const product_data=ref([]);
  const props = defineProps({
    // 是否显示
    modalVisible: {
      type: Boolean,
      default: false,
    },
    id:{
     type:String,
     default:''
    },
    modalForm:{
        type:Object,
        default:{
          user_id:''
        } 
    }
  });

const title=ref('添加问诊配置');
const loading=ref(false);
const doctorList=ref([]);
const packageList=ref([]);
const handleGetDoctor=async(name='',id='')=>{
  loading.value=true;
  const {code,data}=await getDoctorList({
    user_name:name,
    doctor_id:id,
    multi_point_status:1
  });
  if(code==200){
    doctorList.value=data;
  }
  loading.value=false;
};
const packagePrice=ref(null);
const changePackage=(val)=>{
  packageList.value.forEach(item=>{
    if(item.package_id==val){
      packagePrice.value=item.discount_product_total_amount;
    }
  })
   
}
const healthConfig=async()=>{
  const {code,data}=await getHealthConfigAddList({
    limit:100
  });
  if(code==200){
   packageList.value=data;
  }
}
const reset=()=>{
    product_data.value=[];
    packagePrice.value=null;
    tuwenPrice.value='';
};

let multi_point_status=null;
const tuwenPrice=ref('');
const configDetail=async()=>{
  const {code,data}=await getInquiryconfigDetail({
    inquiry_config_id:'',
    inquiry_type:1,
    inquiry_mode:1,
    doctor_id:modalForm.doctor_id
  });
  if(code==200){
    if(data.inquiry_price){
      tuwenPrice.value=data.inquiry_price;
      if(!id.value){
        emits('setPrice',data.inquiry_price*0.6*5+packagePrice.value)
      }
    }else{
      proxy.$message.warning('该医生需要开启图文问诊，才能开通此服务');
    }
   
   
    // if(data.multi_point_status!=1){
    //   multi_point_status=data.multi_point_status;
    //   proxy.$message.warning('本服务需开处方，该医生需要做多点执业认证');
    // }
  }
}
const changeDoctor=(val)=>{
  configDetail();
}
const isSelectPackage=()=>{
  if(packagePrice.value==null && !id.value){
    proxy.$message.warning('请先选择健康包');
  }
}
  const {modalVisible,id,modalForm}=toRefs(props);
  const emits = defineEmits(['healthVisibleChange','freshList']);
  watch(()=>props.id,(newVal,oldValval)=>{
    if(props.id){
      title.value='修改健康包配置';
    }else{
      healthConfig();
      title.value='添加健康包配置';
      handleGetDoctor();
    }
  },{immediate:true})
  watch(()=>props.modalForm,(newVal,oldValval)=>{
    if(props.modalForm.doctor_inquiry_config){
      handleGetDoctor('',props.modalForm.doctor_inquiry_config.doctor_id);
    }
   
  },{immediate:true,deep:true})








  // Akiraka 20230210 关闭弹窗
  const handleClose = () => {
    proxy.$refs.modalFormRefConfig.resetFields();
    reset();
    emits('healthVisibleChange', false);
  };
  
  const handleSubmit=()=>{
    proxy.$refs.modalFormRefConfig.validate(async (valid) => {
        let data=null;
        if(tuwenPrice.value ===''){
          proxy.$message.warning('该医生需要开启图文问诊，才能开通此服务');
          return false;
        }
        // if(multi_point_status!=1){
        //   proxy.$message.warning('本服务需开处方，该医生需要做多点执业认证');
        //   return false;
        // };
        let {doctor_id,package_id,service_price,is_enable}=modalForm.value;
        if (!valid) {
          if(props.id){
            data= await updateHealthConfig(props.id,{
              doctor_id,
              package_id,
              service_price:String(service_price),
              is_enable
            })
          }else{
            data= await addHealthConfig({
              doctor_id,
              package_id,
              service_price:String(service_price),
              is_enable
            });
          }
          if(data.code==200){
            props.id?proxy.$message.success('修改成功'):proxy.$message.success('添加成功');
            //emits('freshList', '');
          }
          handleClose();
        }else {
          console.log(valid)
          proxy.$message.error('表单校验失败');
          //done(false);
        }
      });
  };
  const rules = {
    package_id: [{ required: true, message: '请选择服务包' }],
    doctor_id:[{ required: true, message: '请选择医生' }],
    service_price: [{ required: true, message: '请输入服务价格' }],
    is_enable:[{ required: true, message: '请选择状态' }],
  };

  defineExpose({
    reset,
    changeDoctor
 });
  </script>
  <style scoped>

  </style>