// pages/publicTransfer/index.js
import { getMeterDetail, getMeterList, getTenementMeterList } from "../../service/meter";
import { createPublicTopUp } from '../../service/recharge'
import { uploadFile, uploadPublicFile } from "../../service/public";
import { alertInfo, loadingFunc, getFileName, alertSuccess } from "../../utils/index";
import request from '../../utils/request';
import Dialog from '@vant/weapp/dialog/dialog';

const { OK } = request;

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { money, meterId, tenementId, tenementName, park } = options;
    this.init({ money, meterId, tenementId, tenementName, park });
  },
  async init({ money, meterId, tenementId, tenementName, park }) {
    const { code, message, data } = await getMeterDetail({ tenement: tenementId, id: meterId });
    if (code !== OK) {
      alertInfo(message);
      return;
    }
    this.setData({
      meter: data,
      money,
      tenementName,
      tenementId,
      park
    })
  },
  async upload(res) {
    
    const result = await uploadPublicFile(res.tempFiles[0]?.tempFilePath)
    if (result.statusCode !== 200) {
      alertInfo("上传失败")
      return;
    }
    const data = JSON.parse(result.data)
    this.setData({
      fileList: [{ url: data.data, name: getFileName(data.data) }]
    })
  },
  onUpload() {
    wx.chooseMedia({  
      count: 1, // 可以选择文件的数量  
      mediaType: ['image'], // 可以选择文件的类型，这里只允许上传文件  
      sourceType: ['album', 'camera'],
      success: (res) => {
        const file = res.tempFiles[0];
        if (file.size / 1024 / 1024 > 1) {
          alertInfo("文件不能大于1M")
          return;
        }
        loadingFunc(async () => {
          await this.upload(res)
        })
      }
    }); 
  },
  async handleSubmit() {
    const { fileList, meter, money, park, tenementId } = this.data;
    if (!fileList?.length) {
      alertInfo("请上传凭证")
      return;
    }
    const { code, message } = await createPublicTopUp({ 
      id: meter.id, 
      tenement: tenementId, 
      money: Number(money),
      park,
      vouch:  fileList[0].url
    })
    if (code !== OK) {
      alertInfo(message)
      return;
    }
    Dialog.alert({
      title: '提示',
      message: '订单提交成功！请耐心等待管理员审核，如果疑问请联系客服',
      confirmButtonOpenType: "contact",
      showCancelButton: true,
      confirmButtonText: "联系客服",
      cancelButtonText: "关闭"
    }).then(() => {
      wx.switchTab({
        url: '/pages/home/index',
      })
    }).catch(() => {
      wx.redirectTo({
        url: '/pages/rechargeRecord/index',
      })
    });
    setTimeout(() => {
     
    }, 600)
  },
  handleDelete() {
    this.setData({ fileList: [] })
  },
  copy() {
    wx.setClipboardData({
      data: `河北华昌宝能售电有限公司  
建行新石北路支行  
13050161563600000908`,
      success: () => {
        alertSuccess("复制成功")
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})