<template>
  <uni-card :is-shadow="false" :border="false" style="width: 40vw" padding="10px">
    <template v-slot:title>
      <view class="text-center u-font-16 font-bold uni-mt-10 uni-mb-10">平板设置</view>
    </template>

    <uni-forms ref="formRef" :modelValue="formData" :label-width="90" :rules="rules">
      <uni-forms-item label="平板编号" name="SS_DEVICE_NO">
        <uni-easyinput type="text" v-model="formData.SS_DEVICE_NO" placeholder="请输入编号" />
      </uni-forms-item>
      <uni-forms-item label="访问域名" name="SS_ACCESS_HOST">
        <uni-easyinput type="text" v-model="formData.SS_ACCESS_HOST" placeholder="请输入域名" />
      </uni-forms-item>
    </uni-forms>

    <template v-slot:actions>
      <view class="flex justify-between uni-mb-16">
        <u-button size="medium" shape="circle" @click="onCancel">取消</u-button>
        <u-button size="medium" type="primary" shape="circle" class="bg-main" @click="onConfirm">确定</u-button>
      </view>
    </template>
  </uni-card>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { useServiceStore } from '@/store/modules/service';

const emits = defineEmits(['on-cancel', 'on-confirm'])

const service = useServiceStore()
const formData = reactive({ SS_DEVICE_NO: '', SS_ACCESS_HOST: '' })

const rules = {
  SS_DEVICE_NO: { rules: [{ required: true, errorMessage: '请输入平板编号' }] },
  SS_ACCESS_HOST: { rules: [{ required: true, errorMessage: '请输入域名' },] },
}

const formRef = ref()

// cancel
function onCancel() { emits('on-cancel') }

// confirm
function onConfirm() {
  if (!formRef.value) return
  formRef.value.validate().then(async () => {
    // 保存平板信息到localStorage
    service.setDevice({ deviceNo: formData.SS_DEVICE_NO, host: formData.SS_ACCESS_HOST })

    await uni.$u.api.login()
    emits('on-confirm', formData)
  }).catch(error => {
    console.error(error);
  })
}
</script>

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