// pages/usercenter/apply-shop/index.js
import {
  ServerBasePath
} from '~/services/_utils/request'
import {
  getFirstGoodsCategoryList
} from '~/services/usercenter/apply-shop/getFirstGoodsCategoryList'
import {
  addOrUpdateShop
} from '~/services/usercenter/apply-shop/addOrUpdateShop'
import Toast from 'tdesign-miniprogram/toast/index';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopBusinessCategoryText: '',
    shopBusinessCategoryVisible: false,
    shopBusinessCategoryList: [],
    shopInfo: {
      ShopBusinessCategoryGuid: "",
      ShopName: "",
      ShopIntro: "",
      ShopLogo: [],
      ShopBusinessLicense: [],
    },

  },

  // 提交
  submit() {
    let data = this.data.shopInfo
    data.ShopBusinessCategoryGuid = data.ShopBusinessCategoryGuid[0]
    data.ShopLogo = data.ShopLogo.map(item => item.url).join(',')
    data.ShopBusinessLicense = data.ShopBusinessLicense.map(item => item.url).join(',')
    addOrUpdateShop(data).then((res) => {
      if (res.code === 200) {
        Toast({
          context: this,
          selector: '#t-toast',
          message: res.data,
          theme: 'success',
        })
        this.setData({
          shopInfo: null
        })
        // 返回上一页
        setTimeout(() => {
          const pages = getCurrentPages(); //获取小程序页面栈
          const beforePage = pages[pages.length - 1]; //获取上个页面的实例对象  -2是上上一个页面
          wx.navigateBack({
            delta: 1,
          });
          beforePage.onLoad();
        }, 1000);
      } else {
        Toast({
          context: this,
          selector: '#t-toast',
          message: res.Msg,
          theme: 'error',
        })
      }
    })
  },

  init() {
    // 获取经营类目
    getFirstGoodsCategoryList().then((res) => {
      this.setData({
        shopBusinessCategoryList: res.data
      })
    })
  },

  onLoad(options) {
    this.init()
  },


  // 选项切换
  onPick(e) {},

  // 选中
  onPickerChange(e) {
    const { selectedOptions, value } = e.detail;
    this.setData({
      [`shopInfo.ShopBusinessCategoryGuid`]: value,
      [`shopBusinessCategoryText`]: selectedOptions.map((item) => item.label).join('/'),
    });
  },

  // 选择经营类目
  onshopBusinessCategoryPicker() {
    this.setData({
      shopBusinessCategoryVisible: true
    });
  },

  onChangeShopName(e) {
    this.setData({
      "shopInfo.ShopName": e.detail.value
    })
  },

  onChangeShopIntro(e) {
    this.setData({
      "shopInfo.ShopIntro": e.detail.value
    })
  },


  // 上传Logo
  handleAddLogo(e) {
    const {
      files
    } = e.detail;

    // 每次选择图片都上传，展示每次上传图片的进度
    files.forEach(file => this.onUploadLogo(file))
  },

  // 移除Logo
  handleRemoveLogo(e) {
    const {
      index
    } = e.detail;
    const {
      ShopLogo
    } = this.data.shopInfo;

    ShopLogo.splice(index, 1);
    this.setData({
      "shopInfo.ShopLogo": ShopLogo,
    });
  },

  // 上传ShopBusinessLicense
  handleAddShopBusinessLicense(e) {
    const {
      files
    } = e.detail;

    // 每次选择图片都上传，展示每次上传图片的进度
    files.forEach(file => this.onUploadShopBusinessLicense(file, this.data.shopInfo.ShopBusinessLicense))
  },

  // 移除营业执照
  handleRemoveShopBusinessLicense(e) {
    const {
      index
    } = e.detail;
    const {
      ShopBusinessLicense
    } = this.data.shopInfo;

    ShopBusinessLicense.splice(index, 1);
    this.setData({
      "shopInfo.ShopBusinessLicense": ShopBusinessLicense,
    });
  },


  // 上传Logo方法
  onUploadLogo(file) {
    let {
      ShopLogo
    } = this.data.shopInfo

    this.setData({
      "shopInfo.ShopLogo": [...ShopLogo, {
        ...file,
        status: 'loading'
      }],
    });
    const {
      length
    } = ShopLogo;

    const task = wx.uploadFile({
      url: ServerBasePath + 'Common/UploadFile', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: {
        fileDir: 'Shops'
      },
      success: (res) => {
        this.setData({
          [`shopInfo.ShopLogo[${length}].url`]: JSON.parse(res.data).data.url,
          [`shopInfo.ShopLogo[${length}].status`]: 'done',
        });
        // this.triggerEventToParent()
      },
    });
    task.onProgressUpdate((res) => {
      this.setData({
        [`shopInfo.ShopLogo[${length}].percent`]: res.progress,
      });
    });
  },


  // 上传营业执照方法
  onUploadShopBusinessLicense(file) {
    let {
      ShopBusinessLicense
    } = this.data.shopInfo

    this.setData({
      "shopInfo.ShopBusinessLicense": [...ShopBusinessLicense, {
        ...file,
        status: 'loading'
      }],
    });
    const {
      length
    } = ShopBusinessLicense;

    const task = wx.uploadFile({
      url: ServerBasePath + 'Common/UploadFile', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: {
        fileDir: 'Shops'
      },
      success: (res) => {
        this.setData({
          [`shopInfo.ShopBusinessLicense[${length}].url`]: JSON.parse(res.data).data.url,
          [`shopInfo.ShopBusinessLicense[${length}].status`]: 'done',
        });
        // this.triggerEventToParent()
      },
    });
    task.onProgressUpdate((res) => {
      this.setData({
        [`shopInfo.ShopBusinessLicense[${length}].percent`]: res.progress,
      });
    });
  },


  // onCustomEvent(event) {
  //   // console.log(event.detail,"获取子组件传递的值");
  //   // const receivedData = event.detail; // 获取子组件传递的值
  //   // this.setData({
  //   //   fileList: receivedData // 将子组件传递的值绑定到父组件的 data 属性 fileList 上
  //   // });
  // },


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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})