<script setup>
import RPC from '@/utils/rpc'
import MapSelector from '@/components/MapSelector.vue'

const props = defineProps({
  type: {
    type: String,
    required: true,
  },
  row: [Object],
})
import { VueDraggable } from 'vue-draggable-plus'
const list = ref([
  {
    name: 'Joao',
    id: 1,
  },
  {
    name: 'Jean',
    id: 2,
  },
  {
    name: 'Johanna',
    id: 3,
  },
  {
    name: 'Juan',
    id: 4,
  },
])

const emit = defineEmits(['closed', 'onSuccess'])

const dialogVisible = ref(false)

const isAdd = computed(() => props.type === 'add')

onMounted(() => {
  dialogVisible.value = true
})

const formRef = ref(null)
const formField = ref({})
const formRules = reactive({})

const loading = ref(false)
const showMapSelector = ref(false)

function closed() {
  emit('closed')
}

const submitForm = () => {}

if (!isAdd.value) {
  // formField.value = props.row
}

const chooseCoordinates = () => {
  showMapSelector.value = true
}

// 处理地图坐标选择确认
const handleCoordinateConfirm = (coordinates) => {
  console.log('接收到坐标确认事件:', coordinates)
  formField.value.longitude = coordinates.longitude
  formField.value.latitude = coordinates.latitude
  console.log('表单字段已更新:', formField.value)
  ElMessage.success(`坐标选择成功: 经度 ${coordinates.longitude}, 纬度 ${coordinates.latitude}`)
}
</script>

<template>
  <el-dialog
    align-center
    :title="isAdd ? '新增' : '修改'"
    width="774px"
    v-model="dialogVisible"
    @closed="closed"
    :close-on-click-modal="false"
  >
    <el-form
      ref="formRef"
      label-position="right"
      label-suffix=":"
      :model="formField"
      :rules="formRules"
      v-loading="loading"
    >
      <!-- <VueDraggable ref="el" v-model="list">
      <div v-for="item in list" :key="item.id">
        {{ item.name }}
      </div>
    </VueDraggable> -->
      <el-form-item prop="longitude" label="经度">
        <el-input-number :controls="false" placeholder="请输入" v-model="formField.longitude" />
      </el-form-item>
      <el-form-item prop="latitude" label="纬度">
        <el-input-number :controls="false" placeholder="请输入" v-model="formField.latitude" />
      </el-form-item>
      <el-button type="primary" @click="chooseCoordinates">选择坐标</el-button>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closed"> 取 消 </el-button>
        <el-button type="primary" :loading="loading" @click="submitForm"> 确 定 </el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 地图坐标选择器 -->
  <MapSelector
    v-model="showMapSelector"
    :longitude="formField.longitude"
    :latitude="formField.latitude"
    @confirm="handleCoordinateConfirm"
  />
</template>

<style lang="scss" scoped></style>
