// pages/workBench/components/recharge/components/waitApprove/index.js
import {
    alertInfo,
    alertSuccess,
    loadingFunc
} from "../../../../../../utils/index";
import {
    getRechargeApproveList,
    rechargeApprove
} from "../../../../../../service/recharge"
import request from "../../../../../../utils/request"
import dayjs from "../../../../../../utils/dayjs"
const {
    OK
} = request

// pages/workBench/components/record/index.js
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        status: Number
    },

    /**
     * 组件的初始数据
     */
    data: {
        keyword: "",
        keywordTemp: "",
        page: 1,
        currentActionSheet: null,
        actionItems: [{
            name: '查看详细',
            value: 'detail'
        }],
    },

    lifetimes: {
        attached() {
            loadingFunc(async () => {
                await this.init();
            })
        }
    },
    /**
     * 组件的方法列表
     */
    methods: {
        onParkFocus(e) {
            this.setData({
                show: true,
                title: "园区",
                type: 'park'
            })
        },
        onConfirm(e) {
            const {
                data
            } = e.detail;
            const that = this;
            this.setData({
                parkName: data.name,
                park: data.id,
            }, () => {
                loadingFunc(async () => {
                    await that.init();
                })
            })
            this.onCancel();
        },
        onSearchKeyword(e) {
            const that = this;
            that.setData({
                keyword: e.detail
            }, () => {
                loadingFunc(async () => {
                    await that.init();
                })
            })
        },
        onChangeKeyword(e) {
            this.setData({
                keywordTemp: e.detail
            })
        },
        onSearch() {
            const that = this;
            that.setData({
                keyword: that.data.keywordTemp
            }, () => {
                loadingFunc(async () => {
                    await that.init();
                })
            })
        },
        async init() {
            const {
                page,
                keyword,
                park,
                status
            } = this.data;
            const {
                code,
                message,
                data,
                total
            } = await getRechargeApproveList({
                park,
                page,
                keyword,
                status
            })
            if (code !== OK) {
                alertInfo(message);
                return;
            }
            this.setData({
                list: data.map(item => {
                    item.topTime = dayjs(item.topTime).format("MM-DD")
                    return item
                }),
                total: total,
                totalPage: Math.ceil(total / 20)
            })
        },
        onChangeReason(e) {
            this.setData({
                reason: e.detail,
            })
        },
        onApproveClose() {
            this.setData({
                reason: "",
                approveShow: false,
            })
        },
        onApproveConfirm() {
            const {
                reason,
                record
            } = this.data;
            const that = this;
            if (!reason) {
                alertInfo("请输入拒绝理由")
                return;
            }
            loadingFunc(async () => {
                const {
                    code,
                    message
                } = await rechargeApprove({
                    id: record,
                    status: 1,
                    reason
                })
                if (code !== OK) {
                    alertInfo(message)
                    return;
                }
                alertSuccess("已拒绝")
                that.init();
                that.setData({
                    reason: "",
                    approveShow: false,
                    record: "",
                })
            })

        },
        handleApprove(e) {
            const status = e.currentTarget.dataset.status;
            const that = this;
            if (status === '1') {
                this.setData({
                    approveShow: true
                })
                return;
            }
            const {
                record,
                list
            } = this.data;
            const item = list.find(ele => ele.id === record)

            wx.showModal({
                title: '提示',
                content: `您确认要同意${item?.tenement?.shortName || 当前记录}吗？`,
                complete: async (res) => {
                    if (res.cancel) {

                    }
                    if (res.confirm) {
                        loadingFunc(async () => {
                            const {
                                code,
                                message
                            } = await rechargeApprove({
                                id: record,
                                status: 0,
                            })
                            if (code !== OK) {
                                alertInfo(message)
                                return;
                            }
                            alertSuccess("已同意")
                            that.init();
                        })

                    }
                }
            })
        },

        showActionMenu(e) {
            this.setData({
                currentActionSheet: e.currentTarget.dataset.id
            });
        },

        hideActionMenu() {
            this.setData({
                currentActionSheet: null
            });
        },

        onMenuSelect(e) {
            const {
                value
            } = e.detail;
            const id = e.currentTarget.dataset.id;

            if (value === 'detail') {
                this.jumpToDetail({
                    currentTarget: {
                        dataset: {
                            id
                        }
                    }
                });
            }
        },

        jumpToDetail(e) {
            const {
                id
            } = e.currentTarget.dataset;
            wx.navigateTo({
                url: '/pages/rechargeDetail/index?id=' + id,
            })
        },
        async onChangePage(e) {
            const page = e.detail.currentIndex;
            const that = this;
            this.setData({
                page
            }, () => {
                loadingFunc(async () => {
                    await that.init();
                })
            })
        },
        onCancel() {
            this.setData({
                show: false,
                title: "",
                type: "",
            })
        },
        onChangeSelectRecharge(e) {
            this.setData({
                record: e.detail
            })
        }
    }
})