// pages/gate/visitor/index.js
import HTTP from "../../../../requestFn/Api"
import {
  timestamp
} from "../../utils/util"
var dateTimePicker = require("../../utils/dateTimePicker")
const app = getApp()
Page({

  data: {
    gate_list: [],
    garden_id: null,
    device_id: null,
    value: "",
    dateTimeArray1: null,
    dateTime1: null,
    dateTimeArray2: null,
    dateTime2: null,
    date: null,
  },
  onLoad(options) {
    const that = this;
    const {
      garden_id,
      garden_name
    } = app.globalData;
    if (garden_id) {
      that.setData({
        garden_id,
        garden_name
      })
    }
    that.getGate()
      // 获取完整的年月日 时分秒，以及默认显示的数组
      var obj1 = dateTimePicker.dateTimePicker();
      const dateTime3 = obj1.dateTime.map((item, index) => {
        if (index == 2) {
          return item + 1
        } else {
          return item
        }
      })
      this.setData({
        dateTimeArray1: obj1.dateTimeArray,
        dateTime1: obj1.dateTime,
        date: obj1.dateTimeArray[2][0],
        dateTimeArray2: obj1.dateTimeArray,
        dateTime2: dateTime3,
      });
  },
  bindPickerChange(e) {
    const value = e.detail.value
    this.setData({
      value,
      device_id: this.data.gate_list[value].device_id,
      gate_name: this.data.gate_list[value].name,
      garden_name: this.data.gate_list[value].garden_name,
    });
  },
  //获取门禁列表
  getGate() {
    const that = this;
    const {
      garden_id
    } = that.data
    HTTP.GetGate({
      garden_id
    }).then(res => {
      const gate_list = res.list.map(item => {
        return {
          device_id: item.device_id,
          name: `${item.device_name}(${item.location})`,
          garden_name: item.garden_name
        }
      })
      that.setData({
        gate_list
      })
    })
  },
  // 选择日期时间
  changeDateTime1(e) {
    this.setData({
      dateTime1: e.detail.value
    });
  },
  changeDateTime2(e) {
    this.setData({
      dateTime2: e.detail.value
    });
  },
  changeDateTimeColumn1(e) {
    const that = this;
    var arr = that.data.dateTime1;
    var dateArr = that.data.dateTimeArray1;
    const {
      date
    } = that.data
    arr[e.detail.column] = e.detail.value;
    const dayEnd = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]])
    // if (e.detail.column == 1 && e.detail.value == 0) {
    //   dateArr[2] = dateTimePicker.getLoopArray(parseInt(date), dayEnd.length,"月")
    // } else {
    //   dateArr[2] = dayEnd;
    // }
    dateArr[2] = dayEnd;
    that.setData({
      dateTimeArray1: dateArr,
      // dateTime1: arr
    });
  },
  changeDateTimeColumn2(e) {
    const that = this;
    var arr = that.data.dateTime2;
    var dateArr = that.data.dateTimeArray2;
    const {
      date
    } = that.data
    arr[e.detail.column] = e.detail.value;
    const dayEnd = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]])
    // if (e.detail.column == 1 && e.detail.value == 0) {
    //   dateArr[2] = dateTimePicker.getLoopArray(parseInt(date), dayEnd.length,"月")
    // } else {
    //   dateArr[2] = dayEnd;
    // }
    dateArr[2] = dayEnd;
    that.setData({
      dateTimeArray2: dateArr,
      // dateTime1: arr
    });
  },
  getName(e) {
    const visitor_name = e.detail.value;
    this.setData({
      visitor_name
    })
  },
  getPhone(e) {
    const visitor_phone = e.detail.value;
    this.setData({
      visitor_phone
    })
  },
  getcomment(e) {
    const comment = e.detail.value;
    this.setData({
      comment
    })
  },
  getVisitor() {
    const that = this;
    const {
      device_id,
      garden_id,
      dateTime1,
      dateTime2,
      dateTimeArray1,
      dateTimeArray2,
      visitor_name,
      visitor_phone,
      comment,
      gate_name,
      garden_name
    } = that.data;
    if (!device_id) {
      wx.showToast({
        title: "请选择门禁",
        duration: 2000,
        icon: "none",
      });
      return false
    }
    if (!visitor_name) {
      wx.showToast({
        title: "请输入访客姓名",
        duration: 2000,
        icon: "none",
      });
      return false
    }
    if (!visitor_phone) {
      wx.showToast({
        title: "请输入访客联系方式",
        duration: 2000,
        icon: "none",
      });
      return false
    }
    if (!/^1[345789]\d{9}$/.test(visitor_phone)) {
      wx.showToast({
        title: "联系方式有误",
        duration: 2000,
        icon: "none",
      });
      return false;
    }
    if (!comment) {
      wx.showToast({
        title: "请输入访客事由",
        duration: 2000,
        icon: "none",
      });
      return false
    }
    const startTime = that.handleTime(dateTime1, dateTimeArray1)
    const endTime = that.handleTime(dateTime2, dateTimeArray2)
    const start = timestamp(`${startTime[0]}-${startTime[1]}-${startTime[2]} ${startTime[3]}:${startTime[4]}`)
    const end = timestamp(`${endTime[0]}-${endTime[1]}-${endTime[2]} ${endTime[3]}:${endTime[4]}`)
    HTTP.GetQcode_Visitor({
      device_id,
      garden_id,
      start,
      end,
      visitor_phone,
      visitor_name,
      comment
    }).then(res => {
      const {
        id,
        qcode
      } = res;
      wx.navigateTo({
        url: `../door/index?&qcode=${qcode}&type=2&id=${id}`,
      })
    })
  },
  handleTime(dateTime1, dateTimeArray1) {
    let time = ""
    for (let i = 0; i < dateTime1.length; i++) {
      time += dateTimeArray1[i][
        [dateTime1[i]]
      ].slice(0, dateTimeArray1[i][
        [dateTime1[i]]
      ].length - 1) + '-'
    }
    let time1 = time.split("-")
    let time2 = time1.slice(0, time1.length - 1)
    return time2
  },
})