<script lang="ts" setup>
import {ref, unref, nextTick} from 'vue'
import {updateWarnForm, InstantForm} from './warn.data'
import {useI18n} from '@/hooks/web/useI18n'
import {useMessage} from '@/hooks/web/useMessage'
import {BasicForm, useForm} from '@/components/Form'
import {BasicModal, useModalInner} from '@/components/Modal'
import {getWarn, updateWarn} from '@/api/alert/warn'
import {getSearchFormSchema} from '@/views/run/instant/instant.data'
import {Divider, Descriptions, DescriptionsItem} from 'ant-design-vue'
import {getInstantPoint} from "@/api/alert/run/instant";

defineOptions({name: 'UpdateModal'})

const emit = defineEmits(['success', 'register'])
const {t} = useI18n()
const {createMessage} = useMessage()
const isUpdate = ref(true)


// 新增的时候增加查询表单
const [registerQueryForm, {
  setProps,
  setFieldsValue: setQueryFields,
  resetFields: resetQueryFields,
  validate: validateQueryForm,
}] = useForm({
  labelWidth: 80,
  baseColProps: {span: 24},
  schemas: InstantForm,
  showResetButton: false,
  showSubmitButton: true,
  showActionButtonGroup: true,
  actionColOptions: {span: 2, style: {textAlign: 'left', marginLeft: '10px'}},
})


const [registerForm, {setFieldsValue, resetFields, validate}] = useForm({
  labelWidth: 120,
  baseColProps: {span: 24},
  schemas: updateWarnForm,
  showActionButtonGroup: false,
  actionColOptions: {span: 23},
})
const pointInfo = ref<any>();

function getPointInfo(list, inputName) {
  if (!Array.isArray(list) || !inputName) return undefined
  return list.find(item => item.inputName === inputName)
}

async function handleQuery() {
  //先获取表单数据
  const queryValues = await validateQueryForm()
  //查询各种点号信息
  const res = await getInstantPoint(queryValues.mpId)
  //从结果中筛选inputName等于选中的inputName的项
  pointInfo.value = getPointInfo(res, queryValues?.inputName)
}
const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
  resetFields()
  setModalProps({width: 800, useWrapper: true, minHeight: 180, confirmLoading: false})
  // 如果是修改modal，则赋值给表单
  isUpdate.value = !!data?.isUpdate
  if (unref(isUpdate)) {
    const res = await getWarn(data.record.warnId)
    setFieldsValue({...res})
  }
  //如果是新增
  else {
    // 2. 等待下一次 DOM 更新（确保表单已渲染）
    await nextTick();
//查询各种点号信息
    await handleQuery()
  }
})
async function handleSubmit() {
  try {
    const values = await validate()
    setModalProps({confirmLoading: true})
    if (unref(isUpdate)) {
    await updateWarn(values)
    closeModal()
    emit('success')
    createMessage.success(t('common.saveSuccessText'))
  }
  else{
// 新增预警

    }
  } finally {
    setModalProps({confirmLoading: false})
  }
}
</script>

<template>
  <BasicModal v-bind="$attrs" :title="isUpdate ? t('action.edit') : t('action.create')"
              @register="registerModal" @ok="handleSubmit">
    <!--查询表单-->
    <BasicForm v-if="!isUpdate" @register="registerQueryForm" @submit="handleQuery"/>
    <Divider style="margin-top:0px"/>
    <!--查询出来的结果-->
    <Descriptions bordered size="small" :column="2">
      <DescriptionsItem label="测点名">{{ pointInfo?.inputInfo }}</DescriptionsItem>
      <DescriptionsItem label="测点点号">{{ pointInfo?.inputName }}</DescriptionsItem>
      <DescriptionsItem label="重构点名">{{ pointInfo?.inputInfo + "-重构值" }}</DescriptionsItem>
      <DescriptionsItem label="重构点号">{{ pointInfo?.outPointInfo }}</DescriptionsItem>
      <DescriptionsItem label="残差点名">{{ pointInfo?.inputInfo + "-偏差值" }}</DescriptionsItem>
      <DescriptionsItem label="残差点号">{{ pointInfo?.biasPointInfo }}</DescriptionsItem>
    </Descriptions>
    <Divider style="margin-top:10px"/>
    <BasicForm @register="registerForm"/>
  </BasicModal>
</template>
