/*******************************************************************************
 * Copyright(c) 2024 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 *		2.功能描述：明细报表
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		周兴	    2024年4月17日		     1.00		   	    一览
 *******************************************************************************/
const mixins = require('@/mixins/index.js')
const api = require('@/utils/api.js');
const config = require('@/config/config.js');
const Constants = require('@/utils/Constants');
const util = require('@/utils/util.js')
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据 
   */
  data: {
    // 页面编码
    objectCode: null,
    // 页面标题
    title: null,
    // 方法
    method: '',
    // 导出的方法（可能会跟查询方法不一致）（对应mapper中的方法）
    exportMethod: '',
    // 查询条件
    searchContent: [{
      code: 'makeTime',
      title: mixins.$t('pastThirthtyDays'),//近30天',
      defaultValue: 5,
      searchType: Constants.searchType.date
    },
    {
      code: 'choose',
      title: mixins.$t('choose'),//'筛选',
      searchType: Constants.searchType.pick
    }],
    // 查询条件-筛选
    pullMenuList: [],
    // 内容不定
    contentObj: {},
    // 内容一定
    content: [],
    // 列表区（脚部金额）
    footerAmount: {},
    // 列表区（脚部信息）
    footerInfo: [],
    // 路由
    routeObjName: '',
    // 额外的查询条件
    otherSearchParams: null,
    // 大查询框的提示信息
    placeholder: null,
    // 列表标题
    listTitle: null,
    // 列表状态
    listStatus: null,
    // 副标题
    listSubTitle: null,
    // 不定列的列名
    contentCol: null,
    // 页面是否加载完毕，控制不要先走查询
    loadFlag: false,
    // 初始不进行自动查询
    initSearchFlag: false,
    titleText: ''
  },
  /**
   * @desc : 设置查询参数
   * @date : 2024年3月8日
   * @author : 周兴
   */
  setSearchParams(params) {
    let otherSearchParams = this.data.otherSearchParams;
    if (otherSearchParams) {
      // 把其他查询条件拼到查询实体上去
      params = Object.assign(params, otherSearchParams);
    }
    return params;
  },
  /** 
   * @desc : 查询 
   * @date : 2024年3月8日
   * @author : 周兴
   */
  getData(params) {
    // 只有加载完毕才会走查询
    if (!this.data.loadFlag) {
      return;
    }
    let service = this.data.service
    let method = this.data.method
    if (!service || !method || !service[method]) {
      return;
    }
    return this.excute(service, service[method], params);
  },
  /**
   * @desc : 加载页面初始方法
   * @author : 周兴
   * @date : 2024/1/22
   */
  loadInit() {
    let _this = this
    if (this.data.menuUuid) {
      let filters = app.globalData.menus.filter(it => it.menuUuid == this.data.menuUuid);
      if (filters && filters.length > 0) {
        this.setData({
          objectCode: filters[0].objectCode,
          title: filters[0].menuName
        })
        // 设置页面标题
        wx.setNavigationBarTitle({
          title: filters[0].menuName,
        })
        this.setData({
          titleText: filters[0].menuName
        })

        // 根据objectCode设置页面各个属性
        this.setPropertis(filters[0].objectCode);
        this.setData({
          loadFlag: true
        })
        wx.nextTick(() => {
          // 查询
          _this.searchData();
        })
      }
    }
  },

  /**
   * @desc : 根据页面编码设置明细表各个属性
   * @author : 周兴
   * @date : 2024/1/22
   */
  setPropertis(objectCode) {
    let pullMenuList = []
    let content = []
    let contentObj = {}
    let contentCol = null
    let footerAmount = {}
    let footerInfo = []
    let routeObjName = ''
    let method = ''
    let otherSearchParams = null
    let placeholder = null
    let listTitle = null
    let listStatus = null
    let listSubTitle = null
    let exportCols = null
    switch (objectCode) {
      // 出库明细表
      case Constants.menuCode.outboundDetailReport:
        routeObjName = 'report'
        method = 'getOutboundDetailReport'
        placeholder = ['orderNo', 'customerName', 'telephone', 'address']
        contentCol = 'outStatusName'
        listTitle = 'outNo'
        listStatus = 'outStatusName'
        listSubTitle = 'outTypeName'
        otherSearchParams = { flgViewItem: true }
        pullMenuList = [
          {
            code: 'outType',
            pullType: 'mSelect',
            typeName: 'outTypeR'
          },
          {
            code: 'outStatus',
            pullType: 'mSelect',
            typeName: 'outStatus'
          },
          {
            code: 'staffName',
          },
          {
            code: 'orgName',
          },
          {
            code: 'skuName',
          },
          {
            code: 'skuModel',
          },
          {
            code: 'nonStdCode',
            title: '色号'
          },
          {
            code: 'brandName',
          },
          {
            code: 'whName',
          },
        ]

        contentObj = {
          '已出库':
            [
              {
                left: { tag: 'brandName', title: 'skuModel', type: 'title' },
                right: { name: 'outDate' },
              },
              {
                left: { name: 'skuName', },
              },
              {
                left: { name: 'outAmt', type: 'price' },
                right: { name: 'outQtyPiece', type: 'qty' }
              },
              {
                left: { name: 'nonStdCode', title: '色号：' },
                right: { name: 'outBoxPiece' },
              },
              {
                left: { name: 'skuSpec', title: '规格：' },
                right: { name: 'whName' },
              }
            ],
          '出库中':
            [
              {
                left: { tag: 'brandName', title: 'skuModel', type: 'title' },
                right: { name: 'outDate' },
              },
              {
                left: { name: 'skuName', },
              },
              {
                left: { name: 'outingAmt', type: 'price' },
                right: { name: 'outingQtyPiece', type: 'qty' }
              },
              {
                left: { name: 'nonStdCode', title: '色号：' },
                right: { name: 'outingBoxPiece' },
              },
              {
                left: { name: 'skuSpec', title: '规格：' },
                right: { name: 'whName' },
              }
            ],
        }
        exportCols = {
          "outNo": "出库单号",
          "fromNo": "来源单号",
          "outTypeName": "出库类型",
          "outStatusName": "出库状态",
          "supplierName": "供应商",
          "cusName": "客户名称",
          "cusPhone": "客户电话",
          "cusFrom": "客户来源",
          "outDate": "出库日期",
          "orgName": "部门名称",
          "staffName": "员工姓名",
          "makeStaffName": "制单员",
          "outingQtySum": "出库中总量",
          "outingAmtSum": "出库中总金额",
          "outQtySum": "已出库总量",
          "outAmtSum": "已出库总金额",
          "returnOutQtySum": "已出库退货总量",
          "returnOutAmtSum": "已出库退货总金额",
          "remarks": "备注",
          "skuName": "商品名称",
          "skuModel": "商品型号",
          "nonStdCode": "色号",
          "shortName": "商品品牌",
          "catName": "种类名称",
          "seriesName": "系列名称",
          "whName": "仓库名称",
          "outingQty": "出库中数量",
          "outingAmt": "出库中金额",
          "outQty": "已出库数量",
          "outAmt": "已出库金额",
          "returnQty": "已出库退货数量",
          "returnAmt": "已出库退货金额"
        }
        break;
      // 入库明细表
      case Constants.menuCode.inboundDetailReport:
        routeObjName = 'report'
        method = 'getInboundDetailReport'
        contentCol = 'intoStatusName'
        otherSearchParams = { flgViewItem: true }
        placeholder = ['orderNo', 'supplierName', 'telephone']
        listTitle = 'intoNo'
        listStatus = 'intoStatusName'
        listSubTitle = 'intoDate'
        pullMenuList = [{
          code: 'intoStatus',
          pullType: 'mSelect',
          typeName: 'intoStatus'
        }, {
          code: 'receiveStatus',
          pullType: 'mSelect',
          typeName: 'receiveStatus'
        }, {
          code: 'followCount',
          dataType: 'number'
        }, {
          code: 'nextFollowTime',
          dataType: 'date'
        }]

        contentObj = {
          '已入库':
            [
              {
                left: { tag: 'brandName', title: 'skuModel', type: 'title' },
              },
              {
                left: { name: 'skuName', },
              },
              {
                left: { name: 'intoAmt', type: 'price' },
                right: { name: 'intoQtyPiece', type: 'qty' }
              },
              {
                left: { name: 'nonStdCode', title: '色号：' },
                right: { name: 'intoBoxPiece' },
              },
              {
                left: { name: 'skuSpec', title: '规格：' },
                right: { name: 'whName' },
              }],
          '入库中':
            [
              {
                left: { tag: 'brandName', title: 'skuModel', type: 'title' },
              },
              {
                left: { name: 'skuName', },
              },
              {
                left: { name: 'intoingAmt', type: 'price' },
                right: { name: 'intoingQtyPiece', type: 'qty' }
              },
              {
                left: { name: 'nonStdCode', title: '色号：' },
                right: { name: 'intoingBoxPiece' },
              },
              {
                left: { name: 'skuSpec', title: '规格：' },
                right: { name: 'whName' },
              }
            ]
        }
        exportCols = {
          "intoNo": "入库单号",
          "fromNo": "来源单号",
          "intoTypeName": "入库类型",
          "intoStatusName": "入库状态",
          "supplierName": "供应商",
          "cusName": "客户名称",
          "cusPhone": "客户电话",
          "cusFrom": "客户来源",
          "intoDate": "入库日期",
          "orgName": "部门名称",
          "staffName": "员工姓名",
          "makeStaffName": "制单员",
          "intoingQtySum": "入库中总量",
          "intoingAmtSum": "入库中总金额",
          "intoQtySum": "已入库总量",
          "intoAmtSum": "已入库总金额",
          "returnInQtySum": "已入库退货总量",
          "returnInAmtSum": "已入库退货总金额",
          "remarks": "备注",
          "skuName": "商品名称",
          "skuModel": "商品型号",
          "nonStdCode": "色号",
          "shortName": "商品品牌",
          "catName": "种类名称",
          "seriesName": "系列名称",
          "whName": "仓库名称",
          "intoingQty": "入库中数量",
          "intoingAmt": "入库中金额",
          "intoQty": "已入库数量",
          "intoAmt": "已入库金额",
          "returnQty": "已出库退货数量",
          "returnAmt": "已出库退货金额"
        }
        break;
      // 销售明细表
      case Constants.menuCode.orderDetailReport:
        routeObjName = 'report'
        method = 'getSalesItemReport'
        placeholder = ['orderNo', 'customerName', 'telephone']
        listTitle = 'orderNo'
        listStatus = 'outStatusName'
        listSubTitle = 'cusPhone'
        pullMenuList =
          [
            {
              code: 'outStatus',
              pullType: 'mSelect',
              typeName: 'outStatus'
            },
            {
              code: 'skuName',
            },
            {
              code: 'skuModel',
            },
            {
              code: 'nonStdCode',
              title: '色号'
            },
            {
              code: 'brandName',
            },
            {
              code: 'isFlgGift',
              title: '是否赠品',
              dataType: 'checkbox'
            },
            {
              code: 'isExtCo',
              title: '外协品查询',
              dataType: 'checkbox'
            },
          ]

        content = [
          {
            left: { tag: 'brandName', title: 'skuModel', type: 'title' },
          },
          {
            left: { name: 'skuName', },
          },
          {
            left: { name: 'priceSale', type: 'price' },
            right: { name: 'itemQtyPiece', type: 'qty' }
          },
          {
            left: { name: 'nonStdCode', title: '色号：' },
            right: { name: 'boxPiece' },
          },
          {
            left: { name: 'skuSpec', title: '规格：' },
            right: { name: 'whName' },
          }
        ]
        exportCols = {
          "orderNo": "订单单号",
          "cusName": "客户名称",
          "cusPhone": "客户电话",
          "contactName": "联系人",
          "contactPhone": "联系电话",
          "orderStatusName": "订单状态",
          "addressFull": "详细地址",
          "deliveryDate": "送货日期",
          "orgName": "部门名称",
          "staffName": "员工姓名",
          "channelName": "销售渠道",
          "cusFromName": "客户来源",
          "makeStaffName": "制单员",
          "makeTime": "制单时间",
          "sumQuantity": "商品总数量",
          "sumStandard": "标价合计",
          "sumAmount": "合计金额",
          "saleDiscount": "销售折扣",
          "amtReceivable": "应收金额",
          "amtHandle": "已核销金额",
          "amtResidue": "剩余金额",
          "remarks": "备注",
          "skuName": "商品名称",
          "skuModel": "商品型号",
          "nonStdCode": "色号",
          "shortName": "商品品牌",
          "catName": "种类名称",
          "seriesName": "系列名称",
          "outNo": "出库单号",
          "outStatusName": "出库状态",
          "whName": "仓库名称",
          "itemQty": "商品数量",
          "priceStd": "标价",
          "amtStd": "标价金额",
          "priceSale": "售价",
          "itemAmt": "售价金额",
          "priceDiscount": "折扣",
          "outQty": "已出库数量",
          "outAmt": "已出库金额"
        }
        break;
      // 采购明细表
      case Constants.menuCode.purDetailReport:
        routeObjName = 'report'
        method = 'getPurDetailReport'
        otherSearchParams = { flgViewItem: true }
        placeholder = ['purNo', 'supName', 'telephone']
        listTitle = 'purNo'
        listStatus = 'intoStatus'
        listSubTitle = 'pickupDate'
        pullMenuList = [
          {
            code: 'outStatus',
            pullType: 'mSelect',
            typeName: 'outStatus'
          },
          {
            code: 'receiveStatus',
            pullType: 'mSelect',
            typeName: 'receiveStatus'
          },
          {
            code: 'followCount',
            dataType: 'number'
          },
          {
            code: 'nextFollowTime',
            dataType: 'date'
          }]

        content = [
          {
            left: { tag: 'brandName', title: 'skuModel', type: 'title' },
          },
          {
            left: { name: 'skuName', },
          },
          {
            left: { name: 'pricePur', type: 'price' },
            right: { name: 'itemQtyPiece', type: 'qty' }
          },
          {
            left: { name: 'nonStdCode', title: '色号：' },
            right: { name: 'boxPiece' },
          },
          {
            left: { name: 'skuSpec', title: '规格：' },
            right: { name: 'whName' },
          }
        ]
        exportCols = {
          purNo: "采购订单号",
          pickupDate: "取货日期",
          purStatusName: "订单状态",
          purTypeName: "采购单据类型",
          orgName: "部门名称",
          staffName: "员工姓名",
          supplierName: "供应商",
          contactName: "联系人",
          contactPhone: "联系电话",
          amtPayable: "应付金额",
          amtResidue: "剩余金额",
          makeStaffName: "制单员",
          skuModel: "商品型号",
          skuName: "商品名称",
          catName: "种类名称",
          itemQty: "商品数量",
          pricePur: "采购价",
          intoAmt: "已入库金额",
          intoQty: "已入库数量",
          seriesName: "系列名称",
          shortName: "商品品牌",
          nonStdCode: "色号",
          remarks: "备注"
        }
        break;
      // 客户收款明细表
      case Constants.menuCode.cusReceiptDetailReport:
        routeObjName = 'report'
        method = 'getRecPayDetailReport'
        placeholder = ['rpNo', 'customerName', 'telephone']
        listTitle = 'rpNo'
        listStatus = 'rpTypeName'
        listSubTitle = 'accDate'
        pullMenuList = [{
          code: 'rpType',
          pullType: 'mSelect',
          typeName: 'rpType'
        },
        {
          code: 'staffName',
        },
        {
          code: 'orgName',
        },]

        content = [
          {
            left: { name: 'cusName' },
            right: { name: 'cusPhone' },
          },
          {
            left: { name: 'orgName' },
            right: { name: 'staffName' },
          },
          {
            left: { name: 'amtRec', type: 'price' },
            right: { name: 'macName' },
          },
          {
            left: { name: 'pickupDate', title: '日期：' },
          }]
        exportCols = {
          "rpNo": "收款单号",
          "rpTypeName": "收付款类型",
          "cusName": "客户名称",
          "cusPhone": "客户电话",
          "contactName": "联系人",
          "contactPhone": "联系电话",
          "addressFull": "详细地址",
          "accDate": "账务日期",
          "macName": "账户名称",
          "orgName": "部门名称",
          "staffName": "员工姓名",
          "makeStaffName": "制单员",
          "makeTime": "制单时间",
          "remarks": "备注"
        }
        break;
      default:
        break;
    }
    this.setData({
      placeholder: placeholder ? util.setSearchPlaceholder(this.data.$t, placeholder) : null,
      listTitle: listTitle,
      listStatus: listStatus,
      listSubTitle: listSubTitle,
      routeObjName: routeObjName,
      method: method,
      pullMenuList: pullMenuList,
      content: content,
      footerAmount: footerAmount,
      footerInfo: footerInfo,
      otherSearchParams: otherSearchParams,
      exportCols: exportCols,
      contentCol: contentCol,
      contentObj: contentObj,
      service: app.globalData[routeObjName + 'Service']
    })
  }
})