import HTTP from "../../../../../requestFn/Api";
import {
  timestamp
} from "../../../utils/util"
var dateTimePicker = require("../../../utils/dateTimePicker")
const app = getApp();
const {
  baseURL
} = app.globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    garden_id: null,
    house_id: null,
    files: [],
    appendix: [],
    area_list: [{name:"公共区域",value:1}, {name:"室内",value:2}],
    area: "",
    class_list: [],
    class: '',
    dateTimeArray1: null,
    dateTime1: null,
    valueLength:0,
  },

  onLoad(options) {
    const that = this;
    const {
      garden_id,
      house_id
    } = app.globalData
    var obj1 = dateTimePicker.dateTimePicker();
    that.setData({
      garden_id,
      house_id,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
    })
  },
  submit(e) {
    const that = this;
    const {
      appendix,
      house_id,
      garden_id,
      dateTimeArray1,
      class_list
    } = that.data
    const {
      area,
      apply_content,
      apply_people,
      apply_people_phone,
      class_id,
      appointment
    } = e.detail.value;
    const startTime = that.handleTime(appointment, dateTimeArray1);
    const start = timestamp(`${startTime[0]}-${startTime[1]}-${startTime[2]} ${startTime[3]}:${startTime[4]}`);
    if (area == "") {
      wx.showToast({
        title: '请选择报修区域',
        icon: "error",
      })
      return false
    }
    if (class_id == "") {
      wx.showToast({
        title: '请选择报修分类',
        icon: "error",
      })
      return false
    }
    if (apply_people == "") {
      wx.showToast({
        title: '请输入报修人',
        icon: "error",
      })
      return false
    }
    if (apply_people_phone == "") {
      wx.showToast({
        title: '请输入联系电话',
        icon: "error",
      })
      return false
    }
    if (!/^1[345789]\d{9}$/.test(apply_people_phone)) {
      wx.showToast({
        title: "联系电话有误",
        duration: 2000,
        icon: "error",
      });
      return false;
    }
    if (apply_content == "") {
      wx.showToast({
        title: '请输入报修内容',
        icon: "error",
      })
      return false
    }
    HTTP.Repair_order({
      method: "post",
      apply_content,
      apply_people,
      apply_people_phone,
      apply_pic: appendix,
      house_id,
      garden_id,
      class_id: class_list[class_id].id,
      appointment: start
    }).then(res => {
      if (res.code == 0) {
        wx.showToast({
          title: "提交成功",
          duration: 2000,
          icon: "success",
          success() {
            wx.navigateBack({
              delta: 1,
            })
          }
        });
      } else {
        wx.showToast({
          title: "服务器异常,请稍后再试",
        });
      }
    })
  },
  getArea(e) {
    this.setData({
      area: e.detail.value
    })
    const {garden_id,area_list} = this.data;
    this.getRepair_class(garden_id,area_list[e.detail.value].value)
  },
  getClass(e) {
    this.setData({
      class: e.detail.value
    })
  },
  changeDateTime1(e) {
    this.setData({
      dateTime1: e.detail.value
    });
  },
  getRepair_class(garden_id, area) {
    HTTP.GetRepair_class({
      garden_id,
      area
    }).then(res => {
      this.setData({
        class_list: res.list
      })
    })
  },
  changeDateTimeColumn1(e) {
    const that = this;
    var arr = that.data.dateTime1;
    var dateArr = that.data.dateTimeArray1;
    arr[e.detail.column] = e.detail.value;
    const dayEnd = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]])
    dateArr[2] = dayEnd;
    that.setData({
      dateTimeArray1: dateArr,
    });
  },
  chooseImage() {
    const that = this;
    wx.chooseMedia({
      mediaType: ["image"],
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const userInfo = wx.getStorageSync('userInfo')
        const pic = res.tempFiles.map(item => {
          wx.uploadFile({
            filePath: item.tempFilePath,
            name: 'file',
            url: baseURL + '/upload',
            header: {
              "token": userInfo.token
            },
            success(res) {
              const result = that.data.appendix;
              result.push(JSON.parse(res.data).data)
              that.setData({
                appendix: result
              })
              console.log(JSON.parse(res.data).data);
            },
            fail(res) {
              console.log(res);
            }
          })
          return item.tempFilePath
        })
        that.setData({
          files: that.data.files.concat(pic),
        });
        console.log(pic);
      },
      fail(res) {
        console.log(res);
      }
    });
  },
  previewImage(e) {
    wx.previewImage({
      current: this.data.files[e.currentTarget.dataset.index],
      urls: this.data.files,
    });
  },
  deleteImg(e) {
    var that = this
    wx.showModal({
      title: "提示",
      content: "是否删除图片",
      success: function (res) {
        if (res.confirm) {
          for (var i = 0; i < that.data.files.length; i++) {
            if (i == e.currentTarget.dataset.index) {
              that.data.files.splice(i, 1)
              that.data.appendix.splice(i, 1)
            }
          }
          that.setData({
            files: that.data.files
          })
        } else if (res.cancel) {
          console.log("用户点击取消")

        }
      }
    })
  },
  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
  },
  getTextLength(e) {
    const valueLength = e.detail.value.length;
    this.setData({
      valueLength
    })
  },
})