import {Component, OnInit} from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap/modal';
import {CookieService} from 'ngx-cookie-service';
import {Router} from '@angular/router';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { MsgService } from '../../../../../core/service/msg.service';
import { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';

@Component({
  selector: 'app-exam-video-notice-send',
  templateUrl: './exam-video-notice-send.component.html',
  styleUrls: ['./exam-video-notice-send.component.scss']
})
export class ExamVideoNoticeSendComponent extends FrameDetailComponent implements OnInit {
  wxTemplate: any;

  constructor(bsModalRef: BsModalRef) {
    super(bsModalRef);
  }

  initDefautEntity() {
    this.ResponseMap = {};
  }

  ngOnInit() {
  }

  changeTemplate() {
    if (this.currentEntity.template_id === undefined)
      return;
    this.ResponseMap = {};
    this.currentEntity.test_flag = 'InActive';
    this.currentEntity.real_sql = '';
    AjaxService.requestEntity('weixin/template/find.htm', {template_id: this.currentEntity.template_id},
      (entity) => {
        this.wxTemplate = entity;
      });
  }

  serachWxUser() {
    this.ResponseMap = {};
    this.currentEntity.test_flag = 'InActive';
    this.currentEntity.confirm_flag = 'InActive';
    this.currentEntity.real_sql = '';
    AjaxService.requestMap('weixin/template/user/list.htm', {sql: encodeURIComponent(this.currentEntity.execute_sql)}, (map) => {
      if (!map.UserTotal || map.UserTotal === 0) {
        MsgService.warningToastr('没有找到任何目标用户！');
        return;
      }
      this.currentEntity.real_sql = encodeURIComponent(this.currentEntity.execute_sql);
      this.ResponseMap = map;
      this.currentEntity.json_value = JSON.stringify(this.ResponseMap.UserArray[0], null, 1);
    });
  }
  confirmMsg(flag) {
    this.currentEntity.confirm_flag = flag;
  }
  goSendTest() {
    const wx = Object.assign({}, this.wxTemplate);
    this.replaceValues(wx, this.ResponseMap['UserArray'][0]);
    MsgService.confirmAlert('确定要发送测试消息吗？请留意测试微信号', () => {
      AjaxService.request('weixin/template/send/test.htm', wx, (map) => {
        this.currentEntity.test_flag = 'Active';
      });

    })
  }

  sendAll() {
    const wx = Object.assign({}, this.wxTemplate);
    wx.sql = this.currentEntity.real_sql;
    MsgService.confirmAlert('确定要发送给【'+ this.ResponseMap.UserTotal +'】个考生发送微信消息？该操作不可回退，务必谨慎操作！', () => {
      AjaxService.request('weixin/template/send.htm',wx, () => {
        MsgService.successAlert('已提交到后台处理！')
        this.ResponseMap = {};
        this.currentEntity.test_flag = 'InActive';
        this.currentEntity.confirm_flag = 'InActive';
        this.currentEntity.real_sql = '';
      },{RequestWithLoading :true});

    })
  }

  replaceValues(wx, obj) {
    for (const name in obj) {
      let value = obj[name];
      // 替换
      if (wx.template_url)
        wx.template_url = wx.template_url.replace(`@{${name}}`, value);
      if (wx.content_value)
        wx.content_value = wx.content_value.replace(`@{${name}}`, value);
      if (wx.keyword1_value)
        wx.keyword1_value = wx.keyword1_value.replace(`@{${name}}`, value);
      if (wx.keyword2_value)
        wx.keyword2_value = wx.keyword2_value.replace(`@{${name}}`, value);
      if (wx.keyword3_value)
        wx.keyword3_value = wx.keyword3_value.replace(`@{${name}}`, value);
      if (wx.keyword4_value)
        wx.keyword4_value = wx.keyword4_value.replace(`@{${name}}`, value);
      if (wx.keyword5_value)
        wx.keyword5_value = wx.keyword5_value.replace(`@{${name}}`, value);
      if (wx.remark_value)
        wx.remark_value = wx.remark_value.replace(`@{${name}}`, value);
    }

  }
}
