// package-basic-data/pages/test/add/add.js
const mixins = require('@/mixins/index.js')
const utils = require('@/utils/util');
const app = getApp()
const Constants = require('@/utils/Constants');
Page({
    mixins: [mixins],
    /**
     * 页面的初始数据
     */
    data: {
        draftFlag: false,
        purIdArray: '', //来源单id 
        // 弹出按钮
        buttonList: [],

        btnFormData: null,
        popupTopObj: [{
            leftTitle: mixins.$t('storageNum'),//库存量',
            leftCode: 'usableQty',
            rightTitle: mixins.$t('sellableQuantity'),//'可售量',
            rightCode: 'invQty'
        }],
        popContent: [{
                code: 'itemQty',
                type: 'step',
                title: mixins.$t('countedQuantity'),//'盘点数量', 
                required: true
            },
            {
                code: 'balanceQty',
                title: mixins.$t('profitAndLossQuantity'),//'盈亏数量', 
                type: 'str',
                readonly: true,
            },
            {
                code: 'lossReason',
                name: 'lossReasonName',
                title: mixins.$t('inventoryLossReasons'),//'盘亏原因',
                type: 'choose',
                urlKey: 'openingReason'
            },
            {
                code: 'whId',
                name: 'whName',
                title: mixins.$t('warehouseId'),// '仓库',
                type: 'choose',
            },
            {
                code: 'nonStdCode',
                title: mixins.$t("goodsBatch"), //'商品批次',
                type: 'str',
                readonly: true,
            },
        ],

        cardList: [{
                name: 'main',
                title: mixins.$t("inventoryInformation"), //'盘点信息 
            },
            {
                name: 'items',
                title: mixins.$t("goodsDetail"), //'商品明细',
                info: '(' + mixins.$t("warehousingProcessing") + ')',
                color: '#E4002B',
                displayNum: 2
            }, {
                name: 'other',
                title: mixins.$t("otherInfo"), // '其他信息'
            }
        ],
        contentObj: {
            main: [{
                    code: 'staffId',
                    name: 'staffName',
                    title: mixins.$t("inventoryEmployees"), //'盘点员工', 
                    type: 'choose',
                    required: true,
                    urlKey: 'chooseStaff'
                }, {
                    code: 'orgId',
                    name: 'orgName',
                    title: mixins.$t("inventoryOrg"), //'盘点部门',  
                    type: 'choose',
                    required: true,
                    urlKey: 'chooseOrg'
                },
                {

                    code: 'whId',
                    name: 'whName',
                    type: 'choose',
                    title: mixins.$t("inventoryWh"), //'盘点仓库' 
                    urlKey: 'chooseWh'
                }
            ],

            items: [],
            other: [{
                code: 'remarks',
                type: 'textarea',
                title: mixins.$t("remarks"), //'备注',
                readonly: false
            }, {
                code: 'file',
                type: 'uploader',
                title: mixins.$t("uploader"), //'附件'
            }]
        },
        // 路由
        routeObjName: 'check', 

        // inboundProcessingService: app.globalData['inboundProcessingService'],
    },

    /*
     * @desc   : 绑定数据
     * @author : 王英杰
     * @date   : 2024年3月12日
     */
    chooseData(e) {
        console.log("t", e, this.data.formData);
        let formData = JSON.parse(this.data.formData)
        let code = e.detail.code
        let data = e.detail.data.data

        if (code == "staffId") { //员工
            formData.staffId = data.id
            formData.staffName = data.name
        }
        if (code == "orgId") { //员工
            formData.orgId = data.id
            formData.orgName = data.name
        }
        if (code == "whId") { //仓库
            formData.whId = data.id
            formData.whName = data.name

            if (formData.goodsList) {
                formData.goodsList.forEach(element => { //总单的入库仓库 作为一个总开关 直接改变 所以品的仓库
                    element.whId = data.id

                    element.whName = data.name
                });
            }
        }
        this.setData({
            formData: JSON.stringify(formData)
        })
    },

    /**
     * @desc : 外露设置产品方法 
     * @date : 2024/2/1 15:49
     * @author : 王英杰
     */
    setGoods(e) { 
        let formData = JSON.parse(this.data.formData) 
        formData.goodsList.forEach(element => {
            element.balanceQty = element.itemQty - element.invQty
        });
        this.setData({
            formData: JSON.stringify(formData)
        })
    },
    /**
     * @desc : 保存数据服务--校验
     * @date : 2024/2/1 15:49
     * @author : 王英杰
     */ 
    validData() {
        if (this.data.draftFlag) {
            return true;
        }
        let formData = JSON.parse(this.data.formData)
        let that = this

        if (formData.goodsList == null || formData.goodsList.length == 0) {
            util.showToast(mixins.$t("chooseGoodsInfo"));
            return false
        }

        for (let i = 0; i < formData.goodsList.length; i++) {
            if (formData.goodsList[i].balanceQty < 0) { //盘亏需要盘亏原因
                if (!formData.goodsList[i].lossReason) {
                    util.showToast(mixins.$t("chooseLnventoryLossReasons"));
                    return false
                }
            }
        }


        return true
    },
    /**
     * @desc : 保存的參數設置
     * @date : 2024/2/1 15:49
     * @author : 王英杰
     */
    setParams(params) {
        let formData = JSON.parse(this.data.formData)
        let draftFlag = this.data.draftFlag
        let itemList = []
        formData.goodsList.forEach((element, index) => {
            let item = {}
            item.itemIndex = index
            item.skuId = element.skuId
            item.invId = element.invId
            item.invQty = element.invQty //库存数量
            item.checkQty = element.itemQty //盘点数量
            item.balanceQty = element.itemQty - element.invQty //差异数量
            item.staffId = formData.staffId //责任人
            item.priceInto = element.pricePurchase //单价 
            item.intoingAmt = element.pricePurchase * element.itemQty //单价
            itemList.push(item)
        });
        params.draftFlag = this.data.draftFlag
        params.staffId = formData.staffId
        params.remarks = formData.remarks
        params.orgId = formData.orgId
        params.flgLock = false //锁库标识 todo 不知道为啥
        if (formData.checkStatus == Constants.checkStaus.COMPLETE) {
            params.checkStatus = formData.checkStatus
        } else {
            params.checkStatus = draftFlag ? Constants.checkStaus.STAGING : Constants.checkStaus.COMPLETE
        }
        params.makeStaff = app.globalData.user.staffId
        params.itemList = itemList
        return params;
    },

    /**
     * @desc : 保存数据服务
     * @date : 2024/2/1 15:49
     * @author : 王英杰
     */
    saveData() {
        let formData = JSON.parse(this.data.formData)
        if (this.data.formMode == "edit") { //编辑
            return this.excute(this.data.service, this.data.service.update, this.data.params)
        } else { //新建 或者暂存
            return this.excute(this.data.service, this.data.service.insert, this.data.params)
        }

    },
    /**
     * @desc : 给表单赋值
     * @date : 2024/2/1 15:49
     * @author : 王英杰
     */
    setValuesByEdit(data) {
        let formData = JSON.parse(this.data.formData)
        let readonly = this.data.readonly || false
        let buttonList = this.data.buttonList
        formData = data.data
        formData.goodsList = data.dataItem
        formData.goodsList.forEach(element => {
            element.itemQty = element.checkQty
            element.pricePurchase = element.priceInto
        });

        wx.setNavigationBarTitle({
            title: '编辑库存盘点', // mixins.$t('supplierAdd')
        })
        if (formData.checkStatus == Constants.checkStaus.COMPLETE) {
            buttonList = [{
                code: 'edit',
                name: 'merge',
                title: mixins.$t("edit"), //'保存'
            }]
            readonly = true
        } else {
            buttonList = [{
                code: 'draft',
                name: 'merge',
                color: '#636FB4',
                title: mixins.$t("staging"), //'暂存'
            }, {
                code: 'save',
                name: 'merge',
                title: mixins.$t("save"), //'保存'
            }]
        }
        this.setData({
            formData: JSON.stringify(formData),
            buttonList: buttonList,
            readonly: readonly
        })
        // 刷新商品明细 显示 查看更多
        let card = this.data.cardList.filter(it => it.name == 'items');
        this.selectComponent('#' + this.data.formName).handleContentObj(card[0])
    },
    /**
     * @desc : 初始化数据
     * @date : 2024/2/1 15:49
     * @author : 王英杰
     */
    loadInit() {
        let formData = JSON.parse(this.data.formData)
        let item = this.data.item || []
        let buttonList = this.data.buttonList 
        if (item.length > 0) {//新建选品进来的走这
            formData.goodsList = item
            formData.goodsList.forEach(element => {
                element.balanceQty = element.itemQty - element.invQty
            });
            buttonList = [{
                    code: 'draft',
                    name: 'merge',
                    color: '#636FB4',
                    title: mixins.$t("staging"), //'暂存'
                }, {
                    code: 'save',
                    name: 'merge',
                    title: mixins.$t("save"), //'保存'
                }],
                this.setData({
                    buttonList: buttonList,
                    formData: JSON.stringify(formData)
                })
        }



    },
    /**
     * @desc : 按钮点击事件回调
     * @date : 2024/2/1 15:49
     * @author : 王英杰
     */
    open(e) {
        console.log('ee', e);
        let that = this
        let code = e.detail.name

        let draftFlag = false;
        if (code == 'draft') { //暂存
            draftFlag = true;
            this.setData({
                validFlag: false
            })
        }
        this.setData({
            draftFlag: draftFlag
        })
        this.save({});
    },
})