const _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    CNY = value => currency(value, {
        symbol: "￥",
        precision: 2
    }).format();
import {
    formatTime
} from "../../utils/getTime";
Page({
    data: {
        accountList: [],
        active: 0,
        dateEnd: "", //日期结束时间
        begindate: "",
        enddate: "",
    },
    onLoad(options) {
        let usertype = wx.getStorageSync('userMsg').usertype
        this.setData({
            dateEnd: formatTime(new Date(), '-').split(" ")[0],
            usertype
        });
        if (usertype == 1) {
            _Http.basic({
                "id": 20240111085504,
                "content": {
                    "hrid": wx.getStorageSync('userMsg').hrid,
                    "pageSize": 999,
                    "pageNumner": 1
                }
            }).then(res => {
                console.log("usertype1", res)
                this.setData({
                    accountList: res.data.map(v => {
                        v.balance = CNY(v.balance)
                        v.expectedReward = CNY(v.expectedReward)
                        v.accountname = v.hraccountname
                        return v
                    })
                });
                this.getList(true);
            })
        } else {
            _Http.basic({
                id: 20221008145903,
                "content": {
                    pageSize: 999,
                    type: 1,
                    "where": {
                        "condition": ""
                    }
                }
            }).then(res => {
                console.log(res)
                this.setData({
                    accountList: res.data.map(v => {
                        v.creditquota = CNY(v.creditquota);
                        v.usable = CNY(currency(v.creditquota).add(v.balance));
                        v.balance = CNY(v.balance)
                        return v
                    })
                });
                this.getList(true);
            })
        }
    },
    /* 切换查看账户 */
    changeAccount(e) {
        this.setData({
            active: e.detail.value || e.detail.current
        });
        this.getList();
    },
    /* 切换查看时间 */
    changeDate(e) {
        this.setData({
            [`${e.currentTarget.dataset.name}`]: e.detail.value
        })
        this.getList(true);
    },
    getList() {
        let data = this.data.accountList[this.data.active];
        let params = this.data.usertype == 1 ? {
            "id": 20240111090304,
            "content": {
                "pageSize": 999999,
                "pageNumber": 1,
                "hrid": wx.getStorageSync('userMsg').hrid,
                "where": {
                    "condition": "",
                    begindate: this.data.begindate,
                    enddate: this.data.enddate,
                    "type": "",
                    "sa_hraccountclassid": data.sa_hraccountclassid
                }
            }
        } : {
            "id": "20230111103403",
            "version": 1,
            "content": {
                "sys_enterpriseid": data.sys_enterpriseid,
                "sa_accountclassid": data.sa_accountclassid,
                "where": {
                    begindate: this.data.begindate,
                    enddate: this.data.enddate
                }
            }
        };

        _Http.basic(params).then(res => {
            console.log("账户流水", res)
            if (res.msg == '成功') {
                let expend = 0, //收入
                    earning = 0; //支出
                if (res.data.total.length) {
                    let expendObj = res.data.total.find(v => v.type == 1);
                    if (expendObj) expend = CNY(expendObj.sumamount);
                    let earningObj = res.data.total.find(v => v.type == 0);
                    if (earningObj) earning = CNY(earningObj.sumamount);
                }
                this.setData({
                    recordList: res.data.rows.map(v => {
                        v.amount = CNY(v.amount)
                        v.showBalance = CNY(v.balance)
                        v.showRemarks = wx.getStorageSync('userMsg').usertype == 1 ? v.allremarks : v.remarks
                        return v
                    }),
                    expend,
                    earning
                })
            }
        })
    },
    onReady() {

    },
    toDetail(e) {
        let {
            item
        } = e.currentTarget.dataset;
        item.enddate = this.data.enddate;
        item.begindate = this.data.begindate;
        wx.navigateTo({
            url: '/packageA/account/detail?item=' + JSON.stringify(item)
        })
    }
})