<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="巷道id" prop="plcId">
        <el-input v-model="formData.plcId" placeholder="请输入巷道id" />
      </el-form-item>
      <el-form-item label="巷道ip" prop="plcIp">
        <el-input v-model="formData.plcIp" placeholder="请输入巷道ip" />
      </el-form-item>
      <el-form-item label="巷道端口" prop="plcPort">
        <el-input v-model="formData.plcPort" placeholder="请输入巷道端口" />
      </el-form-item>
      <el-form-item label="左货架类型" prop="leftType">
        <el-select v-model="formData.leftType" placeholder="请选择左货架类型">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.SHELVES_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="左货架列数量" prop="leftColumn">
        <el-input v-model="formData.leftColumn" placeholder="请输入左货架列数量" />
      </el-form-item>
      <el-form-item label="左货架行数量" prop="leftRow">
        <el-input v-model="formData.leftRow" placeholder="请输入左货架行数量" />
      </el-form-item>
      <el-form-item label="右货架类型" prop="rightType">
        <el-select v-model="formData.rightType" placeholder="请选择右货架类型">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.SHELVES_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="右货架列数量" prop="rightColumn">
        <el-input v-model="formData.rightColumn" placeholder="请输入右货架列数量" />
      </el-form-item>
      <el-form-item label="右货架行数量" prop="rightRow">
        <el-input v-model="formData.rightRow" placeholder="请输入右货架行数量" />
      </el-form-item>
      <el-form-item label="球机" prop="camera1Id">
        <el-select v-model="formData.camera1Id" placeholder="请输入球机">
          <el-option
            v-for="item in cameraList"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="球机" prop="camera2Id">
        <el-select v-model="formData.camera2Id" placeholder="请输入球机">
          <el-option
            v-for="item in cameraList"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <!-- 子表的表单 -->
    <el-tabs v-model="subTabsName">
      <el-tab-pane label="扫码枪" name="sensorGun">
        <SensorGunForm ref="sensorGunFormRef" :street-id="formData.id" />
      </el-tab-pane>
      <el-tab-pane label="光源" name="lightSource">
        <LightSourceForm ref="lightSourceFormRef" :street-id="formData.id" />
      </el-tab-pane>
      <el-tab-pane label="RFID" name="rfids">
        <RfidForm ref="RfidFormRef" :street-id="formData.id" />
      </el-tab-pane>
      <el-tab-pane label="货架号" name="shelfCode">
        <ShelfCodeForm ref="shelfCodeRef" :street-id="formData.id" />
      </el-tab-pane>
    </el-tabs>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { StreetApi, StreetVO } from '@/api/logistics/street'
import { CameraApi, CameraVO } from '@/api/logistics/camera'
import SensorGunForm from './components/SensorGunForm.vue'
import LightSourceForm from './components/LightSourceForm.vue'
import ShelfCodeForm from './components/ShelfCodeForm.vue'
import RfidForm from './components/RfidForm.vue'
/** 巷道 表单 */
defineOptions({ name: 'StreetForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const cameraList =ref([])//存储球机
const formData = ref({
  name: undefined,
  plcId: undefined,
  plcIp: undefined,
  plcPort: undefined,
  leftType: undefined,
  leftColumn: undefined,
  leftRow: undefined,
  rightType: undefined,
  rightColumn: undefined,
  rightRow: undefined,
  camera1Id: undefined,
  camera2Id: undefined,
  deleted: undefined,
})
const formRef = ref() // 表单 Ref
/** 子表的表单 */
const subTabsName = ref('sensorGun')
const sensorGunFormRef = ref()
const lightSourceFormRef = ref()
const shelfCodeRef = ref()
const RfidFormRef = ref()
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  onMounted()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await StreetApi.getStreet(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 校验子表单
  try {
    await sensorGunFormRef.value.validate()
  } catch (e) {
    subTabsName.value = 'sensorGun'
    return
  }
  try {
    await lightSourceFormRef.value.validate()
  } catch (e) {
    subTabsName.value = 'lightSource'
    return
  }
  try {
    await RfidFormRef.value.validate()
  } catch (e) {
    subTabsName.value = 'Rfid'
    return
  }
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as StreetVO
    // 拼接子表的数据
    data.sensorGuns = sensorGunFormRef.value.getData()
    data.lightSources = lightSourceFormRef.value.getData()
    data.rfids = RfidFormRef.value.getData()
    if (formType.value === 'create') {
      await StreetApi.createStreet(data)
      message.success(t('common.createSuccess'))
    } else {
      await StreetApi.updateStreet(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    name: undefined,
    plcId: undefined,
    plcIp: undefined,
    plcPort: undefined,
    leftType: undefined,
    leftColumn: undefined,
    leftRow: undefined,
    rightType: undefined,
    rightColumn: undefined,
    rightRow: undefined,
    camera1Id: undefined,
    camera2Id: undefined,
    deleted: undefined,
  }
  formRef.value?.resetFields()
}
//球机下拉列表
onMounted(async () =>{
  try {
    const response = await CameraApi.getCameraList();
    cameraList.value = response;  //传递参数结构
  }catch (error){
    console.error(
        '错误',error
    )
  }
})
</script>