import {
  observable,
  action
} from 'mobx';

import {
	fetch,
	getTopButtons,
	getDropMenuDatas,
	getMenusCreationConfig,
	componentReset,
	exportExcel,
} from '../../hrmAttendance/util/save-time-util.js';

import * as asyncFetch from '../apis/outside.js';

import {
  WeaTools
} from 'ecCom';
import moment from 'moment';
import * as API from '../apis/outside';
import isEmpty from 'lodash/isEmpty';
import {
  WeaTableNew
} from 'comsMobx';
import {
  i18n
} from '../public/i18n';
import {
  WeaLocaleProvider
} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const {
  TableStore
} = WeaTableNew;

export class HrmOutside {
  title = () => getLabel('82634', "外勤签到");
  hrmId = undefined;
  leftTitle = () => getLabel('32997', "人员组织");
  tabDatas = () => [{
    title: getLabel('387101', "时间视图"),
    key: "1"
  }, {
    title: getLabel('387102', "地图视图"),
    key: "2"
  }, {
    title: getLabel('387103', "明细"),
    key: "3"
  }];
  dateTabDatas = () => [{
    title: i18n.label.today(),
    key: "1"
  }, {
    title: i18n.label.yesterday(),
    key: "2"
  }, {
    title: i18n.label.tsmth(),
    key: "3"
  }];
  dateTabDatas1 = () => [{
    title: i18n.label.today(),
    key: "1"
  }, {
    title: i18n.label.yesterday(),
    key: "2"
  }];

  signTitle = () => getLabel('386501', "同时显示移动端考勤记录");
  @observable showSignInfo = '0';
  @observable pDate = new moment().format('YYYY-MM-DD');
  @observable dateTabkey = '1';
  @observable hrmName = WeaTools.ls.getJSONObj('theme-account') && WeaTools.ls.getJSONObj('theme-account').username || undefined;
  @observable loading = false;
  @observable userRoot = undefined;
  @observable hasRight = true;
  @observable tabkey = '1';
  @observable visible = false;
  @observable positionObj = {};
  @observable mapData = [];
  @observable timeData = [];
  @observable table = new TableStore();
  @observable dataKey = "";
  @observable orgTreeIsMult = '0';
  @observable orgTreeCheckedDatas = [];
  @observable radioGroupParams = {};


  @observable config = [];

  @action getRadioGroupConfig() {
		fetch({
			asyncFetch,
			name: 'getSearchCondition',
			logic: (cb) => {
				const {
					condition,
					value,
					option,
				} = cb;

        const configfirstrow = {
          label: i18n.label.date(),
          options: [{
            key: '0',
            showname: i18n.label.all()
          }, {
            key: '1',
            showname: i18n.label.today(),
            selected: true
          }, {
            key: '2',
            showname: i18n.label.tswk()
          }, {
            key: '3',
            showname: i18n.label.tsmth()
          }, {
            key: '4',
            showname: i18n.label.tssn()
          }, {
            key: '5',
            showname: i18n.label.tsyr()
          }, {
            key: '7',
            showname: i18n.label.preMonth()
          }, {
            key: '8',
            showname: i18n.label.preYear()
          }, {
            key: '6',
            showname: i18n.label.dateRange()
          }, ],
          domkey: ['typeselect'],
          selectLinkageDatas: {
            '6': {
              conditionType: 'RANGEPICKER',
              domkey: ['fromDate', 'toDate'],
            }
          },
          labelcol: 4,
          fieldcol: 20,
        }
    
        condition[0].items.unshift(configfirstrow);
        
				this.config = condition[0].items;

			}
		});
	}


  
  @action onRadioGroupChange = params => {
    // this.radioGroupVal = params;
    switch (params.dataScope) {
      case '1':
        if (params.subcomId == null || params.subcomId == '') {
          return;
        }
        break;
      case '2':
        if (params.deptId == null || params.deptId == '') {        
          return;
        }
        break;
      case '3':
        if (params.resourceId == null || params.resourceId == '') {
          return;
        }
        break;
    }
    this.getData(params);
  }


//   config = () => [{
//     label: i18n.label.date(),
//     options: [{
//       key: '0',
//       showname: i18n.label.all()
//     }, {
//       key: '1',
//       showname: i18n.label.today(),
//       selected: true
//     }, {
//       key: '2',
//       showname: i18n.label.tswk()
//     }, {
//       key: '3',
//       showname: i18n.label.tsmth()
//     }, {
//       key: '4',
//       showname: i18n.label.tssn()
//     }, {
//       key: '5',
//       showname: i18n.label.tsyr()
//     }, {
//       key: '7',
//       showname: i18n.label.preMonth()
//     }, {
//       key: '8',
//       showname: i18n.label.preYear()
//     }, {
//       key: '6',
//       showname: i18n.label.dateRange()
//     }, ],
//     domkey: ['typeselect'],
//     selectLinkageDatas: {
//       '6': {
//         conditionType: 'RANGEPICKER',
//         domkey: ['fromDate', 'toDate'],
//       }
//     },
//   }, {
//     label: i18n.label.kqType(),
//     options: [{
//       key: '0',
//       showname: i18n.label.all()
//     }, {
//       key: '2',
//       showname: getLabel('82634', "外勤签到"),
//       selected: true
//     }, {
//       key: '1',
//       showname: getLabel('386502', "移动端考勤")
//     }, ],
//     domkey: ['signtype'],
//   }
//   , {
//     label: 'test',
//     options: [{
//       key: '0',
//       showname: i18n.label.all(),
//       selected: true
//     }, {
//       key: '1',
//       showname: '分部',
//     }, {
//       key: '2',
//       showname: '部门',
//     }, {
//       key: '3',
//       showname: '人员',
//     } ],
//     selectLinkageDatas: {
//       '1': {
//         conditionType: 'BROWSER',
//         browserConditionParam: { type: 164, title:'分部', ...WeaTools.getIconBGC('hrm'), viewAttr: 3 },
//         domkey: ['subcompanyid1'],
//       },
//       '2': {
//         conditionType: 'BROWSER',
//         browserConditionParam: { type: 4, title:'部门', ...WeaTools.getIconBGC('hrm'), viewAttr: 3 },
//         domkey: ['departmentid'],
//       },
//       '3': {
//         conditionType: 'BROWSER',
//         browserConditionParam: { type: 1, title:'人员', ...WeaTools.getIconBGC('hrm'), viewAttr: 3 },
//         domkey: ['resourceid'],
//       }
//     },
//     domkey: ['targetid'],
//   }
// ]

  @action
  init() {
    this.hrmId = undefined;
    this.showSignInfo = '0';
    this.tabkey = '1';
    this.dateTabkey = '1';
    this.visible = false;
    this.pDate = new moment().format('YYYY-MM-DD');
    this.hrmName = WeaTools.ls.getJSONObj('theme-account') && WeaTools.ls.getJSONObj('theme-account').username || undefined;
    this.userRoot = undefined;
    this.orgTreeIsMult = '0';
    this.orgTreeCheckedDatas = [];
  }

  @action
  updateOrgTreeIsMult(v) {
    this.orgTreeIsMult = v;
  }

  @action
  updateOrgTreeCheckedDatas(v = []) {
    this.orgTreeCheckedDatas = v;
  }

  @action
  updatePosition(obj) {
    this.positionObj = obj;
  }

  @action
  updateVisible(v) {
    this.visible = v;
  }

  @action
  handlerCheckbox(v) {
    this.showSignInfo = v;    
    // this.getData();
  }

  @action
  changeTab(key) {
    this.tabkey = key;
    this.dateTabkey = '1';
    this.pDate = new moment().format('YYYY-MM-DD');
    this.showSignInfo = '0';
    if(this.tabkey==3){     
      this.getRadioGroupConfig();
    }
     
  }

  @action
  changeDate(v1, v2) {
    this.pDate = v2;
  }

  changeDateTab(key) {
    this.dateTabkey = key;
    const current = new moment();
    if (key == 1) {
      this.pDate = current.format('YYYY-MM-DD');
    } else if (key == 2) {
      this.pDate = current.subtract(1, 'days').format('YYYY-MM-DD');
    } else {
      this.pDate = current.format('YYYY-MM');
    }
  }

  @action
  updateUserRoot(data) {
    this.userRoot = data;
  }

  @action
  updateHrmId(v) {
    this.hrmId = v;
  }

  @action
  updateHrmName(v) {
    this.hrmName = v || WeaTools.ls.getJSONObj('theme-account') && WeaTools.ls.getJSONObj('theme-account').username || undefined;
  }

  @action
  exportExcel(dataKey) {
    //this.table.exportAll('',this.title);
    this.table.exportAll();
  }




  getData(params = {}) {
    if (this.hrmId) params.resourceId = this.hrmId;
    params.loaddata = "1";
    this.loading = true;
    if (this.tabkey == '3') {      
      params = {
         ...params,
        ...this.radioGroupParams
      };
      if (!params.typeselect || params.typeselect == '') params.typeselect = 1;
      if (!params.signtype || params.signtype == '') params.signtype = 2;
      params.cmd = 'detialData';
    } else {
      params.fromDate = this.pDate;
      params.toDate = this.pDate;
      params.signtype = this.showSignInfo;
      if (this.tabkey == '1') {
        params.cmd = 'timeData';
      } else if (this.tabkey == '2') {
        params.cmd = 'mapData';
      }
    }
   

    API.getHrmMobileSignInInfo(params).then((datas) => {
      if (datas.status == '1') {
        this.hasRight = true;
        if (this.tabkey == '1') {
          this.timeData = datas.result || [];
        } else if (this.tabkey == '2') {
          this.mapData = datas.result || [];
        } else if (this.tabkey == '3') {
          this.table.getDatas(datas.sessionkey, 1);
          this.dataKey = datas.sessionkey;
        }
      } else {
        this.hasRight = false;
      }
      this.loading = false;
    })     
  }

  handleViewClick = (id) => {
    API.getShowSignImg({
      id
    }).then(datas => {
      const {
        signImgIds
      } = datas;

      if (signImgIds) {
        const imgPool = signImgIds.split(',').map(id => `/weaver/weaver.file.FileDownload?fileid=${id}`);
        const indexNum = 0;

        if (window.IMCarousel) {
          window.IMCarousel.showImgScanner4Pool(true, imgPool, indexNum, null, window.top);
        } else {
          loadjs(["/social/js/drageasy/drageasy.js", "/social/js/bootstrap/js/bootstrap.js?v=20171218", "/social/im/js/IMUtil_wev8.js", "/social/js/imcarousel/imcarousel.js"], () => {
            window.IMCarousel.showImgScanner4Pool(true, imgPool, indexNum, null, window.top);
          });
        }
      }

    })
  }
}