<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label-width="120px" prop="orgId" v-if="userInfoVO.userType == '00'">
                <el-select v-model="queryParams.orgId" filterable placeholder="请选择" clearable>
                    <el-option-group label="门店">
                        <template>
                            <el-option v-for="store in storeList" :key="store.id" :label="store.name" :value="store.id"> </el-option>
                        </template>
                    </el-option-group>
                </el-select>
            </el-form-item>
            <el-form-item prop="orderNo">
                <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item prop="orderStatus">
                <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
                    <el-option v-for="dict in dict.type.activity_order_status" :key="dict.code" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item prop="realName">
                <el-input v-model="queryParams.realName" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item prop="phoneNumber">
                <el-input v-model="queryParams.phoneNumber" placeholder="请输入客户手机号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item prop="activityTitle">
                <el-input v-model="queryParams.activityTitle" placeholder="请输入活动名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-date-picker clearable v-model="queryPayTime" type="daterange" value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" @change="getPayTime"> </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-date-picker clearable v-model="queryCreateTime" type="daterange" value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" @change="getCreateTime"> </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="warning" plain icon="el-icon-download" @click="handleExport" v-hasPermi="['order:activity:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <Page uri="/mapi/order/activity/list" :request-params="queryParams" ref="pagination">
            <el-table-column label="编号id" align="center" prop="id" width="180px" />
            <el-table-column label="客户姓名" align="center" prop="realName" width="100px" />
            <el-table-column label="客户手机号" align="center" prop="phoneNumber" width="120px" />
            <el-table-column label="门店名称" align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.sourceType === '01'">
                        {{ scope.row.factoryName }}
                    </span>
                    <span v-if="scope.row.sourceType === '02'">
                        {{ scope.row.storeName }}
                    </span>
                    <span v-if="scope.row.sourceType === '00'"> </span>
                </template>
            </el-table-column>
            <el-table-column label="活动名称" align="center" prop="activityTitle" width="100px" :show-tooltip-when-overflow="true" />
            <el-table-column label="订单编号" align="center" prop="orderNo" width="200px" />
            <el-table-column label="订单状态" align="center" prop="orderStatus">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.orderStatus == dict.value" v-for="dict in dict.type.activity_order_status" :key="dict.code"> {{ dict.label }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="支付方式" align="center" prop="payType">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.payType == '0'">微信</el-tag>
                    <el-tag v-if="scope.row.payType == '1'">支付宝</el-tag>
                    <el-tag v-if="scope.row.payType == '2'">现金</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="支付金额" align="center" prop="payAmount" />
            <el-table-column label="支付时间" align="center" prop="payTime" width="180px"> </el-table-column>
            <el-table-column label="下单时间" align="center" prop="createTime" width="180px"> </el-table-column>
            <!--      <el-table-column label="下单渠道" align="center" prop="orderSource" />-->
            <!--      <el-table-column label="支付宝/微信支付订单号" align="center" prop="outTradeOrderNo" />-->

            <!--      <el-table-column label="退款申请时间" align="center" prop="refundApplyTime" width="180">-->
            <!--        <template slot-scope="scope">-->
            <!--          <span>{{ parseTime(scope.row.refundApplyTime, '{y}-{m}-{d}') }}</span>-->
            <!--        </template>-->
            <!--      </el-table-column>-->
            <!--      <el-table-column label="退款时间" align="center" prop="refundTime" width="180">-->
            <!--        <template slot-scope="scope">-->
            <!--          <span>{{ parseTime(scope.row.refundTime, '{y}-{m}-{d}') }}</span>-->
            <!--        </template>-->
            <!--      </el-table-column>-->
            <!--      <el-table-column label="退款金额" align="center" prop="refundAmount" />-->
            <!--      <el-table-column label="退款描述" align="center" prop="refundContent" />-->
            <!--      <el-table-column label="备注" align="center" prop="remark" />-->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-chat-dot-square" @click="refundOrder(scope.row)" v-if="userInfoVO.userType == '00' && scope.row.orderStatus != '0' && scope.row.orderStatus != '6'">撤单</el-button>
                </template>
            </el-table-column>
        </Page>

        <!-- 添加或修改活动订单对话框 -->
        <el-dialog :title="title" :visible.sync="open" size="30%" append-to-body>
            <el-row :gutter="15">
                <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                    <el-col :span="24">
                        <el-form-item label="订单编号" prop="orderNo">
                            <el-input v-model="form.orderNo" placeholder="请输入订单编号" maxlength="50" show-word-limit />
                        </el-form-item>
                        <el-form-item label="门店/工厂ID" prop="orgId">
                            <el-input v-model="form.orgId" placeholder="请输入门店/工厂ID" />
                        </el-form-item>
                        <el-form-item label="客户ID" prop="appUserId">
                            <el-input v-model="form.appUserId" placeholder="请输入客户ID" />
                        </el-form-item>
                        <el-form-item label="支付时间" prop="payTime">
                            <el-date-picker clearable v-model="form.payTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择支付时间"> </el-date-picker>
                        </el-form-item>
                        <el-form-item label="支付金额" prop="payAmount">
                            <el-input v-model="form.payAmount" placeholder="请输入支付金额" />
                        </el-form-item>
                        <el-form-item label="下单渠道" prop="orderSource">
                            <el-input v-model="form.orderSource" placeholder="请输入下单渠道" maxlength="10" show-word-limit />
                        </el-form-item>
                        <el-form-item label="支付宝/微信支付订单号" prop="outTradeOrderNo">
                            <el-input v-model="form.outTradeOrderNo" placeholder="请输入支付宝/微信支付订单号" maxlength="100" show-word-limit />
                        </el-form-item>
                        <el-form-item label="退款申请时间" prop="refundApplyTime">
                            <el-date-picker clearable v-model="form.refundApplyTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择退款申请时间"> </el-date-picker>
                        </el-form-item>
                        <el-form-item label="退款时间" prop="refundTime">
                            <el-date-picker clearable v-model="form.refundTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择退款时间"> </el-date-picker>
                        </el-form-item>
                        <el-form-item label="退款金额" prop="refundAmount">
                            <el-input v-model="form.refundAmount" placeholder="请输入退款金额" />
                        </el-form-item>
                        <el-form-item label="退款描述">
                            <editor v-model="form.refundContent" :min-height="192" />
                        </el-form-item>
                        <el-form-item label="删除状态" prop="delFlag">
                            <el-input v-model="form.delFlag" placeholder="请输入删除状态" maxlength="1" show-word-limit />
                        </el-form-item>
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" placeholder="请输入备注" maxlength="50" show-word-limit />
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm" style="margin-left: 5%">提交</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!--    撤单页面-->
        <el-dialog :visible.sync="openRefundPage" width="400px" append-to-body>
            <el-form ref="refundVO" :model="refundVO">
                <el-form-item label="撤单原因" label-width="80px" prop="refundReason">
                    <el-input v-model="refundVO.refundReason" type="textarea" placeholder="请输入撤单原因" maxlength="500" :rows="5" show-word-limit></el-input>
                </el-form-item>
            </el-form>
            <el-row :gutter="10">
                <div>
                    <el-button type="primary" @click="submitRefund" style="margin-left: 3%; margin-top: 20px">提交</el-button>
                </div>
            </el-row>
        </el-dialog>
    </div>
</template>

<script>
import { listActivity, getActivity, delActivity, addActivity, updateActivity, updateActivityStatus, refundOrderActivity } from '@/api/order/activity'
import { allOrg } from '@/api/system/store'

export default {
    name: 'Activity',
    dicts: ['activity_order_status'],
    data() {
        return {
            userInfoVO: null,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 活动订单表格数据
            activityList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                realName: null,
                phoneNumber: null,
                activityTitle: null,
                orderNo: null,
                orderStatus: null
            },
            queryCreateTime: null,
            queryPayTime: null,
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            openRefundPage: false,
            refundVO: {
                orderId: null,
                refundReason: null
            },
            storeList: []
        }
    },
    created() {
        this.handleUser()
        allOrg().then((res) => {
            res.data.forEach((org) => {
                if (org.sourceType == '02') {
                    this.storeList.push(org)
                }
            })
        })
        this.getList()
    },
    methods: {
        handleUser() {
            this.userInfoVO = this.getUserInfo()
        },
        getCreateTime() {
            if (this.queryCreateTime != null) {
                this.queryParams.createTimeFrom = this.queryCreateTime[0]
                this.queryParams.createTimeTo = this.queryCreateTime[1]
            }
        },
        getPayTime() {
            if (this.queryPayTime != null) {
                this.queryParams.beginPayTime = this.queryPayTime[0]
                this.queryParams.endPayTime = this.queryPayTime[1]
            }
        },
        /** 查询活动订单列表 */
        getList() {
            this.$nextTick(() => {
                this.$refs.pagination.handleSearch(true)
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                orderNo: null,
                orgId: null,
                sourceType: null,
                appUserId: null,
                payTime: null,
                payAmount: null,
                payType: null,
                orderSource: null,
                outTradeOrderNo: null,
                orderStatus: '0',
                refundApplyTime: null,
                refundTime: null,
                refundAmount: null,
                refundContent: null,
                delFlag: null,
                createById: null,
                createBy: null,
                createTime: null,
                updateById: null,
                updateBy: null,
                updateTime: null,
                remark: null
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryCreateTime = null
            this.queryParams.createTimeFrom = null
            this.queryParams.createTimeTo = null

            this.queryPayTime = null
            this.queryParams.beginPayTime = null
            this.queryParams.endPayTime = null
            this.resetForm('queryForm')
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加活动订单'
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getActivity(id).then((response) => {
                this.form = response.data
                this.open = true
                this.title = '修改活动订单'
            })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateActivity(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addActivity(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal
                .confirm('是否确认删除活动订单编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delActivity(ids)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'mapi/order/activity/export',
                {
                    ...this.queryParams
                },
                `activity_${new Date().getTime()}.xlsx`
            )
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        },
        // 撤单
        refundOrder(row) {
            this.refundVO = {}
            this.openRefundPage = true
            this.refundVO.orderId = row.id
        },
        submitRefund() {
            this.$confirm('是否确认撤回该订单？')
                .then(() => {
                    refundOrderActivity(this.refundVO).then((response2) => {
                        this.$message.success('操作成功')
                        this.getList()
                        this.openRefundPage = false
                        this.refundVO.orderId = null
                        this.refundVO.refundReason = null
                    })
                })
                .catch(() => {})
        }
    }
}
</script>
