<template>
    <div class="patientDialog" v-loading="loading">
        <el-dialog
            v-model="dialogVisible"
            title="设置提醒"
            width="469px">
            <div v-loading="loading" >
                <el-form size="small"  label-width="120px" :model="formInfo" >
                    <el-form-item label="提醒项目:">
                        <el-input v-model="formInfo.alertCategory" disabled />
                    </el-form-item>
                    <el-form-item label="提醒类型:">
                        <el-radio-group v-model="formInfo.alertPeriodType" @change="onAlertPeriodTypeChange">
                            <el-radio :value="'单次'" label="单次"></el-radio>
                            <el-radio :value="'周期性'" label="周期性"></el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="提醒间隔:" v-if="formInfo.alertPeriodType==='周期性'" prop="age">
                            <el-col :span="10">
                                <el-input-number :disabled="formInfo.alertPeriodType==='单次'" v-model="formInfo.alertPeriodValue" :min="1"  >
                                
                            </el-input-number>
                            </el-col>
                            <el-col :span="4" style="background:#f5f7fa">
                                &nbsp;单位：
                            </el-col>
                            <el-col :span="10">
                                <el-select v-model="formInfo.alertPerioidUnit" :disabled="formInfo.alertPeriodType==='单次'" style="width: 100%">
                                        <el-option label="天" value="天" />
                                        <el-option label="周" value="周" />
                                        <el-option label="月" value="月" />
                                        <el-option label="年" value="年" />
                                    </el-select>
                            </el-col>
                            
                    </el-form-item>
                    <el-form-item label="提醒起始日期:" v-if="formInfo.alertPeriodType==='周期性'">
                        <el-date-picker  v-model="formInfo.alertPeriodStart" type="date" style="width: 100%" placeholder="检验日期" :clearable="false" />
                    </el-form-item>
                    <el-form-item label="提醒日期:" v-else>
                        <el-date-picker  v-model="formInfo.alertPeriodStart" type="date" style="width: 100%" placeholder="检验日期" :clearable="false" />
                    </el-form-item>
                    <el-form-item label="提醒次数:" v-if="formInfo.alertPeriodType==='周期性'">
                        <template #label>
                            <el-tooltip content="用于定义提醒发送次数,默认为0, 0为不做提醒限制" placement="top">
                                <span>提醒次数 <el-icon><QuestionFilled /></el-icon></span>
                            </el-tooltip>
                        </template>
                        <el-input-number :disabled="formInfo.alertPeriodType==='单次'" v-model="formInfo.alertPeriodRepeatLimit" :min="0" :max="100" />
                    </el-form-item>
                    <el-form-item label="是否启用:">
                        <el-switch v-model="formInfo.alertIsEnable" />
                    </el-form-item>
                </el-form>
            </div>
            <template #footer>
                <div class="dialogFooter">
                    <el-button size="small" @click="dialogVisible = false">取消</el-button>
                    <el-button   size="small" type="primary" @click="save">确认</el-button>
                </div>
            </template>
        </el-dialog>
        
    </div>
</template>
<script lang="ts" setup>
import { ref,defineExpose,defineEmits,onMounted,watch } from 'vue';
import {Add,update,list} from '/@/api/client_alert_info'

// @ts-ignore
import { useStore } from '/@/store/index';
// @ts-ignore
import { formatDate } from '/@/utils/formatTime';
import { ElLoading, ElMessage } from 'element-plus';
    const emit = defineEmits([ "saveFanhui" ]);
    const store:any = useStore();
    const loading=ref(false)
    const formInfo=ref({
        id:0,
        clientCode:'',
        patientCode:'',
        //预警类别(传染病检验提醒)
        alertCategory:'',
        /**
         * 提醒类型(单次,周期性)
         */
        alertPeriodType:"单次",
        //提醒间隔参数(间隔几天)
        alertPeriodValue:1,
        //单位(天,周,月,年)
        alertPerioidUnit:'天',
        //提醒起始日期
        alertPeriodStart:'',
        //* 该提醒重复几次就停止,0为永不停止,默认0
        alertPeriodRepeatLimit:0,
        //是否启用该提醒 1:是 0:否 ,默认1
        alertIsEnable:true,

    })
    const lsdate=ref('')
    const dialogVisible=ref(false)
    // watch(()=>formInfo.value.alertPeriodType,
    //     (newVal, oldVal)=>{
    //         if(newVal==='单次'){
    //             formInfo.value.alertPeriodValue=1
    //             formInfo.value.alertPeriodRepeatLimit=0
    //             formInfo.value.alertPeriodStart=''
    //         }else{
    //             formInfo.value.alertPeriodStart=lsdate.value?lsdate.value:''

    //         }
    //     }
    // )
    const onAlertPeriodTypeChange = (e: string) => {
         if(e ==='单次'){
                formInfo.value.alertPeriodValue=1
                formInfo.value.alertPeriodRepeatLimit=0
                formInfo.value.alertPeriodStart=''
            }else{
                formInfo.value.alertPeriodStart=lsdate.value?lsdate.value:''

            }
    };

    const save=()=>{
        console.log(formInfo.value)
        const pasm=JSON.parse(JSON.stringify(formInfo.value))
        pasm.alertIsEnable=pasm.alertIsEnable?1:0

        if(formInfo.value.id===0){
            loading.value=true
            Add(pasm).then(re=>{
                console.log(re.data)
                ElMessage.success('设置成功')
                dialogVisible.value=false
            }).finally(()=>{
                loading.value=false
            })
        }else{
            loading.value=true
            update(pasm).then(re=>{
                console.log(re.data)
                ElMessage.success('设置成功')
                dialogVisible.value=false
            }).finally(()=>{
                loading.value=false
            })
        }
    }
    const openDialog=(code,DateString,patcode)=>{
        if(code){
            console.log(code)
            
            dialogVisible.value=true
            lsdate.value=DateString + ''
            const pam = {
                page: 0,
                size: 0,
                wherecondition: `client_code='${store.state.userInfos.userInfos.client.code}' and alert_category='${code}' and patient_code='${patcode}'`,
                ordercondition: 'id DESC'
            }
            loading.value=true
            list(pam).then(re=>{
                console.log('----------------')
                console.log(re)
                if(re?.data?.list.length>0){
                    const row=re?.data?.list[0]
                    // formInfo.value={
                    //     id:row.id,
                    //     patientCode:row.patientCode,
                    //     clientCode:row.clientCode,
                    //     //预警类别(传染病检验提醒)
                    //     alertCategory:row.alertCategory,
                    //     /**
                    //      * 提醒类型(单次,周期性)
                    //      */
                    //     alertPeriodType:row.alertPeriodType,
                    //     //提醒间隔参数(间隔几天)
                    //     alertPeriodValue:row.alertPeriodValue,
                    //     //单位(天,周,月,年)
                    //     alertPerioidUnit:row.alertPerioidUnit,
                    //     //提醒起始日期
                    //     alertPeriodStart:row.alertPeriodStart,
                    //     //* 该提醒重复几次就停止,0为永不停止,默认0
                    //     alertPeriodRepeatLimit:row.alertPeriodRepeatLimit,
                    //     //是否启用该提醒 1:是 0:否 ,默认1
                    //     alertIsEnable:row.alertIsEnable?true:false,
                    // }
                    Object.assign(formInfo.value, {
                         id:row.id,
                        patientCode:row.patientCode,
                        clientCode:row.clientCode,
                        //预警类别(传染病检验提醒)
                        alertCategory:row.alertCategory,
                        /**
                         * 提醒类型(单次,周期性)
                         */
                        alertPeriodType:row.alertPeriodType,
                        //提醒间隔参数(间隔几天)
                        alertPeriodValue:row.alertPeriodValue,
                        //单位(天,周,月,年)
                        alertPerioidUnit:row.alertPerioidUnit,
                        //提醒起始日期
                        alertPeriodStart:row.alertPeriodStart,
                        //* 该提醒重复几次就停止,0为永不停止,默认0
                        alertPeriodRepeatLimit:row.alertPeriodRepeatLimit,
                        //是否启用该提醒 1:是 0:否 ,默认1
                        alertIsEnable:row.alertIsEnable?true:false,
                    })
                    console.log('formInfo: ', formInfo.value)
                }else{
                    // formInfo.value={
                    //     id:0,
                    //     patientCode: store.state.patientInfos.patientInfos.code,
                    //     clientCode:store.state.userInfos.userInfos.client.code,
                    //     //预警类别(传染病检验提醒)
                    //     alertCategory:code,
                    //     /**
                    //      * 提醒类型(单次,周期性)
                    //      */
                    //     alertPeriodType:'周期性',
                    //     //提醒间隔参数(间隔几天)
                    //     alertPeriodValue:1,
                    //     //单位(天,周,月,年)
                    //     alertPerioidUnit:'天',
                    //     //提醒起始日期
                    //     alertPeriodStart:lsdate.value? lsdate.value + '' : '',
                    //     //* 该提醒重复几次就停止,0为永不停止,默认0
                    //     alertPeriodRepeatLimit:0,
                    //     //是否启用该提醒 1:是 0:否 ,默认1
                    //     alertIsEnable:false,
                    // }
                    Object.assign(formInfo.value, {
                        id:0,
                        patientCode: store.state.patientInfos.patientInfos.code,
                        clientCode:store.state.userInfos.userInfos.client.code,
                        //预警类别(传染病检验提醒)
                        alertCategory:code,
                        /**
                         * 提醒类型(单次,周期性)
                         */
                        alertPeriodType:'周期性',
                        //提醒间隔参数(间隔几天)
                        alertPeriodValue:1,
                        //单位(天,周,月,年)
                        alertPerioidUnit:'天',
                        //提醒起始日期
                        alertPeriodStart:lsdate.value? lsdate.value + '' : '',
                        //* 该提醒重复几次就停止,0为永不停止,默认0
                        alertPeriodRepeatLimit:0,
                        //是否启用该提醒 1:是 0:否 ,默认1
                        alertIsEnable:false,
                    })
                }
            }).finally(()=>{
                loading.value=false
            }).catch(error => {
                console.error('error: ', error)
            })
        }
    }
    onMounted(()=>{
        const clientCode = store.state.userInfos.userInfos.client.code
        //穿刺方式
    })
    defineExpose({openDialog})
    
  </script>

