<template>
  <div class="p-2">
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="600px" :draggable="draggable">
      <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="108px">
        <el-form-item label="工作站名称" prop="stationName">
          <el-input v-model="formData.stationName" placeholder="请输入工作站名称" />
        </el-form-item>
        <el-form-item label="工作站编号" prop="stationNumb">
          <el-input v-model="formData.stationNumb" placeholder="请输入工作站编号" />
        </el-form-item>
        <el-form-item label="工作站类型" prop="stationType">
          <el-select v-model="formData.stationType" placeholder="请选择工作站类型">
            <el-option
                v-for="dict in WORKSTATE_TYPE"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="IP地址" prop="ip">
          <el-input v-model="formData.ip" placeholder="请输入IP地址" />
        </el-form-item>
        <el-form-item label="MAC地址" prop="mac">
          <el-input v-model="formData.mac" placeholder="请输入MAC地址" />
        </el-form-item>
        <el-form-item label="可用串口" prop="usablePort">
          <el-input v-model="formData.usablePort" placeholder="请输入可用串口" />
        </el-form-item>
        <el-form-item label="服务端口" prop="commPort">
          <el-input v-model="formData.commPort" placeholder="请输入服务端口" />
        </el-form-item>
        <el-form-item label="计算机名称" prop="computerName">
          <el-input v-model="formData.computerName" placeholder="请输入计算机名称" />
        </el-form-item>
        <el-form-item label="最大充值金额" prop="maxAmount">
          <el-input v-model="formData.maxAmount" placeholder="请输入最大充值金额" />
        </el-form-item>
        <el-form-item label="服务器端口" prop="serverPort">
          <el-input v-model="formData.serverPort" placeholder="请输入服务器端口" />
        </el-form-item>
        <el-form-item label="工作站说明" prop="remark">
          <el-input v-model="formData.remark" placeholder="请输入工作站说明" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="formLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="dialog.visible = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PtWorkstationForm" lang="ts">
import * as PtWorkstationApi from '@/api/basics/basicParameter/ptWorkstation';
import { PtWorkstationForm } from '@/api/basics/basicParameter/ptWorkstation/types';
import { useI18n } from 'vue-i18n';

defineOptions({ name: 'PtWorkstationForm' });

// 接收父组件传入属性
// const props = defineProps({})

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
// 国际化
const { t } = useI18n();

/** 数据字典 */
const { WORKSTATE_TYPE, sys_yes_no } = toRefs<any>(proxy?.useDict('WORKSTATE_TYPE', 'sys_yes_no'));
// 对话框
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
// 窗体是否可以拖动
const draggable = ref(true);
// 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formLoading = ref(false);
// 表单的类型：create - 新增；update - 修改
const formType = ref('');
// 表单引用
const formRef = ref<ElFormInstance>();
// 表单数据
const formData = ref<PtWorkstationForm>({
  stationId: undefined,
  stationName: '',
  stationNumb: undefined,
  stationType: '',
  ip: '',
  mac: '',
  usablePort: '',
  commPort: undefined,
  computerName: '',
  maxAmount: undefined,
  serverPort: undefined,
  onLine: 'N',
  remark: '',
});
// 表单校验规则
const formRules = reactive({
  stationId: [{ required: true, message: '工作站id不能为空', trigger: 'blur' }],
  stationName: [{ required: true, message: '工作站名称不能为空', trigger: 'blur' }],
  stationNumb: [{ required: true, message: '工作站编号不能为空', trigger: 'blur' }],
  stationType: [{ required: true, message: '工作站类型不能为空', trigger: 'change' }],
  ip: [{ required: true, message: 'IP地址不能为空', trigger: 'blur' }],
  usablePort: [{ required: true, message: '可用串口不能为空', trigger: 'blur' }],
  commPort: [{ required: true, message: '服务端口不能为空', trigger: 'blur' }],
  serverPort: [{ required: true, message: '服务器端口不能为空', trigger: 'blur' }],
});
/** 当前组件方法 */
/** 窗体打开事件 */
const open = async (type: string, id?: number) => {
  dialog.visible = true;
  dialog.title = t('action.' + type);
  formType.value = type;
  resetForm();
  // 修改时，设置数据
  if (id) {
    formLoading.value = true;
    try {
      const res = await PtWorkstationApi.getPtWorkstation(id);
      Object.assign(formData.value, res.data);
    } finally {
      formLoading.value = false;
    }
  }
};
/** 表单重置 */
const resetForm = () => {
  formData.value = {
    stationId: undefined,
    stationName: '',
    stationNumb: undefined,
    stationType: '',
    ip: '',
    mac: '',
    usablePort: '',
    commPort: undefined,
    computerName: '',
    maxAmount: undefined,
    serverPort: undefined,
    onLine: '',
    remark: '',
  };
  formRef.value?.resetFields();
};
// 传回给父组件的属性与方法
// 提供 open 方法，用于打开弹窗
defineExpose({ open });
/** 触发父组件的事件 */
/** 定义 success 事件，用于操作成功后的回调 */
const emit = defineEmits(['success']);
/** 提交按钮 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return;
  const valid = await formRef.value?.validate();
  if (!valid) return;
  // 提交请求
  formLoading.value = true;
  try {
    const data = formData.value as unknown as PtWorkstationForm;
    if (formType.value === 'create') {
      await PtWorkstationApi.addPtWorkstation(data);
      proxy?.$modal.msgSuccess('新增成功');
    } else {
      await PtWorkstationApi.updatePtWorkstation(data);
      proxy?.$modal.msgSuccess('修改成功');
    }
    dialog.visible = false;
    // 发送操作成功的事件
    emit('success');
  } finally {
    formLoading.value = false;
  }
};

/** 初始化 */
onMounted(() => {});
</script>
