<template>
  <div class="modal-content">
    <a-form class="content-form" ref="formRef" :model="props.modalData" :label-col="labelCol">
      <a-form-item
        label="名称"
        name="name"
        :rules="[{ required: true, message: '请输入名称' }]"
      >
        <a-input v-model:value="props.modalData.name" />
      </a-form-item>
      <!-- <a-form-item
        label="区县"
        name="countyId"
        :rules="[{ required: true, message: '请选择区县' }]"
      >
        <a-select v-model:value="props.modalData.countyId" :options="countyOptions" @change="changeCountry"/>
      </a-form-item>
      <a-form-item
        label="乡镇"
        name="streetId"
        :rules="[{ required: true, message: '请选择乡镇' }]"
      >
        <a-select v-model:value="props.modalData.streetId" :options="streetOptions" @change="changeStreet"/>
      </a-form-item>
      <a-form-item
        label="社区/村"
        name="communityId"
        :rules="[{ required: true, message: '请选择社区' }]"
      >
        <a-select v-model:value="props.modalData.communityId" :options="communityOptions" @change="changeCommunity"/>
      </a-form-item> -->
      <a-form-item
        name="geom"
        label="图斑"
        :rules="[{ required: true, message: '请绘制图斑' }]"
      >
        <a-button style="margin-right: 10px;" @click="mapVisible = true">绘制图斑</a-button>
        <span v-if="props.modalData.lng && props.modalData.lat">{{ `${props.modalData.lng}, ${props.modalData.lat}` }}</span>
      </a-form-item>

    </a-form>
    <div class="button-div">
      <a-button class="cancel-button" @click="cancel">取消</a-button>
      <a-button class="save-button" type="primary" @click="submit">提交</a-button>
    </div>
  </div>
  <a-modal
    width="60%"
    v-model:open="mapVisible"
    title="绘制图斑"
    @ok="handlePostionOk"
    :destroyOnClose="true"
  >
    <Map ref="PostionRef" :page="'miningSiteManagement'" :data="props.modalData" @getAddress="getAddress"/>
  </a-modal>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue"
import { getChildrenTree } from '@/api/demo/system';
import Map from '@/components/illegalconstruction/Map.vue'
import { Add, Update } from '@/api/illegalconstruction/miningsitemanagement'
import { message } from "ant-design-vue";
const props = defineProps(['modalType', 'modalData'])
const emits = defineEmits(['closeAddOrUpdateModal'])

const formRef = ref()
const PostionRef = ref();
const mapVisible = ref(false);
const countyOptions = ref([])
const streetOptions = ref([])
const communityOptions = ref([])
const labelCol = { style: { width: '80px' } };

onMounted(() => {
  getChildrenTree({parentId: 371300}).then(res => {
    countyOptions.value = res.map(item => {
      return {
        label: item.name,
        value: item.id.toString()
      }
    })
  })
  if(props.modalType == 'update'){
    if(props.modalData.countyId){
      getChildrenTree({parentId: props.modalData.countyId}).then(res => {
        streetOptions.value = res.map(item => {
          return {
            label: item.name,
            value: item.id.toString()
          }
        })
      })
    }
    if(props.modalData.streetId){
      getChildrenTree({parentId: props.modalData.streetId}).then(res => {
        communityOptions.value = res.map(item => {
          return {
            label: item.name,
            value: item.id.toString()
          }
        })
      })
    }
  }
})
const changeCountry = (value, record) => {
  props.modalData.countyName = record.label
  props.modalData.streetId = ''
  props.modalData.streetName = ''
  props.modalData.communityId = ''
  props.modalData.communityName = ''
  getChildrenTree({parentId: value}).then(res => {
    streetOptions.value = res.map(item => {
      return {
        label: item.name,
        value: item.id.toString()
      }
    })
  })
}
const changeStreet = (value, record) => {
  props.modalData.streetName = record.label
  props.modalData.communityId = ''
  props.modalData.communityName = ''
  getChildrenTree({parentId: value}).then(res => {
    communityOptions.value = res.map(item => {
      return {
        label: item.name,
        value: item.id.toString()
      }
    })
  })
}
const changeCommunity = (value, record) => {
  props.modalData.communityName = record.label
}
const handlePostionOk = () => {
  const data = PostionRef.value.getGraphic();
  console.log(data);
  if(data){
    props.modalData.geom = data
  }
  mapVisible.value = false;
};
const getAddress = (lng, lat, siteAddress) => {
  console.log(lng,lat,siteAddress)
}
const submit = () => {
  formRef.value.validate().then(() => {
    console.log('values', props.modalData);
    if(props.modalType == 'add'){
      Add(props.modalData).then(res => {
        message.success('盗采站点添加成功')
        emits('closeAddOrUpdateModal',true)
      })
    }else{
      Update(props.modalData).then(res => {
        message.success('盗采站点编辑成功')
        emits('closeAddOrUpdateModal',true)
      })
    }
  })
  .catch(error => {
    console.log('error', error);
  });
}
const cancel = () => {
  emits('closeAddOrUpdateModal')
}
</script>

<style lang="scss" scoped>
.modal-content{
  padding: 20px;
  .content-form{
    margin-bottom: 20px;
  }
  .button-div{
    display: flex;
    justify-content: end;
    .cancel-button{
      margin-right: 10px;
    }
  }
}
</style>
