import { API } from './../../../utils/network/api'
import { FileUtil } from './../../../utils/fileutil'
import { requestSubscribeMessage } from './../../../utils/requestSubscribeUtil';
const api = new API()
const app = getApp()
Page({
  data: {
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '多点执业认证', //导航栏 中间的标题
    },
    height: app.globalData.height,
    id_card_front: [],
    id_card_back: [],
    sign_image: [],
    btn_txt: "提交",
    multi_point_status: "",
    btn_disabled: true,
    sfz_show: false,
    sub_visible: false,
    static_host: api.getStaticHost()
  },

  onLoad(){
    console.log("onlosd");
    //获得多点执业信息
    api.getDoctorAuthMulti().then(response => {
      console.log("getDoctorAuthMulti: ",response);
      let multi_point_status = Number(response.data.multi_point_status);
      let btn_txt = this.data.btn_txt;
      let btn_disabled = this.data.btn_disabled;
      switch (multi_point_status) {
        case 0:
          btn_txt = "提交";
          btn_disabled = false;
          break;
        case 1:
          btn_txt = "审核通过";
          btn_disabled = true;
          break;
        case 2:
          btn_txt = "审核中";
          btn_disabled = true;
          break;
        case 3:
          btn_txt = "认证失败，再次提交";
          btn_disabled = false;
          break;
        default:
          btn_txt = "提交";
          btn_disabled = false;
          break;
      }
      if(response.data.id_card_front){
        let id_card_front_img = {};
        id_card_front_img.url = response.data.id_card_front;
        id_card_front_img.isImg = true;
        this.data.id_card_front.push(id_card_front_img);
      }

      if(response.data.id_card_back){
        let id_card_back_img = {};
        id_card_back_img.url = response.data.id_card_back;
        id_card_back_img.isImg = true;
        this.data.id_card_back.push(id_card_back_img);
      }

      let sign_image = response.data.sign_image;

      if(sign_image){
        let arr = [];
        let item = {};
        item.isImg = true;
        item.url = sign_image;
        arr.push(item);
        this.setData({
          sign_image: arr
        })
      }

      this.setData({
        id_card_front: this.data.id_card_front,
        id_card_back: this.data.id_card_back,
        btn_disabled: btn_disabled,
        btn_txt: btn_txt
      })
      
    }).catch(errors => {console.error(errors);})
  },
  onShow(){
    console.log("onshow")
    let sign_image = wx.getStorageSync("sign_image");
    console.log("sign_image: ",sign_image);
    if(sign_image){
      let arr = [];
      let item = {};
      item.isImg = true;
      item.url = sign_image;
      arr.push(item);
      this.setData({
        sign_image: arr
      })
    }
  },
  subcancelDialog(){
    this.setData({
      sub_visible: false
    })
    this.goadd()
  },
  subconfirmDialog(){
    this.setData({
      sub_visible: false
    })
    wx.openSetting({
      success (res) {
        console.log(res.authSetting)
      }
    })
  },
  add(){
    let tmplIds = ['XWfEQYtb8_ubz8pCs3GoCG6TFxpDz9jn52895yvDm3s','bGGoKtDZC23GZtrrxuy6i5V7OhHgRgwXWx4yoDO1tlA']
    let sub_promise = requestSubscribeMessage(tmplIds)
    sub_promise.then(res => {
      console.log("订阅成功")
      this.goadd()
    }).catch(err => {
      console.log(err)
      let errCode = err.errCode
      if(errCode == 20004){
        this.setData({
          sub_visible: true,
        })
      }
    })
  },
  goadd(){
    let _this = this;
    console.log(_this.data.id_card_front);
    console.log(_this.data.id_card_back);
    
    if(_this.data.id_card_front.length == 0){
      wx.showToast({
        title: '身份证正面为空',
        icon: "error"
      })
      return;
    }
    
    if(_this.data.id_card_back.length == 0){
      wx.showToast({
        title: '身份证反面为空',
        icon: "error"
      })
      return;
    }
    let id_card_front = _this.data.id_card_front[0].url;
    let id_card_back = _this.data.id_card_back[0].url;
    let sign_image = _this.data.sign_image[0].url;
    console.log("id_card_front: ", id_card_front);
    console.log("id_card_back: ", id_card_back);
    console.log("sign_image: ", sign_image);
    if(sign_image == ""){
      wx.showToast({
        title: '手写签名为空',
        icon: "error"
      })
      return;
    }
    let params = {};
    params.id_card_front = id_card_front;
    params.id_card_back = id_card_back;
    params.sign_image = sign_image;
    api.postDoctorAuthMulti(params).then(response => {
      console.log("postDoctorAuthMulti: ",response);
      wx.showToast({
        title: '提交成功',
        icon: "success"
      })
      _this.setData({
        btn_txt: "审核中",
        btn_disabled: true
      })
    }).catch(errors => {
      console.error(errors);
    })
  },
  doUploadFile(event) {
    console.log("index douploadFIle: ", event);
    const scene = event.currentTarget.dataset.scene;
    const field_name = event.currentTarget.dataset.field_name;
    const file_multiple = event.currentTarget.dataset.file_multiple;//是否为多张图
    console.log("scene: ", scene);
    console.log("field_name: ", field_name);
    console.log("file_multiple: ", file_multiple);
    let _this = this;
    api.getOssSign({user_type:2,scene:scene}).then(response => {
      console.log(response);
      const { file } = event.detail;
      console.log("file: ", file);
      const filename = FileUtil.getFileName(file);
      const host = response.data.host;
      const signature = response.data.signature;
      const ossAccessKeyId = response.data.accessid;
      const policy = response.data.policy;
      const key = response.data.dir+filename;
      const filePath = file.url; // 待上传文件的文件路径。
      wx.uploadFile({
        url: host, // 开发者服务器的URL。
        filePath: filePath,
        name: 'file', // 必须填file。
        formData: {
          key,
          policy,
          OSSAccessKeyId: ossAccessKeyId,
          signature,
        },
        success: (res) => {
          console.log("upload: ", res);
          if (res.statusCode === 204) {
            wx.showToast({title: '上传成功'})
            if(file_multiple == "true"){//多张图
              let img = {}
              img.url = host+"/"+key;
              img.isImg = true;

              
              let img_list = [];

              console.log("img_list: ", img_list);
              img_list.push(img);
              console.log("img_list: ", img_list);

              this.setData({
                [field_name]: img_list
              });
            }else{
              this.setData({[field_name]: host+"/"+key});
            }
          }
        },
        fail: err => {
          console.log(err);
        }
      });
    }).catch(errors => {
      console.error(errors);
    })
  },
  doDeleteFile(event){
    if(this.data.btn_disabled){
      wx.showToast({
        title: '不能修改',
        icon: "error"
      })
      return;
    }
    console.log(event);
    let url = event.detail.file.url;
    let file_name = event.currentTarget.dataset.field_name;
    let img_list = this.data[file_name].filter(item => item.url != url);
    this.setData({
      [file_name]: img_list
    })
  },
  onshowIdCardFrontNote(){
    console.log("onshowIdCardFrontNote");
    this.setData({
      sfz_show: true
    })
  },
  onshowIdCardBackNote(){
    console.log("onshowIdCardBackNote");
    this.setData({
      sfz_show: true
    })
  },
  onsfzClose(){
    console.log("onsfzClose");
    this.setData({
      sfz_show: false
    })
  },
  go(e){
    // if(this.data.btn_disabled){
    //   wx.showToast({
    //     title: '不能修改',
    //     icon: "error"
    //   })
    //   return;
    // }
    let url = e.currentTarget.dataset.url;
    app.go(url)
  }
})