<template>
  <view class="page-server-setting" :style="{ backgroundImage: `url(${image.login.bg1})` }">
    <view class="header" :style="{ backgroundColor: `rgba(23, 83, 234, ${scrollTop / 100})` }">
      <view class="left">
        <view class="left">
          <u-icon class="icon" name="arrow-left" @click="uni.navigateBack()" />
        </view>
      </view>
      <view class="title">{{ $t('message.ServerSetting') }}</view>
      <view class="right"></view>
    </view>
    <u-form class="form" :model="form" ref="form" :border-bottom="false" :error-type="['toast']">
      <u-form-item class="form-item" prop="host" :border-bottom="false"
        ><u-input
          v-model="form.host"
          :placeholder="this.$t('message.PleaseInputIPAddress')"
          placeholder-style="color: rgba(255, 255, 255, 0.36)"
          :custom-style="{
            height: '88rpx',
            marginRight: '36rpx',
            color: 'rgba(255, 255, 255, 0.76)',
          }"
      /></u-form-item>
      <u-form-item class="form-item" prop="port" :border-bottom="false"
        ><u-input
          v-model="form.port"
          :placeholder="this.$t('message.PleaseInputPortNumber')"
          placeholder-style="color: rgba(255, 255, 255, 0.36)"
          :custom-style="{
            height: '88rpx',
            marginRight: '36rpx',
            color: 'rgba(255, 255, 255, 0.76)',
          }"
      /></u-form-item>
    </u-form>
    <u-button
      shape="circle"
      :custom-style="{
        marginTop: '48rpx',
        height: '88rpx',
        backgroundColor: 'rgba(31, 51, 89, 1)',
        color: 'rgba(255, 255, 255, 0.66)',
      }"
      hover-class="button-hover"
      type="primary"
      @click="onSubmit"
      >{{ $t('message.Save') }}</u-button
    >
  </view>
</template>

<script lang="ts">
import { Component, Ref } from 'vue-property-decorator';
import { BasePage } from '@/components/base/page';
import { server } from './model';
import { VForm } from 'vue/types/form';

@Component
export default class ServerPage extends BasePage {
  /**
   * 访客权限
   */
  get guest() {
    return true;
  }

  /**
   * 表单引用
   */
  @Ref('form') readonly $form!: VForm;

  form = {
    host: '',
    port: null,
  };

  rules = {
    host: [{ required: true, message: this.$t('message.PleaseInputIPAddress'), trigger: ['blur'] }],
  };

  onReady(): void {
    this.$form.setRules(this.rules);
  }

  onSubmit(): void {
    this.$form.validate(async (valid: boolean) => {
      if (valid) {
        const { host, port } = this.form;
        const address = port ? host + ':' + port : host;
        try {
          await server.isServerAlive({ host, port });
        } catch (e) {
          // uni.showToast({
          //   icon: 'none',
          //   title: this.$t('message.TryAgain') as string,
          // });
          this.customToast(this.$t('message.TryAgain') as string);
          throw e;
        }
        server.setServerAddress({ address });
        uni.showToast({
          //icon: 'success',
          title: this.$t('message.SetServerSuccessfully') as string,
          image: '/static/icons/icon-51.png',
        });
        setTimeout(() => {
          uni.navigateBack({ delta: 1 });
        }, 2000);
      }
    });
  }

  navBack(): void {
    uni.navigateBack({ delta: 1 });
  }
}
</script>

<style lang="scss" scoped>
.page-server-setting {
  background-size: cover;
  padding: 118rpx 30rpx 162rpx;
  min-height: 100%;
  color: #fff;

  .header {
    position: fixed;
    top: 36rpx;
    left: 0;
    right: 0;
    z-index: 99;
    display: flex;
    height: 88rpx;
    line-height: 88rpx;
    color: #fff;
    font-size: 34rpx;
    font-weight: 500;
    text-align: center;
    .title {
      flex: 3;
    }
    .left,
    .right {
      flex: 1;
    }
    .icon {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 88rpx;
      height: 88rpx;
    }
  }

  .logo {
    margin-top: 100rpx;
    display: flex;
    justify-content: center;
  }

  .form {
    margin-top: 104rpx;

    .form-item {
      height: 90rpx;
      border: 1px solid rgba(255, 255, 255, 0.5);
      border-radius: 90rpx;
      margin-top: 48rpx;
      padding: 0 28rpx;
    }
  }
}

.button-hover {
  opacity: 0.7;
}
</style>
