import { Component, OnInit } from '@angular/core';
import {PickerController} from '@ionic/angular';

@Component({
  selector: 'v-env-cfg',
  templateUrl: './env-cfg.page.html',
  styleUrls: ['./env-cfg.page.scss'],
})
export class EnvCfgPage implements OnInit {

    // 工作模式
    workModeSetting = {  'workMode' : '1',
        'DeviceNo' : '',
        'workModeOld' : '1'};

    defaultColumnOptions = [];

    fanMode = {
        'selecting' : false,
        }

    light = {
        'selecting' : false,
    }

    fanCfg = [{
        'name':'模式一',
        'timeInterval':25,
        'tempSet':25,
        'tempOffset':3,
        'ATmr':20,
        'BTmr':20,
    },{
        'name':'模式二',
        'timeInterval':25,
        'tempSet':30,
        'tempOffset':3,
        'ATmr':20,
        'BTmr':20,
    },{
        'name':'模式三',
        'timeInterval':25,
        'tempSet':20,
        'tempOffset':3,
        'ATmr':20,
        'BTmr':20,
    },]

    lightCfg= [{
        'name':'模式一',
        'timeInterval':25,
        'startTime':25,
        'endTime':3,
        'onTime':20,
        'offTime':20,
    },{
        'name':'模式二',
        'timeInterval':25,
        'startTime':30,
        'endTime':3,
        'onTime':20,
        'offTime':20,
    },]

  constructor(private pickerController:PickerController) { }

    async openPicker(numColumns = 1, numOptions = 5, columnOptions = this.defaultColumnOptions){
        const picker = await this.pickerController.create({
            columns: this.getColumns(numColumns, numOptions, columnOptions),
            buttons: [
                {
                    text: 'Cancel',
                    role: 'cancel',
                    handler: ()=> {
                        this.fanMode.selecting = false;
                    }
                },
                {
                    text: 'Confirm',
                    handler: (value) => {
                        console.log(`Got Value ${value}`);
                        this.fanMode.selecting = false;
                    }
                }
            ]
        });
        await picker.present();
    }

    getColumns(numColumns, numOptions, columnOptions) {
        let columns = [];
        for (let i = 0; i < numColumns; i++) {
            columns.push({
                name: `col-${i}`,
                options: this.getColumnOptions(i, numOptions, columnOptions)
            });
        }
        return columns;
    }

    getColumnOptions(columnIndex, numOptions, columnOptions) {
        let options = [];
        for (let i = 0; i < numOptions; i++) {
            options.push({
                text: columnOptions[columnIndex][i % numOptions],
                value: i
            })
        }
        return options;
    }

    ngOnInit() {
        // this.pickerController = document.querySelector('ion-picker-controller');
        this.defaultColumnOptions = [
            [
                '1',
                '2',
                '3',
                '4',
                '5'
            ]
        ];
  }

    workModeEvent() {
        // if ( this.workModeSetting.workModeOld === this.workModeSetting.workMode ) {
        //     return;
        // }
        //
        // const opt = {fieldDeviceNo : this.workModeSetting.DeviceNo ,
        //     op : this.workModeSetting.workMode ,
        //     startTime: parseInt((new Date().getTime() / 1000).toString(), 10),
        //     ts: parseInt((new Date().getTime() / 1000).toString(), 10)
        // };
        //
        // opt.startTime = opt.ts;
        //
        // this.api.control.controlSwitch([opt, (data) => {
        //     if (data && data.code === 1) {
        //         this.getData(1);
        //         this.api.presentMsgToast(data.message);
        //     } else {
        //         this.api.presentMsgToast(data.message);
        //     }
        // }]);
    }

    async openCalendarPicker(numColumns = 1, numOptions = 5, columnOptions = this.defaultColumnOptions){
        const picker = await this.pickerController.create({
            columns: this.getColumns(numColumns, numOptions, columnOptions),
            buttons: [
                {
                    text: 'Cancel',
                    role: 'cancel',
                    handler: ()=> {
                        this.fanMode.selecting = false;
                    }
                },
                {
                    text: 'Confirm',
                    handler: (value) => {
                        console.log(`Got Value ${value}`);
                        this.fanMode.selecting = false;
                    }
                }
            ]
        });
        await picker.present();
    }

    fanTempHaveFouch(){
      console.log('fanTempHaveFouch');
      if(this.fanMode.selecting === false)
      {
          this.fanMode.selecting = true;
          this.openPicker();
      }
    }

    lightTimeHaveFouch(){
        console.log('lightTimeHaveFouch');
        if(this.light.selecting === false)
        {
            this.light.selecting = true;
            this.openPicker();
        }
    }

}
