/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 *		2.功能描述：主页首页
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		admin		   2024-02-20	   1.00		   	  首页
 *******************************************************************************/

const app = getApp()
const api = require('@/utils/api.js');
const config = require('@/config/config.js');
const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js')

Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    dateType: 'week',// week本周， month本月
    swiperIndex: 0,
    imageUrl: config.image_url + '/static/img/',
    advertisementList: [], // 广告数据
    // 分页flg
    pageFlag: false,
    comMenuList: [],// 常用功能
    companyName: null,
    // 成交额
    transactionAmount: 0,
    // 收款额
    collectionAmount: 0,
    //功能分类-大功能模块-页面
    functionItems: [
    ],
    //  模块名称
    functionRegionItems: [
      {
        diamondflg: false,
        titlesrc: 'home-add-order.png',
        cardname: '采购管理',
        background: 'linear-gradient(200.98deg, #FFFFFF -13.39%, #A1CDFF 100%)',
        arrowBackground:'#A9CCFF',
        color: '#3785E5',
        image:'pur-icon',
        items: [
          { code: 'pur-order', url: '/package-purchase/pages/purchase-order/purchase-order', title: '采购订单', color: '#7AADDF' },
          { code: 'pur-enter', url: '/package-purchase/pages/purchase-receipt/purchase-receipt', title: '采购入库', color: '#7AADDF' }
        ],
      },
      {
        diamondflg: true,
        titlesrc: 'home-customer-data.png',
        cardname: '销售管理',
        background: 'linear-gradient(224.58deg, #FFFFFF -11.05%, #FFC9C2 100%)',
        arrowBackground:'#FDD4C7',
        color: '#CC5E64',
        image:'sale-icon',
        items: [
          { code: 'order', url: '/package-sales/pages/order-billing/order-billing', title: '订单开单',color:'#E6725F' },
          { code: 'order-out', url: '/package-sales/pages/sales-outbound/sales-outbound', title: '销售出库' ,color:'#E6725F'}
        ],
      },
      {
        diamondflg: false,
        titlesrc: 'home-receive-customer.png',
        background: 'linear-gradient(225.36deg, #FFFFFF -10.7%, #F2D1AC 100%)',
        arrowBackground:'#EABB92',
        color:'#D27E52',
        cardname: '存货管理',
        image:'ivt-icon',
        items: [
          { code: 'entry-handle', url: '/package-inventory/pages/warehousing-processing/warehousing-processing', title: '入库办理',color:'#EEA05B' },
          { code: 'out-handle', url: '/package-inventory/pages/outbound-processing/outbound-processing', title: '出库办理',color:'#EEA05B' }
        ],
      },
      {
        diamondflg: false,
        titlesrc: 'home-customer-payment.png',
        cardname: '客户管理',
        background: 'linear-gradient(200.98deg, #FFFFFF -13.39%, #D7DFFF 100%)',
        arrowBackground:'#C3D0FF',
        color:'#606EB2',
        image:'customer-icon',
        items: [
          { code: 'customer', url: '/package-basic-data/pages/customer-list/customer-list', title: '客户档案' ,color:'#606EB2'},
          { code: 'cus-follow', url: '/package-basic-data/pages/customer-reception/customer-reception', title: '客户跟进',color:'#606EB2' }
        ],
      },
      {
        diamondflg: true,
        titlesrc: 'home-customer-payment.png',
        cardname: '资金管理',
        background: 'linear-gradient(223.67deg, #FFFFFF -15.06%, #FFF5E2 100%)',
        arrowBackground:'#ECD1A7',
        color:'#A5814A',
        image:'fin-icon',
        items: [
          { code: 'receipt', url: '/package-accounts-manage/pages/receipt/receipt', title: '收款单' ,color:'#A5814A'},
          { code: 'payment', url: '/package-accounts-manage/pages/payment-order/payment-order', title: '付款单',color:'#A5814A' }
        ],
      },
      {
        diamondflg: false,
        titlesrc: 'home-customer-payment.png',
        cardname: '系统管理',
        background: 'linear-gradient(225.36deg, #FFFFFF -10.7%, #C2DFFF 100%)',
        arrowBackground:'#B7D6FF',
        color:'#89B9FF',
        image:'analysis-icon',
        items: [
          { code: 'goods-attr', url: '/package-basic-data/pages/product-attribute/product-attribute', title: '商品属性',color:'#89B9FF' },
          { code: 'goods', url: '/package-basic-data/pages/product-file/product-file', title: '商品档案',color:'#89B9FF' }
        ],
      },
    ],
  },
  /**
  * @desc : 切换公司
  * @author : 周兴
  * @date : 2024/4/1
  */
  changeCp() {
    wx.navigateTo({
      url: '/pages/company/company',
    })
  },
  /**
  * @desc : 切换日期查询成交额和收款额
  * @author : 周兴
  * @date : 2024/4/1
  */
  changeDate() {
    let dateType = this.data.dateType;
    dateType = dateType == 'month' ? 'week' : 'month';
    console.log('dateType', dateType);
    // 查询应收和收款
    this.getReceivableAccountSum(dateType);
    this.setData({
      dateType: dateType
    })
  },
  /**
  * @desc : 查询应收和收款
  * @author : 周兴
  * @date : 2024/4/3
  */
  getReceivableAccountSum(dateType) {

    let now = new Date();
    let params = {
      staffId: app.globalData.user.staffId,
    }
    // 本周
    if (dateType == 'week') {
      params['accDateStart'] = now.getWeekFirstDay()
      params['accDateEnd'] = now.getWeekLastDay()
    } else if (dateType == 'month') {
      params['accDateStart'] = now.getFirstDateStr()
      params['accDateEnd'] = now.getLastDateStr()
    }
    let service = app.globalData['accountService']
    api.request(service.prefix + service.getReceivableAccountSum, 'POST', params).then(res => {
      if (res.data.code == Constants.SUCESS_CODE) {
        console.log('eee', res.data);
        if (res.data && res.data.data) {
          this.setData({
            transactionAmount: res.data.data.receivable,
            collectionAmount: res.data.data.receipt,
          })
        }
        // if (res.data && res.data.data) {
        //   let advertisementList = res.data.data
        //   advertisementList.forEach(it => {
        //     it.adPath = config.image_url + it.adPath
        //   })
        //   this.setData({
        //     advertisementList: advertisementList
        //   })
        // }
      }
    });
  },
  /**
  * @desc : 查询广告
  * @author : 周兴
  * @date : 2024/4/1
  */
  getAdvertisement() {
    let params = {
      appCode: Constants.APP_CODE,
      gradeCode: app.globalData.company.gradeCode
    }
    let service = app.globalData['advertisementService']
    api.request(service.prefix + service.selectByCond, 'POST', params).then(res => {
      if (res.data.code == Constants.SUCESS_CODE) {
        if (res.data && res.data.data) {
          let advertisementList = res.data.data
          advertisementList.forEach(it => {
            it.adPath = config.image_url + it.adPath
          })
          this.setData({
            advertisementList: advertisementList
          })
        }
      }
    });
  },
  /**
  * @desc : 查询常用功能以及频率高的功能
  * @author : 周兴
  * @date : 2024/3/30
  */
  getComMenu() {
    let staffId = app.globalData.user.staffId;
    if (!staffId) {
      return;
    }
    let params = {
      staffId: staffId,
      appCode: Constants.APP_CODE,
    }
    let service = app.globalData['comMenuService']
    api.request(service.prefix + service.selectMenu, 'POST', params).then(res => {
      if (res.data.code == Constants.SUCESS_CODE) {
        let comMenuList = res.data.data
        // 增加全部列表
        let allMenu = {
          menuUuid: '-1',
          menuName: '全部应用',
          objectIcon: 'customer.png'
        }
        comMenuList.push(allMenu);
        this.setData({
          comMenuList: comMenuList,
        })
      }
    });
  },
  /**
   * @desc : 跳转到功能页面
   * @author : 于继渤
   * @date : 2024/1/23 9:16
   */
  toApp(e) {
    let item = e.currentTarget.dataset.item
    // 如果点击全部应用跳转到应用页面
    if (item.menuUuid == '-1') {
      wx.reLaunch({
        url: '/pages/index/index',
      })
      return
    }

    let hasRightFlag = false;
    let menuUuid = null
    let menuList = app.globalData.menus
    if (menuList && menuList.length > 0) {
      // 查看该功能是否有权限
      let filters = menuList.filter(it => it.objectCode == (item.objectCode || item.code));
      if (filters && filters.length > 0) {
        menuUuid = filters[0].menuUuid
        hasRightFlag = true;
      }
    }
    if (!hasRightFlag) {
      util.showToast('没有该功能权限');
      return;
    }
    // 打开页面
    if (item.objectPath || item.url) {
      wx.navigateTo({
        url: item.objectPath ? item.objectPath : item.url,
        success: function (res) {
          res.eventChannel.emit('params', { formMode: 'index', menuUuid: menuUuid })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function name(params) {
    // 获取常用菜单
    this.getComMenu();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      }
    })
    this.setData({
      companyName: app.globalData.company.cpName
    })
    // 查询广告数据
    this.getAdvertisement();
    // 查询应收和收款额
    this.getReceivableAccountSum(this.data.dateType);
    console.log("home-page", options);
    // options 中的 scene  
    if (options.scene) {
      // var scene = options.scene
      var scene = decodeURIComponent(options.scene)
      let openid = scene.s

      // 邀请员工的确认
      wx.navigateTo({
        url: '/package-basic-data/pages/staff/detail/detail',
        events: {
          // 回调后，在这里给页面赋值
          bandData: function (e) {
          }
        },
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据 TODO 测试例子url是写死的，实际中，需要从route中读取
          res.eventChannel.emit('params', { formType: openid, formMode: Constants.formMode.index })
        }
      })

    }


    //小程序推送消息传过来的值 pages/login/login?loginType=1&otherParam=值
    // loginType 
    // 1 客户跟进通知-进入客户跟进详情  2 客户任务通知  3 客户流失提醒  4 审批任务通知  5 工具栏跳转客户跟进
    // 6 工具栏跳转订单开单  7 工具栏跳客户详细 8 客户跟进通知-进入接待详情 9 销售退货通知跳转 10上样检查跳转

  },

})

