// pages/handoverVehicle/handoverVehicleRecord/handoverVehicleRecord.ts
import { getAction } from '../../../api/base';
const dayjs = require('dayjs');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabActive: 0,
    current: 1,
    isLast: false,
    loading: false,
    projectOptions: [
      {
        text: '全部',
        value: ''
      }
    ],
    filterProjectOptions: [
      {
        text: '全部',
        value: ''
      }
    ],
    projectSearchVal: '',
    selectProjectIndex: 0,
    selectProject: {} as any,
    vehicleTypeOptions: [
      {
        text: '全部',
        value: ''
      }
    ],
    vehicleTypeIndex: 0,
    vehicleType: {} as any,
    checkStatusOptions: [
      {
        text: '全部',
        value: ''
      }
    ],
    checkStatusIndex: 0,
    checkStatus: {} as any,
    receiveStatusOptions: [
      {
        text: '全部',
        value: ''
      },
      {
        text: '未交车',
        value: '0'
      },
      {
        text: '未收车',
        value: '0.5'
      },
      {
        text: '已收车',
        value: '1'
      }
    ],
    receiveStatusIndex: 0,
    receiveStatus: {} as any,
    projectShow: false,
    vehicleTypeShow: false,
    checkStatusShow: false,
    receiveStatusShow: false,
    list1: [],
    list2: [],
    list3: []
  },

  tabOnClick(event: any) {
    console.log(event.detail.name);
    // 采购
    if (event.detail.name === 0) this.getProjectOptions(4);
    // 租赁
    else if (event.detail.name === 1) this.getProjectOptions(1);
    // 看情况要不要重置查询条件
    this.setData({
      current: 1,
      isLast: false,
      list1: [],
      list2: [],
      list3: [],
      tabActive: event.detail.name,
      receiveStatusIndex: 0,
      receiveStatus: {},
      checkStatusIndex: 0,
      checkStatus: {},
      selectProjectIndex: 0,
      selectProject: {},
      vehicleTypeIndex: 0,
      vehicleType: {}
    });
    // 查询
    this.getList();
  },
  getProjectOptions(type: number) {
    // 改成合同的接口其他不动
    getAction('api/contract/list', { contractType: type }).then((res: any) => {
      if (res.code == 200) {
        res.result.unshift({
          id: '',
          name: '全部'
        });
        let arr = [] as any;
        res.result.forEach((item: any) => {
          arr.push({
            text: item.name,
            value: item.id
          });
        });
        this.setData({
          projectOptions: arr,
          filterProjectOptions: arr,
          selectProjectIndex: 0,
          projectSearchVal: ''
          // selectProject: res.result[0]
        });
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        });
      }
    });
  },
  getDictOptions(dictCode: string) {
    getAction('api/sys/dict/getDictItems/' + dictCode).then((res: any) => {
      if (res.code == 200) {
        if (dictCode == 'vehicle_type') {
          this.setData({
            vehicleTypeOptions: this.data.vehicleTypeOptions.concat(res.result),
            vehicleTypeIndex: 0
            // vehicleType: res.result[0]
          });
        } else {
          this.setData({
            checkStatusOptions: this.data.checkStatusOptions.concat(res.result),
            checkStatusIndex: 0
            // checkStatus: res.result[0]
          });
        }
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        });
      }
    });
  },
  getList() {
    console.log(this.data);
    let parms = {
      checkType: this.data.tabActive + 1,
      contractId: this.data.selectProject.value,
      templateId: '',
      vehicleType: this.data.vehicleType.value,
      checkStatus: this.data.checkStatus.value, // 验收结果（1合格，0不合格）
      receiveStatus: this.data.receiveStatus.value, // 验收结果（1合格，0不合格）
      pageNo: this.data.current,
      pageSize: 10
    };
    wx.showLoading({
      title: '加载中'
    });

    if (!this.data.loading) {
      this.setData({
        loading: true
      });
    }
    getAction('api/vehicleCheck/page', parms).then((res: any) => {
      wx.hideLoading();
      this.setData({
        loading: false
      });
      if (res.code == 200) {
        let data: any = { ...this.data };
        // data['list' + (this.data.tabActive + 1)] = res.result;
        data['list' + (this.data.tabActive + 1)] = data[
          'list' + (this.data.tabActive + 1)
        ].concat(res.result.records);
        console.log(data);
        this.setData(data);
        if (
          data['list' + (this.data.tabActive + 1)].length == res.result.total
        ) {
          this.setData({
            isLast: true
          });
        }
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        });
      }
    });
  },
  pickerChange(event: any) {
    //下拉框切换是否展示
    let fieldName: any = event.currentTarget.dataset.fieldname;
    let data: any = { ...this.data };
    data[fieldName] = true;
    this.setData(data);
  },
  hidePicker() {
    this.setData({
      projectShow: false,
      vehicleTypeShow: false,
      checkStatusShow: false,
      receiveStatusShow: false
    });
  },
  pickerConfirm(event: any) {
    console.log(event);
    //下拉框点击确认
    let { fieldname } = event.currentTarget.dataset;
    let detail = event.detail;
    let data: any = { ...this.data };
    data[fieldname] = detail.value;
    data[fieldname + 'Index'] = detail.index;
    // 初始化列表
    data.current = 1;
    data.isLast = false;
    data.list1 = [];
    data.list2 = [];
    data.list3 = [];
    this.setData(data);
    this.hidePicker();
    this.getList();
  },
  toDetail(e: any) {
    let that = this;
    if (this.data.tabActive == 0) {
      wx.navigateTo({
        url: '../procurementDelivery/procurementDelivery',
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('eventName', {
            ...e.currentTarget.dataset.inf
          });
        },
        fail: e => {
          console.log(e);
        }
      });
    } else if (this.data.tabActive == 1) {
      wx.navigateTo({
        url: '../rentalDelivery/rentalDelivery',
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('eventName', {
            ...e.currentTarget.dataset.inf
          });
        },
        fail: e => {
          console.log(e);
        }
      });
    } else {
      wx.navigateTo({
        url: '../rentalRetrieve/rentalRetrieve',
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('eventName', {
            ...e.currentTarget.dataset.inf
          });
        },
        fail: e => {
          console.log(e);
        }
      });
    }
  },
  eventReportAdd() {
    // let that = this;
    let url = '';
    if (this.data.tabActive == 0) {
      url = '../procurementDeliveryEdit/procurementDeliveryEdit';
    } else if (this.data.tabActive == 1) {
      url = '../rentalDeliveryEdit/rentalDeliveryEdit';
    } else {
    }
    wx.navigateTo({
      url: url,
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('eventName', {
          isAdd: true
        });
      },
      fail: e => {
        console.log(e);
      }
    });
  },
  lower(e: any) {
    console.log(e.detail.direction == 'bottom');
    if (this.data.isLast) {
      wx.showToast({
        title: '已经是最后一页',
        icon: 'none'
      });
      return;
    }
    if (e.detail.direction == 'bottom' && !this.data.loading) {
      // this.data.current += 1
      this.setData({
        loading: true,
        current: (this.data.current += 1)
      });
      this.getList();
    }
  },
  filterProject(event: any) {
    console.log(123, event.detail);

    let list = [] as any;
    this.data.projectOptions.forEach((item: any) => {
      if (item.text.toLowerCase().indexOf(event.detail.toLowerCase()) >= 0) {
        list.push(item);
      }
    });
    this.setData({
      filterProjectOptions: list,
      projectSearchVal: event.detail
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getProjectOptions(4);
    this.getDictOptions('vehicle_type');
    this.getDictOptions('check_status');
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      current: 1,
      isLast: false,
      list1: [],
      list2: [],
      list3: []
    });
    this.getList();
  },

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

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

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      selectProject: {},
      vehicleType: {},
      checkStatus: {}, // 验收结果（1合格，0不合格）
      receiveStatusIndex: 0,
      receiveStatus: {}, // 状态
      checkStatusIndex: 0,
      vehicleTypeIndex: 0,
      selectProjectIndex: 0,
      list1: [],
      list2: [],
      list3: [],
      current: 1,
      isLast: false
    });
    this.getList();
    // 手动控制回弹
    wx.stopPullDownRefresh();
  },

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

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