import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { AppService } from "../../../service/appHttpService";
import { Geolocation } from "@ionic-native/geolocation";
import * as moment from "moment";

declare let AMap;
@IonicPage()
@Component({
  selector: 'page-duty-sign-up',
  templateUrl: 'duty-sign-up.html',
})
export class DutySignUpPage {

    id;
    signState;
    signStatus = 'fail';

    //市委组织部坐标
    mainLat = 31.205033;   //纬度
    // mainLat = 31.231871;   //纬度test    31.231871, 121.393319
    mainLng = 121.444888;  //经度
    // mainLng = 121.393319;  //经度test
    nowDay;
  constructor(public navCtrl: NavController, public navParams: NavParams,
              public appService: AppService, public geolocation: Geolocation) {
  }

  ionViewDidLoad() {

      this.nowDay = moment().format('l');
      console.log(this.nowDay);

    this.id = this.navParams.get('id');
    const data = {
      'id':this.id
    };
   this.appService.ObserverHttpGetOption('/wisdomgroup/schedule/hasSignIn',data)
       .subscribe((res)=>{
         this.signState = res.json();
       })
  }

  //签到
    signIn(){
      const data = {
        id:this.id,  //值班记录id,
        address:'上海市委组织部'   //签到地址
      };
      this.appService.ObserverHttpGet('/wisdomgroup/schedule/signIn',data)
          .subscribe((res)=>{
            console.log(res);
      })
  }

  //签退
    signBack(){
      this.calcuDistance();
        // const data = {
        //     id:this.id,  //值班记录id,
        //     address:'上海市委组织部'   //签到地址
        // };
        // this.appService.ObserverHttpGet('/wisdomgroup/schedule/signIn',data)
        //     .subscribe((res)=>{
        //         console.log(res);
        //     })
    }

  //计算距离
  calcuDistance(){
      this.geolocation.getCurrentPosition().then((resp) => {
          console.log(resp.coords.latitude);
          console.log(resp.coords.longitude);
          const distance = this.getDisance(this.mainLat,this.mainLng,resp.coords.latitude,resp.coords.longitude);
          console.log(distance);
          alert('距离：'+distance);
      }).catch((error) => {
          console.log(error);
      });
  }

    getDisance(lat1, lng1, lat2, lng2){
        let radLat1 = this.toRad(lat1);
        let radLat2 = this.toRad(lat2);
        let deltaLat = radLat1 - radLat2;
        let deltaLng = this.toRad(lng1) - this.toRad(lng2);
        let dis = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(deltaLat / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(deltaLng / 2), 2)));
        dis = dis * 6378137;
        dis = Math.round(dis * 10) / 10;
        return dis;
    }

    toRad(d) {
      return d * Math.PI / 180;
    }

}
