<script setup lang="ts">
import XForm from '@af-mobile-client-vue3/components/data/XForm/index.vue'
import { runLogic } from '@af-mobile-client-vue3/services/api/common'
import { useUserStore } from '@af-mobile-client-vue3/stores'
import { showToast, Button as VanButton } from 'vant'
import { onMounted, ref } from 'vue'
// 定义 props 和 emits
const props = defineProps<{
  visible: boolean
}>()
const emit = defineEmits<{
  (event: 'update:visible', value: boolean): void
  (event: 'onSubmit', value: any, userState: any): void
}>()
const userState = useUserStore()
const drivingFrom = ref()
const showLogoutConfirm = ref(false)
onMounted(() => {
  // console.log('>>>onMounted', props)
  showLogoutConfirm.value = props.visible
  runLogic('getLiuliConfiguration', {
    configName: 'DrivingConfigForm',
  }, 'af-system')
    .then((DrivingConfigForm) => {
      // console.log('>>>res', DrivingConfigForm)
      const initParams = {
        formItems: DrivingConfigForm,
      }
      if ((userState.getUserInfo()?.drivingInfo?.drivingMode || null) === null) {
        drivingFrom.value?.init(initParams)
      }
      else {
        drivingFrom.value?.init({
          ...initParams,
          formData: userState.getUserInfo()?.drivingInfo,
        })
      }
    })
    .catch((err) => {
      console.error('>>>err', err)
      showToast('获取配置失败')
    })
})
function onsubmit(data: any) {
  // console.warn('>>>onsubmit', data, userState)
  emit('onSubmit', data, userState)
  // emit('update:visible', false)
}
function updateModalVisible(visible = false) {
  showLogoutConfirm.value = visible
  emit('update:visible', visible)
}

async function handleSubmitClick() {
  // 调用 XForm 对外暴露的 submit 方法（如果存在）
  const res = await drivingFrom.value.asyncSubmit()
  onsubmit(res?.realForm)
}
function handleCloseClick() {
  updateModalVisible(false)
}
defineExpose({ updateModalVisible })
</script>

<template>
  <div v-if="showLogoutConfirm" class="driving-popup-overlay">
    <div class="driving-popup-container">
      <XForm
        ref="drivingFrom"
        service-name:="af-linepatrol"
        :is-group-form="true"
        :submit-button="false"
      />
      <div class="driving-popup-footer">
        <VanButton
          class="close-btn"

          plain block
          @click="handleCloseClick"
        >
          取消
        </VanButton>
        <VanButton
          class="submit-btn"
          type="primary"
          block
          @click="handleSubmitClick"
        >
          确认修改
        </VanButton>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.driving-popup-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgba(0, 0, 0, 0.5);
}

.driving-popup-container {
  width: 90%;
  max-width: 400px;
  max-height: 80vh;
  padding: 16px;
  overflow-y: auto;
  border-radius: 12px;
  background-color: #fff;
}

.driving-popup-footer {
  display: flex;
  gap: 8px;
  padding-top: 16px;
}

.driving-popup-footer .submit-btn,
.driving-popup-footer .close-btn {
  flex: 1;
}
</style>
