<template>
  <div>
    <el-dialog
      v-model="state.showDialog"
      destroy-on-close
      title="通讯设置"
      draggable
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="600px"
    >
      <el-form :model="form" ref="formRef" size="default" label-width="120px">
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="子系统IP" prop="subSystemIP">
              <el-input v-model="form.subSystemIP" clearable placeholder="请输入子系统IP地址" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="子系统端口" prop="subSystemIPPort">
              <el-input v-model="form.subSystemIPPort" clearable placeholder="请输入子系统端口" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="设备IP" prop="deviceIP">
              <el-input v-model="form.deviceIP" clearable placeholder="请输入设备IP地址" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="设备端口" prop="deviceIPPort">
              <el-input v-model="form.deviceIPPort" clearable placeholder="请输入设备端口" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="服务名称" prop="serviceName">
              <el-input v-model="form.serviceName" clearable placeholder="请输入服务名称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" @click="onSure" size="default">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup name="admin/Uspscale/communication-form">
import { reactive, toRefs, ref } from 'vue'

interface CommunicationData {
  subSystemIP?: string | null
  subSystemIPPort?: string | null
  deviceIP?: string | null
  deviceIPPort?: string | null
  serviceName?: string | null
}

const formRef = ref()
const state = reactive({
  showDialog: false,
  form: {
    subSystemIP: '',
    subSystemIPPort: '',
    deviceIP: '',
    deviceIPPort: '',
    serviceName: '',
  } as CommunicationData,
})

const { form } = toRefs(state)

const emit = defineEmits(['confirm'])

// 打开对话框
const open = (data: CommunicationData = {}) => {
  state.form = {
    subSystemIP: data.subSystemIP || '',
    subSystemIPPort: data.subSystemIPPort || '',
    deviceIP: data.deviceIP || '',
    deviceIPPort: data.deviceIPPort || '',
    serviceName: data.serviceName || '',
  }
  state.showDialog = true
}

// 取消
const onCancel = () => {
  state.showDialog = false
}

// 确定
const onSure = () => {
  emit('confirm', { ...state.form })
  state.showDialog = false
}

defineExpose({
  open,
})
</script>

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