<template>
  <div>
    <el-col :span="16">
      <el-card style="height: 650px">
        <div class="send-person">消息推送设定</div>
        <el-divider style="margin: 0;width: 420px"/>
        <el-form ref="form1" :model="form1" :rules="rules1" label-position="left" label-width="120px">
          <el-form-item label="corpid：" prop="corpid">
            <el-input :rows="2" v-model="form1.corpid" type="textarea" style="width: 340px;border-radius: 4px" clearable placeholder="请输入corpid"/>
            <el-tooltip class="item" effect="dark" placement="right">
              <div slot="content">
                每个企业都拥有唯一的corpid，获取此信息可在管理后台
                <br>
                “我的企业”－“企业信息”下查看“企业ID”（需要有管理员权限）!
              </div>
              <div class="tips-weChat" />
            </el-tooltip>
          </el-form-item>
          <el-form-item label="secret：" prop="secret">
            <el-input :rows="2" v-model="form1.secret" type="textarea" style="width: 340px;border-radius: 4px" clearable placeholder="请输入secret"/>
            <el-tooltip class="item" effect="dark" placement="right">
              <div slot="content">
                在管理后台->“应用与小程序”->“应用”->“自建”，
                <br>
                点进某个应用，即可看到!
              </div>
              <div class="tips-weChat" />
            </el-tooltip>
          </el-form-item>
          <el-form-item label="agentid：" prop="agentid">
            <el-input :rows="2" v-model="form1.agentid" type="textarea" style="width: 340px;border-radius: 4px" clearable placeholder="请输入agentid"/>
            <el-tooltip class="item" effect="dark" placement="right">
              <div slot="content">
                每个应用都有唯一的agentid。
                <br>
                在管理后台->“应用与小程序”->“应用”，
                <br>
                点进某个应用，即可看到agentid。
              </div>
              <div class="tips-weChat" />
            </el-tooltip>
          </el-form-item>
          <el-form-item label="touser:" prop="touser">
            <el-input :rows="2" v-model="form1.touser" type="textarea" style="width: 340px;border-radius: 4px" clearable placeholder="请输入touser"/>
            <el-tooltip class="item" effect="dark" placement="right">
              <div slot="content">
                指定接收消息的成员，成员ID列表
                <br>
                多个接收者用‘|’分隔，最多支持1000个。
                <br>
                默认为"@all"
              </div>
              <div class="tips-weChat" />
            </el-tooltip>
          </el-form-item>
          <el-form-item label="toparty:" prop="toparty">
            <el-input :rows="2" v-model="form1.toparty" type="textarea" style="width: 340px;border-radius: 4px" clearable placeholder="请输入toparty"/>
            <el-tooltip class="item" effect="dark" placement="right">
              <div slot="content">
                指定接收消息的部门，部门ID列表，
                <br>
                多个接收者用‘|’分隔，最多支持100个。
                <br>
                默认为"@all"
              </div>
              <div class="tips-weChat" />
            </el-tooltip>
          </el-form-item>
          <el-form-item label="启用推送" prop="used">
            <el-radio-group v-model="form1.used">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-row>
            <el-col :span="14"/>
            <el-form-item>
              <el-button style="width: 80px;height: 40px;margin-left: 50px" type="primary" @click="submit1('form1')">确 定</el-button>
            </el-form-item>
          </el-row>
        </el-form>
      </el-card>
    </el-col>
    <el-col :span="7"/>
  </div>
</template>

<script>
export default {
  name: 'SendWeChat',
  data() {
    return {
      form1: {
        corpid: '',
        secret: '',
        agentid: '',
        touser: '',
        toparty: '',
        used: 0
      },
      rules1: {
        agentid: [{ required: true, message: '请输入agentid', trigger: 'blur' }],
        touser: [{ required: true, message: '请输入touser', trigger: 'blur' }],
        toparty: [{ required: true, message: '请输入toparty', trigger: 'blur' }],
        corpid: [{ required: true, message: '请输入corpid', trigger: 'blur' }],
        secret: [{ required: true, message: '请输入secret', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.searchTable1()
  },
  methods: {
    // 查询设定信息
    searchTable1() {
      var param = []
      param[0] = ['pushType', 2]
      var Data = this.CreateData('11', '基础数据_推送管理_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          let info = JSON.parse(response.data[0].pushSetting)
          if (!info) info = {}
          this.form1.ID = response.data[0].ID
          this.form1.corpid = Object.hasOwnProperty.call(info, 'corpid') ? info['corpid'] : ''
          this.form1.secret = Object.hasOwnProperty.call(info, 'secret') ? info['secret'] : ''
          this.form1.agentid = Object.hasOwnProperty.call(info, 'agentid') ? info['agentid'] : ''
          this.form1.touser = Object.hasOwnProperty.call(info, 'touser') ? info['touser'] : '@all'
          this.form1.toparty = Object.hasOwnProperty.call(info, 'toparty') ? info['toparty'] : '@all'
          this.form1.used = response.data[0].used === 1 ? 1 : 0
        } else {
          this.form1.ID = ''
          this.form1.corpid = ''
          this.form1.secret = ''
          this.form1.agentid = ''
          this.form1.touser = '@all'
          this.form1.toparty = '@all'
          this.form1.used = 0
        }
      })
    },
    // 增加
    submit1(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          const pushSetting = {
            corpid: this.form1.corpid,
            secret: this.form1.secret,
            agentid: this.form1.agentid,
            touser: this.form1.touser,
            toparty: this.form1.toparty
          }
          param[0] = ['ID', this.form1.ID]
          param[1] = ['pushSetting', JSON.stringify(pushSetting)]
          param[2] = ['opName', '企业微信']
          param[3] = ['used', this.form1.used]
          var Data = this.CreateData('11', '基础数据_推送管理_编辑', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === 1) {
              this.$message({
                message: '信息编辑成功',
                type: 'success'
              })
              this.searchTable1()
            } else {
              this.$message.error('信息编辑失败！')
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
  .send-person {
    font-size: 22px;
    font-family: "MiSans Demibold",serif;
    text-align: center;
    margin-bottom: 12px;
  }
  .tips-weChat {
    display: inline-block;
    background-image: url(../../../assets/img/tips.png);
    width: 15px;
    height: 15px;
  }
</style>
