<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ __('微信设置') }}</span>
          </div>
          <div>
            <el-form
              ref="form"
              label-width="100px"
              :model="form"
              :rules="rules"
              size="medium"
            >
              <el-form-item
                label="Weixin Callback Url"
                label-width="200px"
                prop="wechat_callback_url"
              >
                <el-input
                  v-model="form.wechat_callback_url"
                  clearable
                  :placeholder="__('请输入 Weixin Callback Url')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                label="Weixin Token"
                label-width="200px"
                prop="wechat_token"
              >
                <el-input
                  v-model="form.wechat_token"
                  clearable
                  :placeholder="__('请输入 Weixin Token')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                label="Weixin encodingaeskey"
                label-width="200px"
                prop="wechat_token"
              >
                <el-input
                  v-model="form.wechat_encodingaeskey"
                  clearable
                  :placeholder="__('请输入 Weixin encodingaeskey')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                label="Weixin App Id"
                label-width="200px"
                prop="wechat_app_id"
              >
                <el-input
                  v-model="form.wechat_app_id"
                  clearable
                  :placeholder="__('请输入 Weixin App Id')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                label="Weixin App Secret"
                label-width="200px"
                prop="wechat_app_secret"
              >
                <el-input
                  v-model="form.wechat_app_secret"
                  clearable
                  :placeholder="__('请输入 Weixin App Secret')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('公众号原始ID')"
                label-width="200px"
                prop="mp_id"
              >
                <el-input
                  v-model="form.mp_id"
                  clearable
                  :placeholder="__('请输入 公众号原始ID')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('小程序App Id')"
                label-width="200px"
                prop="wechat_xcx_app_id"
              >
                <el-input
                  v-model="form.wechat_xcx_app_id"
                  clearable
                  :placeholder="__('请输入 小程序App Id')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('小程序 App Secret')"
                label-width="200px"
                prop="wechat_xcx_app_secret"
              >
                <el-input
                  v-model="form.wechat_xcx_app_secret"
                  clearable
                  :placeholder="__('请输入 小程序 App Secret')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('微信关注链接')"
                label-width="200px"
                prop="wechat_suburl"
              >
                <el-input
                  v-model="form.wechat_suburl"
                  clearable
                  :placeholder="__('请输入 微信关注链接')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('是否必须绑定手机')"
                label-width="200px"
                prop="force_bind_mobile"
              >
                <el-radio-group v-model="form.force_bind_mobile" size="medium">
                  <el-radio
                    v-for="(item, index) in force_bind_mobileOptions"
                    :key="index"
                    :disabled="item.disabled"
                    :label="__(item.value)"
                  >
                    {{ __(item.label) }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item
                :label="__('是否开启')"
                label-width="200px"
                prop="wechat_status"
              >
                <el-radio-group v-model="form.wechat_status" size="medium">
                  <el-radio
                    v-for="(item, index) in wechat_statusOptions"
                    :key="index"
                    :disabled="item.disabled"
                    :label="__(item.value)"
                  >
                    {{ __(item.label) }}
                  </el-radio>
                </el-radio-group>
              </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: 'Index',
    components: {},
    data() {
      return {
        form: {
          wechat_callback_url: undefined,
          wechat_token: undefined,
          wechat_encodingaeskey: undefined,
          wechat_app_id: undefined,
          wechat_app_secret: undefined,
          mp_id: undefined,
          wechat_xcx_app_id: undefined,
          wechat_xcx_app_secret: undefined,
          wechat_suburl: undefined,
          force_bind_mobile: '',
          wechat_status: undefined,
        },
        rules: {
          wechat_callback_url: [
            {
              required: true,
              message: '请输入 Weixin Callback Url',
              trigger: 'blur',
            },
          ],
          wechat_token: [
            {
              required: true,
              message: '请输入 Weixin Token',
              trigger: 'blur',
            },
          ],
          wechat_encodingaeskey: [
            {
              required: true,
              message: '请输入 Weixin encodingaeskey',
              trigger: 'blur',
            },
          ],
          wechat_app_id: [
            {
              required: true,
              message: '请输入 Weixin App Id',
              trigger: 'blur',
            },
          ],
          wechat_app_secret: [
            {
              required: true,
              message: '请输入 Weixin App Secret',
              trigger: 'blur',
            },
          ],
          mp_id: [
            {
              required: true,
              message: '请输入 公众号原始ID',
              trigger: 'blur',
            },
          ],
          wechat_xcx_app_id: [
            {
              required: true,
              message: '请输入 小程序App Id',
              trigger: 'blur',
            },
          ],
          wechat_xcx_app_secret: [
            {
              required: true,
              message: '请输入 小程序 App Secret',
              trigger: 'blur',
            },
          ],
          wechat_suburl: [
            {
              required: true,
              message: '请输入 微信关注链接',
              trigger: 'blur',
            },
          ],
          force_bind_mobile: [
            {
              required: true,
              message: '是否必须绑定手机不能为空',
              trigger: 'change',
            },
          ],
          wechat_status: [
            {
              required: true,
              message: '是否开启不能为空',
              trigger: 'change',
            },
          ],
        },
        force_bind_mobileOptions: [
          {
            label: this.__('开启'),
            value: '1',
          },
          {
            label: this.__('关闭'),
            value: '0',
          },
        ],
        wechat_statusOptions: [
          {
            label: this.__('开启'),
            value: '1',
          },
          {
            label: this.__('关闭'),
            value: '2',
          },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      async fetchData() {
        const { data } = await getList()
        this.form = {
          wechat_callback_url: data.wechat_callback_url,
          wechat_token: data.wechat_token,
          wechat_encodingaeskey: data.wechat_encodingaeskey,
          wechat_app_id: data.wechat_app_id,
          wechat_app_secret: data.wechat_app_secret,
          mp_id: data.mp_id,
          wechat_xcx_app_id: data.wechat_xcx_app_id,
          wechat_xcx_app_secret: data.wechat_xcx_app_secret,
          wechat_suburl: data.wechat_suburl,
          force_bind_mobile: data.force_bind_mobile,
          wechat_status: data.wechat_status,
        }
      },
      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>
