<script setup lang="ts">
import {ref} from "vue";
import type {IAppBaseLed} from "@/model/table/IAppBaseLed.ts";
import LedBrandFormatter from "@/plugin/formatter/LedBrandFormatter.ts";
import TrueFalseTagStyleFormatter from "@/plugin/formatter/TrueFalseTagStyleFormatter.ts";
import LedColorTypeFormatter from "@/plugin/formatter/LedColorTypeFormatter.ts";
import MessageUtils from "@/utils/MessageUtils.ts";
import LedApi from "@/api/led.ts";
import {AppServeResponseCodeEnum} from "@/constant/enums/AppServeResponseCodeEnum.ts";
import type {AppServeResponse} from "@/interface/api/AppServeResponse.ts";

const modelValue = defineModel('modelValue', {required: true, default: false});
const emit = defineEmits(['reLoadingTableData']);
const ledBrandFormatter = new LedBrandFormatter();
const trueFalseTagStyleFormatter = new TrueFalseTagStyleFormatter();
const ledColorTypeFormatter = new LedColorTypeFormatter();
const formData = ref<IAppBaseLed>({ledBrand: 0, colorType: 1, isExternal: 0, height: 64, width: 192});
const close = () => {
  modelValue.value = false;
};
// 添加
const add = () => {
  if(formData.value.ledIp == '' || formData.value.ledIp == null) {
    MessageUtils.warningMessageBox('请填写 LED IP 地址', '警告');
    return;
  }
  MessageUtils.confirmMessageBox('确定添加吗？', '添加 LED 屏基础资料').then(() => {
    const loadingInstance = MessageUtils.loading();
    LedApi.addBaseLed(formData.value).then((res) => {
      const response = JSON.parse(JSON.stringify(res.data)) as AppServeResponse;
      if (response.code == AppServeResponseCodeEnum.SUCCESS) {
        MessageUtils.successMessage('添加成功');
        emit('reLoadingTableData');
        close();
        return;
      }
      MessageUtils.warningMessageBox(response.msg);
    }).catch(() => {}).finally(() => {
      loadingInstance.close();
    })
  }).catch(() => {})
};
</script>

<template>
  <div>
    <el-dialog :model-value="modelValue" title="添加 LED 基础资料" @close="close" :close-on-click-modal="false">
      <el-scrollbar style="height:50vh">
        <el-form :model="formData" label-position="right" label-width="120px" require-asterisk-position="right">
          <el-form-item label="LED IP:" required>
            <el-input v-model="formData.ledIp" placeholder="请填写LED 的 IP 地址"></el-input>
            <el-text type="warning" style="font-size: 12px">必须是正确的 IP 格式</el-text>
          </el-form-item>
          <el-form-item label="LED 品牌:" required>
            <el-select v-model="formData.ledBrand" placeholder="请选择 LED 品牌" clearable>
              <el-option v-for="item in ledBrandFormatter.ledBrand" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="高度:" required>
            <el-input-number v-model="formData.height" :min="1"/>
          </el-form-item>
          <el-form-item label="宽度:" required>
            <el-input-number v-model="formData.width" :min="1"/>
          </el-form-item>
          <el-form-item label="颜色类型:" required>
            <el-select v-model="formData.colorType" placeholder="请选择 LED 颜色类型" clearable>
              <el-option v-for="item in ledColorTypeFormatter.ledColorType" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="位置:" required>
            <el-input v-model="formData.location"></el-input>
          </el-form-item>
          <el-form-item label="是否外部控制:" required>
            <el-select v-model="formData.isExternal" clearable>
              <el-option v-for="item in trueFalseTagStyleFormatter.trueFalseTagStyle" :key="item.value" :label="item.label" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="备注:">
            <el-input v-model="formData.remark"></el-input>
          </el-form-item>
        </el-form>
      </el-scrollbar>
      <template #footer>
        <el-button-group>
          <el-button type="primary" @click="add">确定添加</el-button>
          <el-button type="danger" @click="close">关闭窗口</el-button>
        </el-button-group>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>