// pages/handoverVehicle/rentalDelivery/rentalDelivery.ts
import { deleteAction } from '../../../api/base';
import Dialog from '@vant/weapp/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabActive: 0,
    showBox: {
      base: true,
      vehicle: true,
      check: true
    },
    item: {} as any
  },
  // 变更tab
  changeTab(e: any) {
    this.setData({
      tabActive: e.currentTarget.dataset.tab
    });
  },
  showBoxInf(e: any) {
    console.log(e);
    let { fieldname } = e.currentTarget.dataset;
    // let detail = e.detail;
    let data: any = { ...this.data };
    data.showBox[fieldname] = !data.showBox[fieldname];
    this.setData(data);
  },
  previewImg(e: any) {
    console.log(e);
    let { index } = e.currentTarget.dataset;
    // let { photoList } = this.data;
    let list = [] as any;
    // photoList.forEach((item: any) => {
    //   list.push(item.fileUrl);
    // });
    wx.previewImage({
      current: list[index | 0], // 当前显示图片的http链接  默认urls[0]
      urls: list // 需要预览的图片http链接列表
    });
  },
  toDel() {
    Dialog.confirm({
      title: '提示',
      message: '是否确认删除'
    })
      .then(() => {
        // on confirm
        let params = {
          id: this.data.item.id
        };
        wx.showLoading({
          title: '加载中'
        });
        deleteAction('api/vehicleCheck/delete', params).then((res: any) => {
          wx.hideLoading();
          if (res.code == 200) {
            wx.showToast({
              title: '删除成功',
              icon: 'none'
            });
            setTimeout(() => {
              wx.navigateBack({
                delta: 1,
                fail: e => {
                  console.log(e);
                }
              });
            }, 500);
          } else {
            wx.showToast({
              title: res.message,
              icon: 'none'
            });
          }
        });
      })
      .catch(() => {
        // on cancel
      });
  },
  toEdit(e: any) {
    let that = this;
    wx.navigateTo({
      url: '../rentalDeliveryEdit/rentalDeliveryEdit',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('eventName', {
          tabActive: e.currentTarget.dataset.status,
          ...that.data.item
        });
      },
      fail: e => {
        console.log(e);
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    // 接收传参
    const eventChannel = this.getOpenerEventChannel();

    eventChannel &&
      eventChannel.on &&
      eventChannel.on('eventName', (data: any) => {
        console.log('接收参数', data);

        data.ckeckUrlList = [];
        data.ensureUrlList = [];
        data.otherUrlList = [];
        if (data.ckeckUrl) {
          for (let i = 0; i < data.ckeckUrl.split(',').length; i++) {
            let pic = data.ckeckUrl.split(',')[i];
            data.ckeckUrlList.push({
              url: pic,
              name: ''
            });
          }
        }
        if (data.ensureUrl) {
          for (let i = 0; i < data.ensureUrl.split(',').length; i++) {
            let pic = data.ensureUrl.split(',')[i];
            data.ensureUrlList.push({
              url: pic,
              name: ''
            });
          }
        }
        if (data.otherUrl) {
          for (let i = 0; i < data.otherUrl.split(',').length; i++) {
            let pic = data.otherUrl.split(',')[i];
            data.otherUrlList.push({
              url: pic,
              name: ''
            });
          }
        }
        // 收车
        if (data.children) {
          data.children.parentId = data.id;
          data.children.projectList = data.projectList;
          data.children.vehicleList = data.vehicleList;
          data.children.ckeckUrlList = [];
          data.children.ensureUrlList = [];
          data.children.otherUrlList = [];
          if (data.children.ckeckUrl) {
            for (let i = 0; i < data.children.ckeckUrl.split(',').length; i++) {
              let pic = data.children.ckeckUrl.split(',')[i];
              data.children.ckeckUrlList.push({
                url: pic,
                name: ''
              });
            }
          }
          if (data.children.ensureUrl) {
            for (
              let i = 0;
              i < data.children.ensureUrl.split(',').length;
              i++
            ) {
              let pic = data.children.ensureUrl.split(',')[i];
              data.children.ensureUrlList.push({
                url: pic,
                name: ''
              });
            }
          }
          if (data.children.otherUrl) {
            for (let i = 0; i < data.children.otherUrl.split(',').length; i++) {
              let pic = data.children.otherUrl.split(',')[i];
              data.children.otherUrlList.push({
                url: pic,
                name: ''
              });
            }
          }
        }
        this.setData({
          item: data
        });
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
