import HTTP from "../../../../requestFn/Api"
var common = require('../../utils/common');
const app = getApp();
const {
  baseURL
} = app.globalData;
Page({
  data: {
    inputShowed: false,
    inputVal: '',
    city: '成都',
    province_list: [],
    city_list: [],
    area_list: [],
    committee_list: [],
    garden_id: null,
    house_id: null,
    name: "",
    user_type: "",
    id_type: "",
    appendix: [],
    id_array: [{
      name: "身份证",
      value: 1
    }, {
      name: "护照",
      value: 2
    }],
    id_number: "",
    phone: "",
    files: [],
    applyInfo: [{
        name: "我的身份",
        click: "myType",
        array: [{
            value: 1,
            name: "业主"
          },
          {
            value: 2,
            name: "家人"
          },
          {
            value: 3,
            name: "租客"
          }
        ],
        value: "请选择",
      },
      {
        name: "省份",
        click: "selectProvice",
        array: [],
        value: "请选择",
      },
      {
        name: "城市",
        click: "selectCity",
        array: [],
        value: "请选择"
      },
      {
        name: "区域",
        click: "selectArea",
        array: [],
        value: "请选择"
      },
      {
        name: "街道",
        click: "selectStreet",
        array: [],
        value: "请选择"
      },
      {
        name: "社区",
        click: "selectCommittee",
        array: [],
        value: "请选择"
      },
      {
        name: "小区",
        click: "selectGarden",
        array: [],
        value: "请选择"
      },
      {
        name: "楼栋",
        click: "selectBuilding",
        array: [],
        value: "请选择"
      },
      {
        name: "单元",
        click: "selectUnit",
        array: [],
        value: "请选择"
      },
      {
        name: "房间",
        click: "selectHouse",
        array: [],
        value: "请选择"
      },
    ],
    Certification: false,
    navHeight: app.globalData.navHeight,
    navTop: app.globalData.navTop,
    listShowed: false,
  },
  onLoad() {
    const that = this;
    HTTP.GetProvince().then(res => {
      this.setData({
        "applyInfo[1].array": res.province_list,
        province_list: res.province_list,
        city_list: res.city_list,
        area_list: res.area_list
      })
    })
  },
  onShow() {
    const that = this;
    HTTP.GetUserInfo().then(res => {
      if (res.id_number && res.id_type && res.real_name) {
        that.setData({
          Certification: true
        })
      } else {
        that.setData({
          Certification: false
        })
      }
    })
  },
  showInput() {
    const {
      inputShowed
    } = this.data;
    this.setData({
      inputShowed: !inputShowed,
    });
  },
  hideList() {
    this.setData({
      listShowed: false,
    })
  },
  showList() {
    this.setData({
      listShowed: true,
    })
  },
  async searchGarden() {
    const garden_name = this.data.inputVal;
    const {
      list
    } = await HTTP.GetGardenList({
      garden_name
    });
    this.setData({
      garden_list: list,
      listShowed: true,
    })
    console.log(list);
  },
  clearInput() {
    this.setData({
      inputVal: '',
    });
    this.hideList();
    this.showInput();
  },
  inputTyping(e) {
    const {
      value
    } = e.detail;
    this.setData({
      inputVal: value,
    });
    if (value == '') {
      this.hideList();
    }
  },
  choiceGarden(e) {
    this.hideList();
    const {
      detail
    } = e.currentTarget.dataset;
    const {
      province_list,
      city_list,
      area_list,
    } = this.data;
    let province_index, city_index, area_index, street_index, committee_index;
    province_list.map((item, index) => {
      if (item.code == detail.province_code) {
        province_index = index
      }
    })
    const city = city_list.filter(item => item.province_code == detail.province_code);
    city.map((item, index) => {
      if (item.code == detail.city_code) {
        city_index = index
      }
    })
    const area = area_list.filter(item => item.city_code == detail.city_code)
    area.map((item, index) => {
      if (item.code == detail.area_code) {
        area_index = index
      }
    })
    this.setData({
      [`applyInfo[1].value`]: detail.province,
      [`applyInfo[1].index`]: province_index,
      "applyInfo[2].array": city,
      "applyInfo[2].value": detail.city + "市",
      [`applyInfo[2].index`]: city_index,
      "applyInfo[3].value": detail.area,
      "applyInfo[3].array": area,
      "applyInfo[3].index": area_index,
    })
    HTTP.GetStreetCommittee({
      area_code: detail.area_code
    }).then(res => {
      const street = res.committee_list.filter(item => item.street_code == detail.street_code);
      res.street_list.map((item, index) => {
        if (item.code == detail.street_code) {
          street_index = index
        }
      })
      street.map((item, index) => {
        if (item.code == detail.committee_code) {
          committee_index = index
        }
      })
      this.setData({
        "applyInfo[4].array": res.street_list,
        committee_list: res.committee_list,
        "applyInfo[4].value": detail.street,
        "applyInfo[5].array": street,
        "applyInfo[5].index": committee_index,
        "applyInfo[4].index": street_index,
        "applyInfo[5].value": detail.committee,
      })
    });
    this.findGarden(detail.province_code, detail.city_code, detail.area_code, detail.street_code, detail.committee_code, detail.garden_name, detail.id);
  },
  //获取定位
  getMap() {
    console.log("暂未开发");
  },
  selectProvice(e) {
    const index = e.detail.value;
    const value = this.data.applyInfo[1].array[index]
    const city = this.data.city_list.filter(item => item.province_code == value.code)
    this.setData({
      "applyInfo[2].array": city,
      "applyInfo[2].index": "",
      "applyInfo[3].index": "",
      "applyInfo[4].index": "",
      "applyInfo[5].index": "",
      "applyInfo[6].index": "",
      "applyInfo[2].value": "请选择",
      "applyInfo[3].value": "请选择",
      "applyInfo[4].value": "请选择",
      "applyInfo[5].value": "请选择",
      "applyInfo[6].value": "请选择",
      "applyInfo[7].value": "请选择",
      "applyInfo[8].value": "请选择",
      "applyInfo[9].value": "请选择",
      "applyInfo[3].array": [],
      "applyInfo[4].array": [],
      "applyInfo[5].array": [],
      "applyInfo[6].array": [],
      "applyInfo[7].array": [],
      "applyInfo[8].array": [],
      "applyInfo[9].array": [],
      "applyInfo[1].value": value.name,
    })
  },
  myType(e) {
    const value = this.data.applyInfo[0].array[e.detail.value]
    this.setData({
      "applyInfo[0].value": value.name,
      user_type: value.value
    });
  },
  selectCity(e) {
    const value = this.data.applyInfo[2].array[e.detail.value]
    const area = this.data.area_list.filter(item => item.city_code == value.code)
    this.setData({
      "applyInfo[3].array": area,
      "applyInfo[2].value": value.name,
      "applyInfo[3].index": "",
      "applyInfo[4].index": "",
      "applyInfo[5].index": "",
      "applyInfo[6].index": "",
      "applyInfo[3].value": "请选择",
      "applyInfo[4].value": "请选择",
      "applyInfo[5].value": "请选择",
      "applyInfo[6].value": "请选择",
      "applyInfo[7].value": "请选择",
      "applyInfo[8].value": "请选择",
      "applyInfo[9].value": "请选择",
      "applyInfo[4].array": [],
      "applyInfo[5].array": [],
      "applyInfo[6].array": [],
      "applyInfo[7].array": [],
      "applyInfo[8].array": [],
      "applyInfo[9].array": [],
    })
  },
  selectArea(e) {
    const value = this.data.applyInfo[3].array[e.detail.value]
    this.setData({
      "applyInfo[3].value": value.name,
      "applyInfo[4].index": "",
      "applyInfo[5].index": "",
      "applyInfo[6].index": "",
      "applyInfo[4].value": "请选择",
      "applyInfo[5].value": "请选择",
      "applyInfo[6].value": "请选择",
      "applyInfo[7].value": "请选择",
    });
    HTTP.GetStreetCommittee({
      area_code: value.code
    }).then(res => {
      this.setData({
        "applyInfo[4].array": res.street_list,
        "applyInfo[5].array": [{
          name: "请选择"
        }],
        "applyInfo[6].array": [{
          name: "请选择"
        }],
        "applyInfo[7].array": [{
          name: "请选择"
        }],
        committee_list: res.committee_list,
      })
    })
  },
  selectStreet(e) {
    const value = this.data.applyInfo[4].array[e.detail.value]
    const street = this.data.committee_list.filter(item => item.street_code == value.code)
    console.log(street);
    this.setData({
      "applyInfo[4].value": value.name,
      "applyInfo[5].array": street,
      "applyInfo[5].index": "",
      "applyInfo[6].index": "",
      "applyInfo[5].value": "请选择",
      "applyInfo[6].value": "请选择",
      "applyInfo[7].value": "请选择",
      "applyInfo[6].array": [],
      "applyInfo[8].value": "请选择",
      "applyInfo[9].value": "请选择",
      "applyInfo[7].array": [],
      "applyInfo[8].array": [],
      "applyInfo[9].array": [],
    });
  },
  selectCommittee(e) {
    const value = this.data.applyInfo[5].array[e.detail.value]
    this.setData({
      "applyInfo[5].value": value.name,
      "applyInfo[6].index": "",
      "applyInfo[6].value": "请选择",
      "applyInfo[7].value": "请选择",
      "applyInfo[8].value": "请选择",
      "applyInfo[9].value": "请选择",
      "applyInfo[7].array": [],
      "applyInfo[8].array": [],
      "applyInfo[9].array": [],
    });
    this.findGarden('', '', '', '', value.code, '');
  },
  findGarden(province_code, city_code, area_code, street_code, committee_code, garden_name, id = '') {
    HTTP.GetGardenList({
      province_code,
      city_code,
      area_code,
      street_code,
      committee_code,
    }).then(res => {
      if (res.list.length > 0) {
        const array = res.list.map(item => {
          return {
            garden_id: item.id,
            name: item.garden_name
          }
        })
        this.setData({
          "applyInfo[6].array": array,
        })
        if (garden_name) {
          let garden_index;
          array.map((item, index) => {
            if (item.garden_id == id) {
              garden_index = index
            }
          })
          this.setData({
            "applyInfo[6].value": garden_name,
            "applyInfo[6].index": garden_index,
            garden_id: id
          })
          this.getBuilding(id);
        }
      } else {
        this.setData({
          "applyInfo[6].array": []
        })
      }
    })
  },
  selectGarden(e) {
    const value = this.data.applyInfo[6].array[e.detail.value]
    this.setData({
      "applyInfo[6].value": value.name,
      "applyInfo[7].value": "请选择",
      "applyInfo[8].value": "请选择",
      "applyInfo[8].array": [],
      "applyInfo[9].array": [],
      "applyInfo[9].value": "请选择",
      garden_id: value.garden_id
    });
    this.getBuilding(value.garden_id);
  },
  getBuilding(garden_id){
    HTTP.GetGardenBuilding({
      garden_id
    }).then(res => {
      if (res.list.length > 0) {
        const array = res.list.map(item => {
          return {
            id: item.id,
            name: item.building_name
          }
        })
        this.setData({
          "applyInfo[7].array": array
        })
      } else {
        this.setData({
          "applyInfo[7].array": []
        })
      }
    })
  },
  selectBuilding(e) {
    const value = this.data.applyInfo[7].array[e.detail.value]
    this.setData({
      "applyInfo[7].value": value.name,
      "applyInfo[8].value": "请选择",
      "applyInfo[9].array": [],
      "applyInfo[9].value": "请选择",
    });
    HTTP.GetGardenUnit({
      garden_id: this.data.garden_id,
      building_id: value.id
    }).then(res => {
      if (res.list.length > 0) {
        const array = res.list.map(item => {
          return {
            id: item.id,
            name: item.unit_name
          }
        })
        this.setData({
          "applyInfo[8].array": array
        })
      } else {
        this.setData({
          "applyInfo[8].array": []
        })
      }
    })
  },
  selectUnit(e) {
    const value = this.data.applyInfo[8].array[e.detail.value]
    this.setData({
      "applyInfo[8].value": value.name,
      "applyInfo[9].value": "请选择",
    });
    HTTP.GetGardenHouse({
      unit_id: value.id,
      garden_id: this.data.garden_id
    }).then(res => {
      if (res.list.length > 0) {
        const array = res.list.map(item => {
          return {
            id: item.id,
            name: item.house_name
          }
        })
        this.setData({
          "applyInfo[9].array": array,
        })
      } else {
        this.setData({
          "applyInfo[9].array": []
        })
      }
    })
  },
  selectHouse(e) {
    const value = this.data.applyInfo[9].array[e.detail.value]
    this.setData({
      "applyInfo[9].value": value.name,
      house_id: value.id
    });
  },
  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
              })
            },
            fail(res) {
              console.log(res);
            }
          })
          return item.tempFilePath
        })
        that.setData({
          files: that.data.files.concat(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("用户点击取消")

        }
      }
    })
  },
  //提交申请
  submit(e) {
    const that = this;
    const user = that.data
    if (user.user_type == "") {
      wx.showToast({
        title: '请选择我的身份',
      })
      return false
    }
    if (user.garden_id == null) {
      wx.showToast({
        title: '请选择小区',
      })
      return false
    }
    if (user.house_id == null) {
      wx.showToast({
        title: '请选择房间',
      })
      return false
    }
    if (!user.Certification) {
      wx.showToast({
        title: "请点击实名认证进行实名认证",
        duration: 2000,
        icon: "none",
      });
      return false;
    }
    const {
      user_type,
      appendix,
      garden_id,
      house_id,
    } = user
    HTTP.HouseholdApply({
        user_type,
        appendix,
        garden_id,
        house_id,
      }).then(res => {
        if (res.code == 0) {
          wx.navigateTo({
            url: "../success/index",
          })
        }
      })
      .catch(res => {
        console.log(res);
      })
  },
  goInentity() {
    console.log(2);
    wx.navigateTo({
      url: '../setting/inentity/index',
    })
  },
  // 点击返回上级页面
  goBack() {
    let pages = getCurrentPages(); //获取小程序页面栈
    let beforePage = pages[pages.length - 2]; //获取上个页面的实例对象
    wx.navigateBack({ //返回上一页  
      delta: 1
    })
  },
});