<template>
    <div class="app-container">
        <el-card>
            <el-descriptions :column="2" direction="vertical" :label-style="{ 'text-align': 'center' }" :content-style="{ 'text-align': 'center' }">
                <el-descriptions-item label="顺丰同城余额（元）"
                    ><span style="font-size: 40px; color: red; font-weight: bold">{{ balanceInfo.sftc / 100 }}</span></el-descriptions-item
                >
                <el-descriptions-item label="达达配送余额（元）"
                    ><span style="font-size: 40px; color: red; font-weight: bold">{{ balanceInfo.dada / 100 }}</span></el-descriptions-item
                >
            </el-descriptions>
        </el-card>
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" style="margin-top: 10px">
            <el-form-item label="查询日期">
                <el-date-picker v-model="queryParams.searchDate" style="width: 240px" value-format="yyyy-MM-dd" type="date" placeholder="选择日期"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" @click="handleCash">充值</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-tabs v-model="queryParams.flow_type" @tab-click="onChangeTabs" type="border-card">
            <el-tab-pane label="充值流水" name="1"></el-tab-pane>
            <el-tab-pane label="消费流水" name="2"></el-tab-pane>

            <el-table v-loading="loading" fit highlight-current-row border stripe :data="dataSource" style="margin-top: 10px">
                <el-table-column label="支付金额（元）" align="center" prop="pay_amount">
                    <template slot-scope="scope">
                        {{ scope.row.pay_amount / 100 }}
                    </template>
                </el-table-column>
                <el-table-column label="支付时间" align="center" prop="pay_time">
                    <template slot-scope="scope">
                        {{ transDate(scope.row.pay_time) }}
                    </template>
                </el-table-column>
                <el-table-column label="支付状态" align="center" prop="pay_status">
                    <template slot-scope="scope">
                        {{ scope.row.pay_status == 'SUCCESS' ? '支付成功' : '支付失败' }}
                    </template>
                </el-table-column>
                <!--        <el-table-column label="支付状态" align="center" v-if="tabs=='0'">-->
                <!--          <template slot-scope="scope">-->
                <!--            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.hasPay"/>-->
                <!--          </template>-->
                <!--        </el-table-column>-->
            </el-table>
        </el-tabs>

        <el-dialog :visible.sync="cashOpen" width="50%" title="充值配送余额">
            <div>
                <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="small">
                    <el-form-item label="快递公司" prop="service_trans_id">
                        <el-radio-group v-model="form.service_trans_id">
                            <el-radio-button v-for="dict in dict.type.express_service_type" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
                        </el-radio-group>
                    </el-form-item>

                    <el-form-item label="充值金额" prop="amount">
                        <el-radio-group v-model="form.amount">
                            <el-radio key="50" label="50" border>50元</el-radio>
                            <el-radio key="100" label="100" border>100元</el-radio>
                            <el-radio key="200" label="200" border>200元</el-radio>
                            <el-radio key="500" label="500" border>500元</el-radio>
                            <el-radio key="1000" label="1000" border>1000元</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="cashOpen = false" v-if="isPaying">已完成支付</el-button>
                <el-button type="primary" @click="submitForm" v-else>确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { intracityExpressGetStoreBalance, intracityExpressQueryFlow, intracityExpressChargeBalance } from '@/api/express/intracity'
import { formatDate } from '../../utils'

export default {
    name: 'manageDay',
    dicts: ['express_service_type'],
    data() {
        return {
            cashOpen: false,
            isPaying: true,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            clothTotal: 0,
            goodsTotal: 0,
            dataSource: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                flow_type: '1'
            },
            createTimeRange: [],
            balanceInfo: {
                sftc: 0,
                dada: 0
            },
            pickerOptions: {
                onPick: this.getPickDate,
                disabledDate: this.disabledDate
            },
            pickDate: '', // 存放getPickDate获取的数据
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                title: [{ required: true, message: '标题不能为空', trigger: 'blur' }]
            }
        }
    },
    created() {
        this.queryParams.searchDate = formatDate(new Date().getTime())
        intracityExpressGetStoreBalance().then((res) => {
            res.data.balance_detail.forEach((item) => {
                if (item.service_trans_id == 'DADA') {
                    this.balanceInfo.dada = item.balance
                }
                if (item.service_trans_id == 'SFTC') {
                    this.balanceInfo.sftc = item.balance
                }
            })
        })
        this.getList()
    },
    methods: {
        onChangeTabs() {
            this.handleQuery()
        },
        getList() {
            this.loading = true
            intracityExpressQueryFlow(this.queryParams).then((res) => {
                this.dataSource = res.data.flow_list
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.createTimeRange = null
            this.queryParams.beginCreateTime = null
            this.queryParams.endCreateTime = null
            this.resetForm('queryForm')
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        },
        handleCash() {
            this.cashOpen = true
            this.isPaying = false
        },
        submitForm() {
            var ctx = this
            this.$confirm('确定选择好充值内容吗？')
                .then(function () {
                    ctx.$refs['form'].validate((valid) => {
                        if (valid) {
                            intracityExpressChargeBalance(ctx.form).then((res) => {
                                window.open(res.data.payurl)
                                ctx.cashOpen = false
                            })
                        }
                    })
                })
                .catch(() => {})
        },
        transDate(pay_time) {
            return formatDate(pay_time * 1000, 1)
        }
    }
}
</script>
