<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ __('App推送设置') }}</span>
          </div>
          <div>
            <el-form
              ref="form"
              label-width="100px"
              :model="form"
              :rules="rules"
              size="medium"
            >
              <el-form-item
                label="APP ID"
                label-width="150px"
                prop="unipush_app_id"
              >
                <el-input
                  v-model="form.unipush_app_id"
                  clearable
                  :placeholder="__('请输入 APP ID')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                label="AppKey"
                label-width="150px"
                prop="unipush_app_key"
              >
                <el-input
                  v-model="form.unipush_app_key"
                  clearable
                  :placeholder="__('请输入AppKey')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                label="AppSecret"
                label-width="150px"
                prop="unipush_app_secret"
              >
                <el-input
                  v-model="form.unipush_app_secret"
                  clearable
                  :placeholder="__('请输入AppSecret')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                label="MasterSecret"
                label-width="150px"
                prop="unipush_master_secret"
              >
                <el-input
                  v-model="form.unipush_master_secret"
                  clearable
                  :placeholder="__('请输入MasterSecret')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('APP包名')"
                label-width="150px"
                prop="unipush_packagename"
              >
                <el-input
                  v-model="form.unipush_packagename"
                  clearable
                  :placeholder="__('请输入APP包名')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
            </el-form>
          </div>
          <div>
            <el-button
              class="form-sub-btn"
              icon="el-icon-s-tools"
              type="primary"
              @click="handleEdit"
            >
              {{ __('修改') }}
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, doEdit } from '@/api/base/config'

  export default {
    name: 'Push',
    components: {},
    data() {
      return {
        form: {
          unipush_app_id: undefined,
          unipush_app_key: undefined,
          unipush_app_secret: undefined,
          unipush_master_secret: undefined,
          unipush_packagename: undefined,
        },
        rules: {
          unipush_app_id: [
            {
              required: true,
              message: this.__('请输入 APP ID'),
              trigger: 'blur',
            },
          ],
          unipush_app_key: [
            {
              required: true,
              message: this.__('请输入AppKey'),
              trigger: 'blur',
            },
          ],
          unipush_app_secret: [
            {
              required: true,
              message: this.__('请输入AppSecret'),
              trigger: 'blur',
            },
          ],
          unipush_master_secret: [
            {
              required: true,
              message: this.__('请输入MasterSecret'),
              trigger: 'blur',
            },
          ],
          unipush_packagename: [
            {
              required: true,
              message: this.__('请输入APP包名'),
              trigger: 'blur',
            },
          ],
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      async fetchData() {
        const { data } = await getList()
        this.form = {
          unipush_app_id: data.unipush_app_id,
          unipush_app_key: data.unipush_app_key,
          unipush_app_secret: data.unipush_app_secret,
          unipush_master_secret: data.unipush_master_secret,
          unipush_packagename: data.unipush_packagename,
        }
      },
      handleEdit() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit({
              param: JSON.stringify(this.form),
            })

            if (status == 200) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
