import currency from "../../../utils/currency";
const _Http = getApp().globalData.http,
  file = require("../../../utils/FormatTheAttachment");
Page({
  data: {
    loading: true,
    params: {}, //请求体
    result: [], //返回结果
    idname: "itemid", //idkey
    showName: "itemname",
    newPrice: "price",
    oldPrice: "oldprice",
  },
  onLoad(options) {
    if (options.params) {
      let params = JSON.parse(options.params);
      if (!params.content.pageNumber || !params.content.pageTotal) {
        params.content.pageNumber = 1;
        params.content.pageTotal = 1;
      }
      this.setData({
        params
      });
    };
    this.setData({
      idname: options.idname || this.data.idname
    });
    this.getList()
  },
  getList(init = false) {
    //init 用于初始化分页
    if (init.detail != undefined) init = init.detail;
    let params = this.data.params;
    if (init) params.content.pageNumber = 1
    if (params.content.pageNumber > params.content.pageTotal) return;
    _Http.basic(params).then(res => {
      console.log("选择产品列表", res)
      this.selectComponent('#ListBox').RefreshToComplete();
      if (res.msg != '成功') return wx.showToast({
        title: res.msg,
        icon: "none"
      })
      const CNY = num => currency(num, {
        symbol: "￥",
        precision: 2
      }).format();
      let newPrice = this.data.newPrice,
        oldPrice = this.data.oldPrice;
      res.data = res.data.map(value => {
        if (value.attinfos.length != 0) {
          value.attinfos = file.fileList(value.attinfos)
          let image = value.attinfos.find(v => v.fileType == "image");
          value.cover = image ? image.cover : "";
        }
        if (newPrice) value.newPrice = CNY(value[newPrice] || 0);
        if (oldPrice) value.oldPrice = CNY(value[oldPrice] || 0);
        value.qty = value.orderminqty;
        return value;
      })
      this.setData({
        'params.content.pageNumber': res.pageNumber + 1,
        'params.content.pageTotal': res.pageTotal,
        'params.content.total': res.total,
        list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
        loading: false
      })
    })
  },
  /* 选中 */
  changeResult(e) {
    if (e.target.dataset.iscatch) return;
    let {
      id
    } = e.currentTarget.dataset, result = this.data.result;
    console.log(e)
    result.some(v => v == id) ? result = result.filter(v => v != id) : result.push(id)
    this.setData({
      result
    });
  },
  /* 提交 */
  submit() {
    let result = this.data.result,
      obj = {
        result,
        list: result.map(v => this.data.list.find(value => value[this.data.idname] == v)),
        value: [result.map(v => {
          let data = this.data.list.find(value => value[this.data.idname] == v);
          return data ? data[this.data.showName] : ""
        }), result]
      }
    getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  },
  /* 预览图片 */
  viewImage(e) {
    const {
      file
    } = e.currentTarget.dataset;
    if (file.length) wx.previewMedia({
      sources: file.filter(value => ['image', 'vadio'].includes(value.fileType)).map(v => {
        return {
          url: v.url,
          type: v.fileType
        }
      }),
      current: 0,
      showmenu: true
    })
  },
  /* 开始搜索 */
  startSearch({
    detail
  }) {
    let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
    if (detail == condition) return;
    this.setData({
      'content.where.condition': detail,
      'params.content.where.condition': detail
    });
    this.getList(true);
  },
  /* 取消搜索 */
  onClear() {
    this.setData({
      'content.where.condition': "",
      'params.content.where.condition': ""
    });
    this.getList(true);
  },
  onReady() {
    this.selectComponent("#ListBox").setHeight(".total", this);
  },
  /* 步进器输入框失去焦点 */
  inputBlur(e) {
    const {
      index
    } = e.currentTarget.dataset;
    let item = this.data.list[index];
    let qty = 0;
    if (item.orderminqty > e.detail.value) {
      wx.showToast({
        title: '输入数量低于最低起订量！',
        icon: "none"
      })
      qty = item.orderminqty;
    } else if (item.orderminqty < e.detail.value) {
      var currencyRounding = value => currency(value, {
        increment: item.orderaddqty
      });
      qty = currency(currencyRounding(currency(e.detail.value).subtract(item.orderminqty)).format()).add(item.orderminqty).value;

    } else {
      qty = e.detail.value;
    }
    this.setData({
      [`list[${index}].qty`]: 0
    });
    this.setData({
      [`list[${index}].qty`]: qty
    });
  },
  stepperChange(e) {
    const {
      index
    } = e.currentTarget.dataset;
    let item = this.data.list[index];
    console.log(item)
    console.log(item.qty)
    console.log(item.orderaddqty)
    if (e.type == 'plus') {
      item.qty += (item.orderaddqty) - 0
    } else {
      item.qty -= item.orderaddqty
    }
    console.log(item.qty)
    this.setData({
      [`list[${index}]`]: item
    })
  },
  onUnload() {
    //回收数据
    getApp().globalData.handleSelect = null;

  }
})