<template>
    <div>
        <div class="app-container">
            <el-form
                :model="queryParams"
                ref="queryForm"
                :inline="true"
                v-show="showSearch"
            >
                <el-form-item label="订单编号" prop="orderIndex">
                    <el-input
                        v-model="queryParams.orderIndex"
                        placeholder="请输入订单编号"
                        maxlength="30"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                    />
                </el-form-item>

                <el-form-item
                    label="服务主题"
                    prop="goodsName"
                    style="margin-left: 30px"
                >
                    <el-input
                        v-model="queryParams.goodsName"
                        placeholder="请输入服务主题"
                        maxlength="30"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="服务类型" style="margin-left: 30px">
                    <el-cascader
                        v-model="queryParams.categoryId"
                        :options="categoryList"
                        :props="optionProps"
                        size="small"
                        clearable
                    ></el-cascader>
                </el-form-item>

                <el-form-item label="顾客手机号" prop="serverId">
                    <el-input
                        v-model="queryParams.memberPhone"
                        placeholder="请输入顾客手机号"
                        maxlength="11"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="核销码" prop="orderVerificationCode">
                    <el-input
                        v-model="queryParams.orderVerificationCode"
                        placeholder="请输入核销码"
                        maxlength="30"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                    />
                </el-form-item>

                <el-form-item
                    label="状态"
                    prop="orderState"
                    style="margin-left: 30px"
                >
                    <el-select
                        v-model="queryParams.orderState"
                        placeholder="请选择"
                        size="small"
                    >
                        <el-option
                            v-for="item in statusOptions"
                            :key="item.decodeValue"
                            :label="item.decodeValue"
                            :value="item.decodeId"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="下单日期" prop="createTime">
                    <el-date-picker
                        v-model="dateRange"
                        type="daterange"
                        value-format="yyyy-MM-dd"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        style="width: 280px"
                        size="small"
                    >
                    </el-date-picker>
                </el-form-item>
                <el-form-item style="margin-left: 30px">
                    <el-button
                        type="primary"
                        icon="el-icon-search"
                        size="mini"
                        @click="handleQuery"
                        >搜索</el-button
                    >
                    <el-button
                        icon="el-icon-refresh"
                        size="mini"
                        @click="resetQuery"
                        >重置</el-button
                    >
                </el-form-item>
            </el-form>
            <el-row>
                <el-input
                    v-model="form.orderVerificationCode"
                    style="width: 200px; margin-left: 930px; margin-right: 10px"
                    placeholder="请输入核销码"
                    maxlength="30"
                    clearable
                    size="small"
                />
                <el-button
                    type="primary"
                    size="mini"
                    @click="verifyGroupOrder(form.orderVerificationCode)"
                    >核销</el-button
                >
            </el-row>

            <el-row :gutter="10" class="mb8">
                <right-toolbar
                    :showSearch.sync="showSearch"
                    @queryTable="getList"
                ></right-toolbar>
            </el-row>

            <el-table
                v-loading="loading"
                :data="orderList"
                @selection-change="handleSelectionChange"
            >
                <!-- <el-table-column label="序号" type="index" /> -->
                <el-table-column
                    label="订单编号"
                    align="center"
                    prop="orderIndex"
                    width="180px"
                />

                <el-table-column
                    label="服务编号"
                    align="center"
                    prop="goodsIndex"
                    width="180px"
                />
                <el-table-column
                    label="服务主题"
                    align="center"
                    prop="goodsName"
                    width="120px"
                />
                <el-table-column
                    label="服务类型"
                    align="center"
                    prop="categoryName"
                    width="80px"
                />
                <el-table-column
                    label="规格描述"
                    align="center"
                    prop="specificationDesc"
                    width="150px"
                />
                <el-table-column
                    label="次数(剩余/购买）"
                    align="center"
                    prop="times"
                    width="120"
                >
                </el-table-column>
                <el-table-column
                    label="价格"
                    align="center"
                    prop="specificationCurrentPrice"
                    width="80px"
                />

                <el-table-column
                    label="订单金额"
                    align="center"
                    prop="orderAmount"
                    width="80px"
                />
                <el-table-column
                    label="支付金额"
                    align="center"
                    prop="totalAmount"
                    width="80px"
                />

                <el-table-column
                    label="顾客手机号"
                    align="center"
                    prop="memberPhone"
                    width="120px"
                />

                <el-table-column
                    label="下单时间"
                    align="center"
                    prop="createTime"
                    width="150"
                />

                <el-table-column
                    label="状态 "
                    align="center"
                    prop="refStatus"
                />
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                    fixed="right"
                    width="150"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-view"
                            @click="toSee(scope.row)"
                            v-hasPermi="['system:order:edit']"
                            >查看</el-button
                        >
                        <div v-if="scope.row.orderState == '001'">
                            <el-button
                                size="mini"
                                type="text"
                                icon="el-icon-edit"
                                @click="toVerifyOrder(scope.row)"
                                v-hasPermi="['system:order:edit']"
                                >核销订单</el-button
                            >
                        </div>
                        <div v-if="scope.row.orderState == '000'">
                            <el-button
                                size="mini"
                                type="text"
                                icon="el-icon-coordinate"
                                @click="editRefund(scope.row)"
                                v-hasPermi="['system:order:edit']"
                                >退款</el-button
                            >
                        </div>
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </div>

        <!-- 预约核销弹框 -->
        <el-dialog
            title="预约核销"
            :visible.sync="open"
            width="800px"
            append-to-body
        >
            <el-form
                :model="form"
                label-width="80px"
                ref="userForm"
                :inline="true"
                label-position="left"
                :rules="rules"
            >
                <el-row style="margin-left: 50px">
                    <el-col
                        :span="24"
                        style="font-size: 14px; margin-bottom: 15px"
                    >
                        <!-- <el-col :span="12">
                            <el-input
                                label="核销码"
                                v-model="form.orderVerificationCode"
                                placeholder="请输入核销码"
                                maxlength="30"
                                clearable
                                size="small"
                                @keyup.enter.native="handleQuery"
                            />
                             <span style="text-align: left; margin-right: 10px"
                                >核销码</span
                            >{{ order.orderVerificationCode }}
                        </el-col> -->
                        <el-col :span="12"
                            >顾客购买<span
                                style="
                                    font-weight: 400;
                                    font-size: 20px;
                                    color: #8400ff;
                                "
                                >{{ order.groupTimes }}</span
                            >次服务， 已经使用<span
                                style="
                                    font-weight: 400;
                                    font-size: 18px;
                                    color: #0000ff;
                                "
                                >{{
                                    order.groupTimes - order.groupRemainingTimes
                                }}</span
                            >次， 剩余<span
                                style="
                                    font-weight: 700;
                                    font-size: 16px;
                                    color: #d9001b;
                                "
                                >{{ order.groupRemainingTimes }}</span
                            >次</el-col
                        >
                    </el-col>
                    <el-card
                        :span="24"
                        style="
                            background-color: #f2f2f2;
                            color: white;
                            color: #333333;
                            margin-bottom: 20px;
                        "
                    >
                        <el-col
                            :span="24"
                            style="font-size: 16px; margin-bottom: 15px"
                        >
                            {{ order.goodsName }}
                        </el-col>
                        <el-col
                            :span="24"
                            style="font-size: 14px; margin-bottom: 15px"
                        >
                            <span
                                style="
                                    color: #7f7f7f;
                                    text-align: left;
                                    margin-right: 10px;
                                "
                                >服务类别
                            </span>
                            <span style="color: #333333; text-align: left"
                                >{{
                                    order.parentName == null
                                        ? ""
                                        : order.parentName + "-"
                                }}{{ order.categoryName }}</span
                            >
                        </el-col>
                        <el-col
                            :span="24"
                            style="font-size: 14px; margin-bottom: 15px"
                        >
                            <span
                                style="
                                    color: #7f7f7f;
                                    text-align: left;
                                    margin-right: 10px;
                                "
                                >规格
                            </span>
                            <span style="color: #333333; text-align: left">{{
                                order.specificationDesc
                            }}</span>
                        </el-col>
                    </el-card>
                    <el-card
                        :span="24"
                        style="
                            background-color: #f2f2f2;
                            color: white;
                            color: #333333;
                            margin-bottom: 15px;
                        "
                    >
                        <el-col
                            :span="24"
                            style="font-size: 14px; margin-bottom: 15px"
                        >
                            <span
                                style="
                                    color: #7f7f7f;
                                    text-align: left;
                                    margin-right: 10px;
                                "
                                >顾客信息
                            </span>
                            <span style="color: #333333; text-align: left">
                                {{
                                    memberOrderAddress.name
                                        ? memberOrderAddress.name.replace(
                                              memberOrderAddress.name.substring(
                                                  1,
                                                  2
                                              ),
                                              "*"
                                          )
                                        : ""
                                }}
                            </span>
                            <span>
                                {{
                                    memberOrderAddress.phone
                                        ? memberOrderAddress.phone.replace(
                                              memberOrderAddress.phone.substring(
                                                  3,
                                                  7
                                              ),
                                              "****"
                                          )
                                        : ""
                                }}
                            </span>
                        </el-col>
                        <el-col
                            :span="24"
                            style="font-size: 14px; margin-bottom: 15px"
                        >
                            <span
                                style="
                                    color: #7f7f7f;
                                    text-align: left;
                                    margin-right: 10px;
                                "
                                >服务地址
                            </span>
                            <span style="color: #333333; text-align: left"
                                ><i class="el-icon-location-information" />{{
                                    memberOrderAddress.address
                                }}{{ memberOrderAddress.detailed }}</span
                            >
                        </el-col>
                        <el-col
                            :span="24"
                            style="font-size: 14px; margin-bottom: 15px"
                        >
                            <span
                                style="
                                    color: #7f7f7f;
                                    text-align: left;
                                    margin-right: 10px;
                                "
                                >备注
                            </span>
                            <span style="color: #333333; text-align: left">{{
                                order.memo
                            }}</span>
                        </el-col>
                    </el-card>
                    <el-col :span="24">
                        <el-form-item
                            label="核销码"
                            prop="orderVerificationCode"
                        >
                            <el-input
                                label="核销码"
                                v-model="form.orderVerificationCode"
                                placeholder="请输入核销码"
                                maxlength="30"
                                clearable
                                size="small"
                                :disabled="orderVerificationCodeIsInput"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="上门时间" prop="appointmentTime">
                            <el-date-picker
                                v-model="form.appointmentTime"
                                type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                placeholder="选择日期时间"
                                :picker-options="pickerOptions"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input
                                type="textarea"
                                :rows="6"
                                maxlength="200"
                                style="width: 600px"
                                placeholder="请填写备注，如备注本次服务人员姓名等"
                                show-word-limit
                                v-model="form.remark"
                            >
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col
                        :span="24"
                        style="
                            font-family: 'Nunito Sans', sans-serif;
                            font-weight: 400;
                            font-style: normal;
                            font-size: 12px;
                            color: #333333;
                            margin-left: 80px;
                        "
                    >
                        注：此备注信息不展示给顾客
                    </el-col>
                    <el-col :span="24" style="margin-left: 53%">
                        <el-button @click="open = false">取 消</el-button>
                        <el-button type="primary" @click="submitVerifyOrderTime"
                            >确 定</el-button
                        >
                    </el-col>
                </el-row>
            </el-form>
        </el-dialog>

        <!-- 退款弹框 -->
        <el-dialog
            title="退款"
            :visible.sync="refundOpen"
            width="600px"
            :close-on-click-modal="false"
        >
            <el-form
                :model="refundParams"
                label-width="100px"
                ref="refundForm"
                :inline="true"
                label-position="left"
                style="margin-left: 100px"
                :rules="rules"
            >
                <el-form-item label="退款金额" prop="refundMoney">
                    <el-input v-model="refundParams.refundMoney"></el-input>
                </el-form-item>

                <el-form-item label="退款原因" prop="refundReason">
                    <el-input v-model="refundParams.refundReason"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer">
                <el-button @click="refundOpen = false">取 消</el-button>
                <el-button type="primary" @click="handleRefund"
                    >确 定</el-button
                >
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listGroupOrder,
    getOrder,
    getAddress,
    getOrderByVerificationCode,
} from "@/api/system/order";
import { handleVerifyGroupOrder } from "@/api/system/orderVerificationRecord";
import { merchantRefund } from "@/api/merchant/merchant";
import { categoryTree } from "@/api/basic/category";
import { getInfo } from "@/api/login";
import { listCode } from "@/api/system/dict/code";
export default {
    name: "order",
    data() {
        return {
            // 核销码是否可输入
            orderVerificationCodeIsInput: true,
            // 日期范围
            dateRange: "",
            //服务类别筛选框
            serviceOptions: [],
            //服务状态筛选框
            statusOptions: [],
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 订单主表格数据
            orderList: [],
            // 弹出层标题
            title: "",
            // 是否显示核销弹出层
            open: false,
            // 顾客订单地址信息
            memberOrderAddress: {},
            // 订单信息
            order: {},
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                orderIndex: null,
                memberId: null,
                merchantId: null,
                merchantIndex: "",
                goodsName: "",
                categoryId: [],
                categoryName: null,
                memberPhone: "",
                orderState: null,
                orderVerificationCode: "",
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                refundMoney: [
                    {
                        required: true,
                        message: "退款金额不能为空",
                        trigger: "blur",
                    },
                ],
            },
            refStatusOptions: [],
            timeForm: {
                serverId: null,
            },
            timeOpen: false,
            timeOptions: [],
            //追加服务选择框
            addStatusOptions: [
                {
                    label: "全部",
                    value: "",
                },
                {
                    label: "是",
                    value: "1",
                },
                {
                    label: "否",
                    value: "0",
                },
            ],
            refundParams: {
                orderId: null,
                refundMoney: "",
                refundReason: "",
            },
            refundOpen: false,
            categoryList: [],
            appointmentStartTime: null,
            optionProps: {
                value: "categoryId",
                label: "categoryName",
                children: "childrenCategory",
            },
            // 表单校验
            rules: {
                orderVerificationCode: [
                    {
                        required: true,
                        message: "核销码不能为空",
                        trigger: "blur",
                    },
                ],
                appointmentTime: [
                    {
                        required: true,
                        message: "上门服务时间不能为空",
                        trigger: "blur",
                    },
                ],
            },
            pickerOptions:{
                disabledDate(time) {
                return time.getTime() < Date.now();
          },
            },
        };
    },
    created() {
        this.getStatus();
        this.queryCategory();
        this.getList();
    },
    mounted() {},
    methods: {
        // 处理服务主体名称
        handleMerchantType(merchantType) {
            if (merchantType === 0) {
                return "商家";
            }
            if (merchantType === 1) {
                return "个体";
            }
            return "-";
        },

        // 查询服务类型
        queryCategory() {
            categoryTree()
                .then((res) => {
                    if (res.code == 200) {
                        this.categoryList = res.data;
                    } else {
                        this.$message.error("获取分类列表失败");
                    }
                })
                .catch((err) => {
                    this.$message.error("获取分类列表失败");
                });
        },

        // 提交预约核销
        submitVerifyOrderTime() {
            handleVerifyGroupOrder(this.form).then((res) => {
                if (res.code == "200") {
                    this.$modal.msgSuccess("核销成功");
                    this.open = false;
                    this.getList();
                }
            });
        },

        // 详情页面跳转
        toSee(row) {
            this.$router.push({
                path: "/order/groupDetail",
                query: { id: row.orderId },
            });
        },

        // 查询团购订单列表
        getList() {
            getInfo().then((r) => {
                if (r.roles[0] == "merchant") {
                    if (r.merchant.merchantId != null) {
                        this.queryParams.merchantId = r.merchant.merchantId;
                    }
                }
                this.loading = true;
                // 级联选择器绑定的参数queryParams.categoryId是一个数组，需要将最后一个层级分类的id 重新赋值给查询参数categoryId
                if (
                    this.queryParams.categoryId &&
                    this.queryParams.categoryId.length > 1
                ) {
                    this.queryParams.categoryId =
                        this.queryParams.categoryId[
                            this.queryParams.categoryId.length - 1
                        ];
                }
                if (this.queryParams.categoryName != null) {
                    this.queryParams.categoryName =
                        this.queryParams.categoryName[0].toString();
                }
                // 查询家政团购订单列表
                this.queryParams.orderType = "003";
                // 传参调用接口
                listGroupOrder(
                    this.addDateRange(this.queryParams, this.dateRange)
                ).then((response) => {
                    this.orderList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.dateRange = null;
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                orderIndex: null,
                memberId: null,
                merchantId: null,
                merchantIndex: "",
                goodsName: "",
                categoryId: [],
                memberPhone: "",
                orderState: null,
                orderVerificationCode: "",
            };
        },
        // 搜索按钮操作
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        // 重置按钮操作
        resetQuery() {
            this.reset();
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.orderId);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },

        editRefund() {
            this.$message.warning("退款功能暂未开通");
        },
        async handleRefund() {
            await this.$refs.refundForm.validate();

            const { code } = await merchantRefund(this.refundParams);
            if (code === 200) {
                this.$modal.msgSuccess("操作成功");
            }
        },
        // 核销订单按钮
        toVerifyOrder(row) {
            this.form = JSON.parse(JSON.stringify(row));
            this.open = true;
            this.orderVerificationCodeIsInput = false;
            this.form.orderVerificationCode = "";
            getOrder(row.orderId).then((response) => {
                this.order = response.data;
                this.queryOrderAdress();
            });
        },
        // 获取订单状态
        getStatus() {
            let param = {
                codeValue: "订单状态",
            };
            listCode(param).then((res) => {
                this.statusOptions = res.rows;
            });
        },
        // 查询订单地址
        queryOrderAdress() {
            getAddress(this.order.orderId).then((res) => {
                this.memberOrderAddress = res.data;
            });
        },

        // 输入核销码进行核销
        verifyGroupOrder(orderVerificationCode) {
            if (!orderVerificationCode) {
                return this.$modal.msgError("核销码不能为空!");
            }
            getOrderByVerificationCode(orderVerificationCode).then(
                (response) => {
                    if (response.code === 200 && response.data) {
                        this.order = response.data;
                        if (this.order.orderState !== '001') {
                            return this.$modal.msgError("只有待服务的订单才可进行核销操作！");
                        }
                        this.open = true;
                        this.orderVerificationCodeIsInput = true;
                        this.queryOrderAdress();
                    } else {
                         return this.$modal.msgError("核销码所属订单信息不存在!");
                    }
                }
            ) .catch((err) => {
                this.$modal.msgError(err);
            });
        },
    },
};
</script>

<style scoped></style>
