import * as API from '../../apis/autographApi';
import { observable, action } from 'mobx';

export default class AutographStore {
  @observable initInfo = [];
  @observable isMustInput = '';
  @observable isAutoResizeSignImage = true;
  @observable recordId = '0';
  @observable visible = true;

  @action
  setMobxState = (params = {}) => {
    Object.keys(params).forEach((v) => {
      this[v] = params[v];
    });
  }
  @action
  getAutographInfo = params => new Promise((resolve) => {
      API.getAutographInfo(params).then((res) => {
        this.initInfo = res;
        resolve(res);
      });
    })

  @action
  saveAutograph = (src) => {
    const isMustInput = this.isMustInput;
    if (isMustInput === '1' || (isMustInput === '2' && src === 'reject')) {
      if (Consult.DocEmpty && Consult.OpinionText == '') {
        return false;
      }
    }
    // TODO save
    let record = '';
    const timestamp = Date.parse(new Date()) / 1000;
    if (Consult.Modify) {
      let saveFlag = this.isAutoResizeSignImage ? Consult.SaveAsGifEx(`${timestamp}.gif`, 'All', 'Remote') : Consult.SaveSignature();
      if (!saveFlag) {
        return false;
      }
      record = Consult.WebGetMsgByName('RECORDID');
      if (parseInt(record) > 0 && this.isAutoResizeSignImage) {
        Consult.WebSetMsgByName('TEMPCALL', '1');
        Consult.WebSetMsgByName('TEMPID', record);
        saveFlag = Consult.SaveSignature();
      }
    } else {
      return this.recordId;
    }
    return record;
  }
}
