<template>
    <van-popup
        v-model:show="visible"
    >
    <van-form @submit="on_submit">
        <div class="van-doc-block__title">上报伤亡情况</div>
        <van-cell-group inset>
            <van-field
                v-model="form.deaths"
                label="死亡人数"
                placeholder="填写死亡人数"
                :rules="[{ required: true, message: '请填写死亡人数'  }]"
            />
            <van-field
                v-model="form.injuries"
                label="受伤人数"
                placeholder="填写受伤人数"
                :rules="[{ required: true, message: '请填写受伤人数'  }]"
            />
            <van-field
                v-model="form.missing"
                label="失联人数"
                placeholder="填写失联人数"
                :rules="[{ required: true, message: '请填写失联人数'  }]"
            />
        </van-cell-group>
        <div style="margin: 3.0vmin;display: flex;
            flex-direction: row;
            justify-content: flex-end;">
            <div style="display: flex;        flex-direction: row;  justify-content: space-between;">
                <van-button @click="closeDialog(false)" style="margin-right:10px;">取 消</van-button>
                <van-button type="primary" native-type="submit">确 定</van-button>
            </div>
        </div>
    </van-form>    
</van-popup>
</template>

<script lang="ts" setup>
import {getCurrentInstance, reactive, ref, toRefs, watch, defineEmits} from 'vue';
import { uploadCasualties } from "@/api/event";

interface Form {
    event_id: string;
    deaths: string;
    injuries: string;
    missing: string;
}
interface Props {
    modelValue: boolean;
    data: Form;
}

const form = ref<Form>({
    event_id: '',
    deaths: '',
    injuries: '',
    missing: ''
});

const props = withDefaults(defineProps<Props>(), {
  modelValue: false
});

const emits = defineEmits(['update:modelValue']);
watch(
  () => props.modelValue,
  () => {
    if (props.modelValue) {
      form.value = props.data;
    }
    visible.value = props.modelValue;
  }
);

const visible = ref(false);

const on_submit = () => {
    console.log('on_submit');
    uploadCasualties({ ...form.value }).then((res) => {
        closeDialog(true)
      }).catch((err) => {
        closeDialog(false);
      });
};

const closeDialog = (t) => {
  emits('update:modelValue', t);
};

</script>

<style lang="scss" scoped>
.van-doc-block__title {
    color: var(--van-doc-text-color-4);
    margin: 0px;
    padding: 3vmin;
    font-size: 4.6vmin;
    font-weight: 600;
    line-height: 6.0vmin;
}
</style>