<template>
    <el-dialog :model-value="visible" title="编辑主诉" width="600px" append-to-body @close="handleClose" class="main-sult-dialog">
        <div class="dialog-body">
            <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
                <el-form-item label="主诉：" prop="mainSult">
                    <el-input 
                        v-model="form.mainSult" 
                        type="textarea" 
                        :rows="6"
                        placeholder="请输入主诉内容"
                        maxlength="500"
                        show-word-limit
                    />
                </el-form-item>
            </el-form>
        </div>
        <template #footer>
            <el-button type="primary" @click="handleSave">保存</el-button>
            <el-button @click="handleClose">取消</el-button>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { ElDialog, ElForm, ElFormItem, ElInput, ElButton } from 'element-plus';

const props = defineProps({
    visible: Boolean,
    mainSultData: {
        type: String,
        default: ''
    }
});

const emit = defineEmits(['update:visible', 'save', 'close']);

const formRef = ref();
const form = ref({
    mainSult: ''
});

watch(() => props.mainSultData, (val) => {
    form.value.mainSult = val || '';
}, { immediate: true });

const rules = {
    mainSult: [
        { required: true, message: '请输入主诉内容', trigger: 'blur' }
    ]
};

function handleSave() {
    formRef.value.validate((valid) => {
        if (valid) {
            emit('save', form.value.mainSult);
            emit('update:visible', false);
        }
    });
}

function handleClose() {
    emit('update:visible', false);
    emit('close');
}

defineOptions({
    name: 'MainSultDialog'
});
</script>

<style scoped>
.main-sult-dialog :deep(.el-dialog__body) {
    padding: 20px 20px 10px 20px;
}

.dialog-body {
    font-size: 14px;
    color: #222;
}

.el-form-item {
    margin-bottom: 18px;
}
</style>
