<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.event_title"
                label="事件名称"
                readonly 
                :rules="[{ required: true, message: '请填写事件名称'  }]"
            />
            <van-field
                v-model="form.event_level_text"
                is-link
                readonly
                label="事件等级"
                placeholder="选择事件等级"
                @click="show_event_level_picker = true"
            />
        </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>

<van-popup v-model:show="show_event_level_picker" round position="bottom">
    <van-picker
        :columns="opt_event_level"
        @cancel="show_event_level_picker = false"
        @confirm="on_event_level_confirm"
    />
</van-popup>

</template>

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

const proxy = getCurrentInstance()?.proxy;
const { mm_event_level } = toRefs<any>(proxy?.useDict( 'mm_event_level' ));

const opt_event_level = [
    { text: '一般', value: '1' },
    { text: '较大', value: '2' },
    { text: '重大', value: '3' },
    { text: '特别重大', value: '4' },
    { text: '其他', value: '0' },
];


interface Form {
    event_id: string;
    event_title: string;
    event_level: string;
    event_level_text: string
}
interface Props {
    modelValue: boolean;
    data: Form;
}

const form = ref<Form>({
    event_id: '',
    event_title: '',
    event_level: '',
    event_level_text: ''
});

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

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

const visible = ref(false);
const show_event_level_picker = ref(false)

const on_event_level_confirm = ({ selectedOptions }) => {
    console.log(selectedOptions[0]);
    show_event_level_picker.value = false;
    form.value.event_level_text = selectedOptions[0].text;
    form.value.event_level = selectedOptions[0].value;
};


const on_submit = () => {
    console.log('on_submit');
    startEvent({ eventId: form.value.event_id, ...form.value }).then((res) => {
        closeDialog(true)
      }).catch((err) => {
      });
};

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>