import { API } from './../../../utils/network/api'

const api = new API()
const app = getApp()
Page({
  data: {
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '问诊订单', //导航栏 中间的标题
    },
    height: app.globalData.height, 
    result: [],
    order_list:[],
    current_page: 0,
    total: 0,
    per_page: 10,
    last_page: 0,
    allchecked: false,
    select_order_length: 0,
    select_order_amount: 0,
    select_order_inquiry_id: []
  },
  onChange(event) {
    console.log("onChange: ",event)
    this.setData({
      result: event.detail,
    });
    this.resetSelectOrder();
    this.setData({
      allchecked: this.data.result.length == this.data.order_list.length
    })
  },
  onAllChange(event) {
    console.log("onAllChange: ",event)
    this.setData({
      allchecked: event.detail
    })
    if(event.detail == true){
      this.data.order_list.forEach((item,index) => {
        this.data.result.push(""+index);
      })
      this.setData({
        result: this.data.result
      })
    }
    if(event.detail == false){
      this.setData({
        result: []
      })
    }
    this.resetSelectOrder();
  },
  resetSelectOrder(e){
    let result = this.data.result;
    let select_order_amount = 0;
    let select_order_inquiry_id = [];
    result.forEach(item => {
      select_order_amount = Number(select_order_amount) + Number(this.data.order_list[item].expected_amount_total);
      select_order_inquiry_id.push(this.data.order_list[item].order_inquiry_id);
    })
    this.setData({
      select_order_length: result.length,
      select_order_amount: select_order_amount,
      select_order_inquiry_id: select_order_inquiry_id
    })
  },
  selectOrder(e){
    const { index } = e.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },
  getDoctorWithdrawalOrder(){
    //可提现问诊订单列表
    let params = {};
    params.page = this.data.current_page + 1;
    params.per_page = this.data.per_page;
    api.getDoctorWithdrawalOrder(params).then(response => {
      console.log(response);
      let list = this.data.order_list
      if(response.data.total > 0){
        this.setData({
          order_list: list.concat(response.data.data),
          current_page: response.data.current_page,
          per_page: response.data.per_page,
          last_page: response.data.last_page
        })
      }
    }).catch(errors => {console.error(errors);})
  },
  onShow(){
    this.getDoctorWithdrawalOrder();
    this.resetSelectOrder();
  },
  onReachBottom() {
    console.log('===触底了！！===');
    if(this.data.current_page < this.data.last_page){//最后一页时停止分页
      this.getDoctorWithdrawalOrder()
    }
  },
  confirmOrder(){
    const select_order_inquiry_id = this.data.select_order_inquiry_id;
    console.log(select_order_inquiry_id);
    let order_inquiry_ids = select_order_inquiry_id.join(",");
    app.go("/Pages/yishi/cash/index?order_inquiry_ids="+order_inquiry_ids);
  }
})