<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="__('离线回复')"
                label-width="150px"
                prop="im_offline_msg"
              >
                <el-input
                  v-model="form.im_offline_msg"
                  clearable
                  :placeholder="
                    __('当接收信息用户离线时，自动回复消息内容！离线回复')
                  "
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('IM账号')"
                label-width="150px"
                prop="site_im"
              >
                <el-input
                  v-model="form.site_im"
                  clearable
                  :placeholder="
                    __('请输入用户编号')
                  "
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item :label="__('启用IM系统')" label-width="150px" prop="im_enable">
                <el-radio v-model="form.im_enable" :label="0">
                  {{ __('否') }}
                </el-radio>
                <el-radio v-model="form.im_enable" :label="1">
                  {{ __('是') }}
                </el-radio>
              </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: 'Im',
    components: {},
    data() {
      return {
        form: {
          im_offline_msg: undefined,
          site_im: undefined,
          im_enable: undefined,
        },
        rules: {
          im_offline_msg: [
            {
              required: true,
              message: '当接收信息用户离线时，自动回复消息内容！离线回复',
              trigger: 'blur',
            },
          ],
          site_im: [
            {
              required: true,
              message: '请输入用户编号',
              trigger: 'blur',
            },
          ],
          im_enable: [
            {
              required: true,
              message: '请选择是否启用',
              trigger: 'change',
            },
          ],
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      async fetchData() {
        const { data } = await getList()
        this.form.im_offline_msg = data.im_offline_msg
        this.form.site_im = data.site_im
        this.form.im_enable = parseInt(data.im_enable)
      },
      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>
