<template>
  <el-form :model="siteForm" ref="siteForm" label-width="220px" v-loading="loading" style="width: 550px">
    <el-form-item label="企业ID：">
      <el-input v-model="siteForm.corp_id"></el-input>
    </el-form-item>
    <el-form-item label="自建应用Secret：">
      <el-input v-model="siteForm.self_secret"></el-input>
    </el-form-item>
    <el-form-item label="基础应用Secret：">
      <el-input v-model="siteForm.base_secret"></el-input>
    </el-form-item>
    <el-form-item label="通讯录管理Secret：">
      <el-input v-model="siteForm.contacts_secret"></el-input>
    </el-form-item>
    <el-form-item label="外部联系人管理Secret：">
      <el-input v-model="siteForm.external_contact_secret"></el-input>
    </el-form-item>
    <el-form-item label="客户联系-接收事件服务器：" style="margin-bottom: 0"></el-form-item>
    <el-form-item label="Token">
      <el-input v-model="siteForm.token"></el-input>
    </el-form-item>
    <el-form-item label="EncodingAESKey">
      <el-input v-model="siteForm.encoding_aeskey"></el-input>
    </el-form-item>
    <el-form-item label="买家端小程序AppID：">
      <el-input v-model="siteForm.buyer_mpapp_id"></el-input>
    </el-form-item>
    <el-form-item label="导购端小程序AppID：">
      <el-input v-model="siteForm.guide_mpapp_id"></el-input>
    </el-form-item>
    <el-form-item label="智慧导购应用ID：">
      <el-input v-model="siteForm.agent_id"></el-input>
    </el-form-item>
    <el-form-item label="企业微信通知URL：" style="width: max-content">
      <div style="display: flex">
        <el-input :value="notify_url" disabled>
          <el-button
            slot="append"
            v-clipboard:copy="notify_url"
            v-clipboard:error="() => $message.error('复制失败，请手动复制！')"
            v-clipboard:success="() => $message.success('复制成功！')"
          >复制</el-button>
        </el-input>
      </div>
      <p style="margin: 0;color:#999;">请将此链接复制到：企业微信后台 => 客户联系 => 接收事件服务器 => URL字段</p>
    </el-form-item>
    <el-form-item label="">
      <el-button type="primary" @click="submitForm">保存设置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import * as API_SystemSetting from '@/api/systemSetting'
  import { api } from '~/ui-domain'

  export default {
    name: 'systemSettingsQiyewx',
    data() {
      return {
        loading: true,
        /** 企业微信设置 */
        siteForm: {}
      }
    },
    created() {
      API_SystemSetting.getQiyewxSetting().then(response => {
        this.loading = false
        this.siteForm = response
      }).catch(() => { this.loading = false })
    },
    computed: {
      notify_url() {
        return `${api.admin}/weixin/contact-notify-receiver`
      }
    },
    methods: {
      submitForm() {
        this.$refs['siteForm'].validate((valid) => {
          if (valid) {
            const params = this.MixinClone(this.siteForm)
            API_SystemSetting.editQiyewxSetting(params).then(() => {
              this.$message.success('修改成功！')
            })
          } else {
            this.$message.error('表单填写有误，请检查！')
            return false
          }
        })
      }
    }
  }
</script>
