<template>
  <div class="order-container">
    <el-form class="search-form" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item :label="$t('hui-yuan-ID')" prop="name">
        <el-input
            v-model="queryParams.userId"
            :placeholder="$t('qing-shu-ru-hui-yuan-ID')"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('shou-ji-hao')" prop="mobile">
        <el-input
            v-model="queryParams.mobile"
            :placeholder="$t('qing-shu-ru-hui-yuan-shou-ji-hao')"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item :label="$t('ding-dan-hao')" prop="orderSn">
        <el-input
            v-model="queryParams.orderSn"
            :placeholder="$t('qing-shu-ru-ding-dan-hao')"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('zhuo-ma')" prop="tableCode">
        <el-input
            v-model="queryParams.tableCode"
            :placeholder="$t('qing-shu-ru-zhuo-ma')"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item :label="$t('ding-dan-lei-xing')" prop="type">
        <el-select
            v-model="queryParams.type"
            clearable
            :placeholder="$t('ding-dan-lei-xing')"
        >
          <el-option v-for="orderType in typeList" :key="orderType.key" :label="orderType.name" :value="orderType.value"/>
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item :label="$t('suo-shu-dian-pu')" prop="storeIds">
        <el-select v-model="storeIds" multiple filterable clearable :placeholder="$t('qing-xuan-ze-dian-pu')" style="width: 100%;">
          <el-option
              v-for="item in storeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item :label="$t('ding-dan-zhuang-tai')" prop="status">
        <el-select
            v-model="queryParams.status"
            :placeholder="$t('qing-xuan-ze-ding-dan-zhuang-tai')"
            clearable
        >
          <el-option v-for="statusItem in statusList" :key="statusItem.key+''" :label="statusItem.name" :value="statusItem.key+''"/>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('zhi-fu-zhuang-tai')" prop="payStatus">
        <el-select
            v-model="queryParams.payStatus"
            :placeholder="$t('zhi-fu-zhuang-tai')"
            clearable
        >
          <el-option v-for="statusItem in payStatusList" :key="statusItem.key+''" :label="statusItem.name" :value="statusItem.key+''"/>
        </el-select>
      </el-form-item>
      <!-- <el-form-item :label="$t('pei-song-fang-shi')">
        <el-select
            v-model="queryParams.orderMode"
            :placeholder="$t('pei-song-fang-shi')"
            clearable
        >
          <el-option v-for="item in orderModeList" :key="item.key+''" :label="item.name" :value="item.key+''"/>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" class="main-button-middle" icon="el-icon-search" @click="handleQuery">
          {{ $t('cha-xun') }}
        </el-button>
        <el-button icon="el-icon-refresh" class="main-button-middle main-button-reset" @click="resetQuery">
          {{ $t('chong-zhi') }}
        </el-button>
      </el-form-item>
    </el-form>
    <el-table ref="tables" v-loading="loading" :data="list" border style="width: 100%;" fit height="100%" show-overflow-tooltip @selection-change="handleSelectionChange" :default-sort="defaultSort" @sort-change="handleSortChange">
      <el-table-column :label="$t('ID')" prop="id" width="80"/>
      <el-table-column :label="$t('ding-dan-hao')"  align="center" width="190" prop="orderSn">
        <template slot-scope="scope">
          <span @click="handleView(scope.row)" style="color: #00afff;cursor: pointer;">{{ scope.row.orderSn }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('zhuo-ma')" align="center" width="100" prop="tableInfo.code">
        <template slot-scope="scope">
          <span v-if="scope.row.tableInfo">{{ scope.row.tableInfo.code }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <!-- <el-table-column :label="$t('hui-yuan-ming-cheng')" align="center" prop="orderInfo.name">
        <template slot-scope="scope">
          <span v-if="scope.row.userInfo && scope.row.isVisitor == 'N'">
              <span>{{ scope.row.userInfo.name }}</span>
          </span>
          <span v-else>{{ $t('you-ke') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column :label="$t('ding-dan-lei-xing')" align="center" prop="typeName">
        <template slot-scope="scope">
          <span>{{ scope.row.typeName }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column :label="$t('suo-shu-dian-pu')" align="center" width="140" prop="storeInfo.name">
        <template slot-scope="scope">
          <span v-if="scope.row.storeInfo">{{ scope.row.storeInfo.name }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column> -->
      <el-table-column :label="$t('zong-jin-e')" align="center" prop="amount">
        <template slot-scope="scope">
          <span v-if="true">{{ scope.row.amount.toFixed(2) }}</span>
          <span v-else>0.00</span>
        </template>
      </el-table-column>
      <!-- <el-table-column :label="$t('zhi-fu-zhuang-tai')" align="center" width="80" prop="payStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.payStatus == 'B'" class="status-active">{{ getName(payStatusList, scope.row.payStatus) }}</span>
          <span v-else class="status-disabled">{{ getName(payStatusList, scope.row.payStatus) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column :label="$t('ding-dan-zhuang-tai')" align="center" width="80" prop="status">
        <template slot-scope="scope">
          <span class="status-normal">{{ getName(statusList, scope.row.status) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column :label="$t('pei-song-fang-shi')" align="center" prop="orderMode">
        <template slot-scope="scope">
          <span>{{ getName(orderModeList, scope.row.orderMode) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column :label="$t('sheng-cheng-shi-jian')" align="center" width="130" prop="createTime">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('zhi-fu-jin-e')" align="center" prop="payAmount">
        <template slot-scope="scope">
          <span v-if="true">{{ scope.row.payAmount.toFixed(2) }}</span>
          <span v-else>0.00</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('you-hui-jin-e')" align="center" prop="discount">
        <template slot-scope="scope">
          <span v-if="true">{{ scope.row.discount.toFixed(2) }}</span>
          <span v-else>0.00</span>
        </template>
      </el-table-column>
      <!-- <el-table-column :label="$t('ji-fen-jin-e')" align="center" prop="pointAmount">
        <template slot-scope="scope">
          <span v-if="true">{{ scope.row.pointAmount.toFixed(2) }}</span>
          <span v-else>0.00</span>
        </template>
      </el-table-column> -->
      <el-table-column :label="$t('geng-xin-shi-jian')" align="center" width="130" prop="updateTime">
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('cao-zuo')" align="left" width="138" fixed='right'>
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              class="main-text"
              v-hasPermi="['cashier:index']"
              @click="handleUpdate(scope.row)"
          >
            {{ $t('xiu-gai') }}
          </el-button>
          <el-button
              v-if="scope.row.orderMode == 'express'"
              size="mini"
              type="text"
              class="main-text"
              v-hasPermi="['cashier:index']"
              @click="handleExpress(scope.row)"
          >
            {{ $t('fa-huo') }}
          </el-button>
          <!-- <el-button
              v-if="scope.row.orderMode == 'oneself'"
              type="text"
              size="mini"
              class="main-text"
              v-hasPermi="['cashier:index']"
              :disabled="scope.row.isVerify == true"
              @click="handleVerify(scope.row)"
          >
            {{ $t('he-xiao') }}
          </el-button> -->
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['order:edit', 'order:delete']">
                <span class="el-dropdown-link">
                  <i class="el-icon-d-arrow-right el-icon--right"></i>{{ $t('geng-duo') }}
                </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleView" icon="el-icon-view" v-hasPermi="['cashier:index']">
                {{ $t('cha-kan-xiang-qing') }}
              </el-dropdown-item>
              <el-dropdown-item command="handlePrint" icon="el-icon-printer" v-hasPermi="['cashier:index']">
                {{ $t('da-yin-xiao-piao') }}
              </el-dropdown-item>
              <!-- <el-dropdown-item command="handlePay" v-if="scope.row.payStatus != 'B'" icon="el-icon-wallet" v-hasPermi="['cashier:index']">
                {{ $t('fa-qi-zhi-fu') }}
              </el-dropdown-item> -->
              <!-- <el-dropdown-item v-if="scope.row.payStatus == 'B'" command="handleRefund" icon="el-icon-s-order" v-hasPermi="['cashier:index']">
                {{ $t('li-ji-tui-kuan') }}
              </el-dropdown-item> -->
              <!-- <el-dropdown-item v-if="scope.row.payStatus != 'B'" command="handleDelete" icon="el-icon-remove" v-hasPermi="['cashier:index']">
                {{ $t('shan-chu-ding-dan') }}
              </el-dropdown-item> -->
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 修改对话框 start-->
    <el-dialog :title="$t('xiu-gai-ding-dan')" :visible.sync="open" class="common-dialog" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-hao')" prop="orderSn">
              <el-input v-model="form.orderSn" :placeholder="$t('ding-dan-hao')" disabled maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.orderMode && form.orderMode.length > 0">
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-mo-shi')" prop="orderMode">
              <el-select v-model="form.orderMode" :placeholder="$t('ding-dan-mo-shi')">
                <el-option v-for="item in orderModeList" :key="item.key+''" :label="item.name" :value="item.key+''"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-zong-jin-e')" prop="amount">
              <el-input v-model="form.amount" :placeholder="$t('qing-shu-ru-ding-dan-zong-jin-e')" maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('you-hui-jin-e')" prop="discount">
              <el-input v-model="form.discount" :placeholder="$t('qing-shu-ru-you-hui-jin-e')" maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-bei-zhu')" prop="remark">
              <el-input v-model="form.remark" type="textarea" :placeholder="$t('qing-shu-ru-ding-dan-bei-zhu')"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-zhuang-tai')">
              <el-select v-model="form.status" :placeholder="$t('ding-dan-zhuang-tai')">
                <el-option v-for="statusItem in statusList" :key="statusItem.key+''" :label="statusItem.name" :value="statusItem.key+''"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="main-button" @click="submitForm">
          {{ $t('que-ding') }}
        </el-button>
        <el-button @click="cancel">
          {{ $t('qu-xiao') }}
        </el-button>
      </div>
    </el-dialog>
    <!-- 修改对话框 end-->

    <!-- 修改对话框 start-->
    <el-dialog :title="title" :visible.sync="open" class="common-dialog" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-hao')" prop="orderSn">
              <el-input v-model="form.orderSn" :placeholder="$t('ding-dan-hao')" disabled maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.orderMode && form.orderMode.length > 0">
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-mo-shi')" prop="orderMode">
              <el-select v-model="form.orderMode" :placeholder="$t('ding-dan-mo-shi')">
                <el-option v-for="item in orderModeList" :key="item.key+''" :label="item.name" :value="item.key+''"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-zong-jin-e')" prop="amount">
              <el-input v-model="form.amount" :placeholder="$t('qing-shu-ru-ding-dan-zong-jin-e')" maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('you-hui-jin-e')" prop="discount">
              <el-input v-model="form.discount" :placeholder="$t('qing-shu-ru-you-hui-jin-e')" maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-bei-zhu')" prop="remark">
              <el-input v-model="form.remark" type="textarea" :placeholder="$t('qing-shu-ru-ding-dan-bei-zhu')"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-zhuang-tai')">
              <el-select v-model="form.status" :placeholder="$t('ding-dan-zhuang-tai')">
                <el-option v-for="statusItem in statusList" :key="statusItem.key+''" :label="statusItem.name" :value="statusItem.key+''"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="main-button" @click="submitForm">{{ $t('que-ding') }}</el-button>
        <el-button @click="cancel">{{ $t('qu-xiao') }}</el-button>
      </div>
    </el-dialog>
    <!-- 修改对话框 end-->

    <!-- 核销对话框 start-->
    <el-dialog :title="$t('he-xiao-ding-dan')" :visible.sync="openVerify" class="common-dialog" width="700px" append-to-body>
      <el-form ref="vForm" :model="vForm" :rules="vFormRules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-hao')" prop="orderSn">
              <el-input v-model="vForm.orderSn" :placeholder="$t('ding-dan-hao')" disabled maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('he-xiao-ma')" prop="verifyCode">
              <el-input v-model="vForm.verifyCode" :placeholder="$t('qing-shu-ru-ding-dan-he-xiao-ma')" maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-bei-zhu')" prop="remark">
              <el-input v-model="vForm.remark" type="textarea" :placeholder="$t('qing-shu-ru-ding-dan-bei-zhu')"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="main-button" @click="submitVForm">{{ $t('que-ding-he-xiao') }}</el-button>
        <el-button @click="cancelVForm">{{ $t('qu-xiao') }}</el-button>
      </div>
    </el-dialog>
    <!-- 核销对话框 end-->

    <!-- 发货对话框 start-->
    <el-dialog :title="$t('tian-xie-wu-liu-xin-xi')" :visible.sync="openExpress" class="common-dialog" width="700px" append-to-body>
      <el-form ref="eForm" :model="eForm" :rules="eFormRules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-hao')" prop="orderSn">
              <el-input v-model="eForm.orderSn" :placeholder="$t('ding-dan-hao')" disabled maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('wu-liu-gong-si')" prop="expressCompany">
              <el-input v-model="eForm.expressCompany" :placeholder="$t('qing-shu-ru-wu-liu-gong-si')" maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('wu-liu-dan-hao')" prop="expressNo">
              <el-input v-model="eForm.expressNo" :placeholder="$t('qing-shu-ru-wu-liu-dan-hao')"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEForm">{{ $t('ti-jiao-wu-liu-xin-xi') }}</el-button>
        <el-button @click="cancelEForm">{{ $t('qu-xiao') }}</el-button>
      </div>
    </el-dialog>
    <!-- 发货对话框 end-->

    <!--打印订单对话框 start-->
    <orderPrintDialog :show-dialog="openOrderPrintDialog" :storeInfo="storeInfo" :orderInfo="orderInfo" @closeDialog="closePrintDialog"/>
    <!--打印订单对话框 end-->

    <!-- 退款对话框 start -->
    <el-dialog :title="$t('ding-dan-tui-kuan')" :visible.sync="openRefundDialog" class="common-dialog" width="700px" append-to-body>
      <el-form ref="rForm" :model="rForm" :rules="rFormRules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-hao')" prop="orderSn" style="width:450px;">
              <el-input v-model="rForm.orderSn" :placeholder="$t('ding-dan-hao')" disabled maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('zhi-fu-fang-shi')" prop="payType" style="width:450px;">
              <el-input v-model="rForm.payType" :placeholder="$t('zhi-fu-fang-shi')" disabled maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('ding-dan-shi-fu-jin-e')" prop="payAmount" style="width:450px;">
              <el-input v-model="'￥' + rForm.payAmount" :placeholder="$t('ding-dan-shi-fu-jin-e')" disabled maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('tui-kuan-jin-e')" prop="refundAmount" style="width:450px;">
              <el-input v-model="rForm.refundAmount" :placeholder="$t('qing-shu-ru-tui-kuan-jin-e')"></el-input>
              <div class="form-tips">{{ $t('tui-kuan-jin-e-tips') }}</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('tui-kuan-bei-zhu')" prop="remark">
              <el-input v-model="rForm.remark" type="textarea" :placeholder="$t('qing-shu-ru-tui-kuan-bei-zhu')"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="main-button" @click="submitRForm">{{ $t('que-ding') }}</el-button>
        <el-button @click="cancelRForm">{{ $t('qu-xiao') }}</el-button>
      </div>
    </el-dialog>
    <!-- 退款对话框 end -->

    <!--订单详情对话框 start-->
    <orderDetail :show-dialog="openOrderDetailDialog" :order-id="orderInfo ? orderInfo.id+'' : 0" @closeDialog="closeOrderDialog"/>
    <!--订单详情对话框 end-->
    </div>
</template>

<script>
import { getName } from "@/utils/fuint";
import { getOrderList, updateOrderStatus, getOrderInfo, saveOrder, deleteOrder, verifyOrder, delivered } from "@/api/order";
import { doRefund } from "@/api/refund";
import orderPrintDialog from './orderPrintDialog';
import orderDetail from './orderDetail';
import {Message} from "element-ui";
export default {
  components: {
    orderPrintDialog,
    orderDetail
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 标题
      title: "",
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 店铺列表
      storeList: [],
      // 所属店铺
      storeIds: [],
      // 订单类型
      typeList: [],
      // 订单状态列表
      statusList: [],
      // 订单支付状态列表
      payStatusList: [],
      // 配送类型列表
      orderModeList: [],
      // 支付类型
      payTypeList: [],
      // 是否显示弹出层
      open: false,
      // 是否显示核销对话框
      openVerify: false,
      // 是否显示发货对话框
      openExpress: false,
      // 默认排序
      defaultSort: {prop: 'createTime', order: 'descending'},
      // 打印订单对话框
      openOrderPrintDialog: false,
      // 退款对话框
      openRefundDialog: false,
      // 订单详情对话框
      openOrderDetailDialog: false,
      storeInfo: {},
      orderInfo: {},
      // 表单参数
      form: { orderId: "", orderSn: 0, amount: "", orderMode: "oneself", discount: "", remark: "", status: "A" },
      vForm: { orderId: "", orderSn: "", remark: "", verifyCode: "" },
      eForm: { orderId: "", orderSn: "", expressCompany: "", expressNo: "" },
      rForm: { orderId: "", orderSn: "", payType: '', payAmount: "", refundAmount: "", remark: ""},
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        userId: '',
        mobile: '',
        status: '',
        payStatus: '',
        orderMode: '',
        orderSn: '',
        tableCode: '',
        storeIds: ''
      },
      // 表单校验
rules: {
    amount: [
        { required: true, message: this.$t("zong-jin-e-bu-neng-wei-kong"), trigger: "blur" },
    ]
},
// 核销表单校验
vFormRules: {
    verifyCode: [
        { required: true, message: this.$t("he-xiao-ma-bu-neng-wei-kong"), trigger: "blur" },
    ]
},
eFormRules: {
    expressCompany: [
        { required: true, message: this.$t("wu-liu-gong-si-bu-neng-wei-kong"), trigger: "blur" },
    ],
    expressNo: [
        { required: true, message: this.$t("wu-liu-dan-hao-bu-neng-wei-kong"), trigger: "blur" },
    ]
},
rFormRules: {
    refundAmount: [
        { required: true, message: this.$t("tui-kuan-jin-e-bu-neng-wei-kong"), trigger: "blur" },
        { pattern: /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/, message: this.$t("qing-shu-ru-zheng-que-de-tui-kuan-jin-e"), trigger: 'blur' }
    ]
}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查询订单
    getList() {
      const app = this;
      app.loading = true;
      app.queryParams.storeIds = app.storeIds ? app.storeIds.join(",") : '';
      getOrderList(app.queryParams).then( response => {
            this.list = response.data.paginationResponse.content;
            this.total = response.data.paginationResponse.totalElements;
            this.typeList = response.data.typeList;
            this.statusList = response.data.statusList;
            this.payStatusList = response.data.payStatusList;
            this.orderModeList = response.data.orderModeList;
            this.payTypeList = response.data.payTypeList;
            this.storeList = response.data.storeList;
            this.loading = false;
          }
      ).catch(() => {
        // empty
      });
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    // 重置按钮操作
    resetQuery() {
      this.dateRange = [];
      this.queryParams.status = '';
      this.queryParams.mobile = '';
      this.queryParams.orderMode = '';
      this.queryParams.orderSn = '';
      this.queryParams.storeIds = '';
      this.storeIds = [];
      this.resetForm("queryForm");
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      this.handleQuery();
    },
    // 状态修改
    handleStatusChange(row) {
    let text = row.status === "A" ? this.$t('qi-yong') : this.$t('jin-yong');
    this.$confirm(this.$t('que-ren-yao') + text + '"' + row.orderSn + '"' + this.$t('ma') + '？')
      .then(() => {
            return updateOrderStatus(row.id, row.status);
        })
      .then(() => {
            this.$message({
                message: text + this.$t('cheng-gong'),
                type: "success"
            });
        })
      .catch(() => {
            row.status = row.status === "N" ? "A" : "N";
        });
},
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    // 排序触发事件
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: "",
        orderSn: "",
        status: "A",
        amount: "",
        discount: "",
        remark: "",
        orderMode: "oneself",
      };
      this.resetForm("form");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消核销按钮
    cancelVForm() {
      this.openVerify = false;
      this.vForm.orderId = '';
      this.vForm.orderSn = '';
      this.vForm.verifyCode = '';
    },
    // 提交核销按钮
    submitVForm: function() {
      this.$refs["vForm"].validate(valid => {
        if (valid) {
          verifyOrder(this.vForm).then(response => {
            Message({
              message: this.$t('ti-shi-he-xiao-cheng-gong'),
              type: "success"
            });
            this.cancelVForm();
            this.getList();
          }).catch(() => {
             // empty
          });
        }
      });
    },
    // 提交修改按钮
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          saveOrder(this.form).then(response => {
            Message({
              message: this.$t('ti-shi-xiu-gai-cheng-gong'),
              type: "success"
            });
            this.open = false;
            this.getList();
          }).catch(() => {
             // empty
          });
        }
      });
    },
    // 查看详情按钮操作
    handleView(row) {
      this.orderInfo = row;
      this.openOrderDetailDialog = true;
      return true;
    },
    // 修改按钮操作
    handleUpdate(row) {
      const app = this;
      app.reset();
      const id = row.id || this.ids;
      getOrderInfo(id).then(response => {
        let orderInfo = response.data.orderInfo;
        if (orderInfo) {
            app.form.orderId = orderInfo.id;
            app.form.orderSn = orderInfo.orderSn;
            app.form.amount = orderInfo.amount;
            app.form.discount = orderInfo.discount;
            app.form.remark = orderInfo.remark;
            app.form.status = orderInfo.status;
            app.form.orderMode = orderInfo.orderMode+"";
        }
        this.open = true;
        this.title = this.$t('xiu-gai-ding-dan');
      }).catch(() => {
         // empty
      });
    },
    // 核销按钮操作
    handleVerify(row) {
      const app = this;
      const id = row.id || this.ids;
      getOrderInfo(id).then(response => {
        let orderInfo = response.data.orderInfo;
        if (orderInfo) {
          app.vForm.orderId = orderInfo.id;
          app.vForm.orderSn = orderInfo.orderSn;
          app.vForm.remark = orderInfo.remark;
        }
        app.openVerify = true;
      }).catch(() => {
         // empty
      });
    },
    // 发货按钮
    handleExpress(row) {
      const app = this;
      const id = row.id || this.ids;
      getOrderInfo(id).then(response => {
        let orderInfo = response.data.orderInfo;
        if (orderInfo) {
          app.eForm.orderId = orderInfo.id;
          app.eForm.orderSn = orderInfo.orderSn;
          app.eForm.expressNo = orderInfo.expressInfo ? orderInfo.expressInfo.expressNo : "";
          app.eForm.expressCompany = orderInfo.expressInfo? orderInfo.expressInfo.expressCompany : "";
        }
        app.openExpress = true;
      }).catch(() => {
         // empty
      });
    },
    // 取消发货按钮
    cancelEForm() {
      this.openExpress = false;
      this.eForm.orderId = '';
      this.eForm.orderSn = '';
      this.eForm.expressCompany = '';
      this.eForm.expressNo = '';
    },
    // 提交发货按钮
    submitEForm: function() {
      const app = this;
      if (app.loading) {
          app.$alert(this.$t('qing-qiu-chu-li-zhong'));
      }
      app.$refs["eForm"].validate(valid => {
        if (valid) {
          app.loading = true;
          delivered(this.eForm).then(response => {
            Message({
              message: this.$t('ti-shi-ti-jiao-wu-liu-xin-xi-cheng-gong'),
              type: "success"
            });
            app.cancelEForm();
            app.getList();
            app.loading = false;
          });
        }
      });
    },
    // 取消退款按钮
    cancelRForm() {
      this.openRefundDialog = false;
      this.rForm.orderId = '';
      this.rForm.orderSn = '';
      this.rForm.payAmount = '';
      this.rForm.payType = '';
      this.rForm.refundAmount = '';
      this.rForm.remark = '';
    },
    // 提交退款按钮
submitRForm: function() {
    const app = this;
    if (app.loading) {
        app.$alert(this.$t('qing-qiu-chu-li-zhong'));
    }
    if (parseFloat(app.rForm.refundAmount) > parseFloat(app.rForm.amount)) {
        app.$alert(this.$t('tui-kuan-jin-e-bu-neng-da-yu-ding-dan-zong-jin-e'));
        return false;
    }
    app.$refs["rForm"].validate(valid => {
        if (valid) {
            app.loading = true;
            doRefund(app.rForm).then(response => {
                this.$message({
                    message: this.$t('ti-shi-tui-jiao-tui-kuan-cheng-gong'),
                    type: "success"
                });
                app.cancelRForm();
                app.getList();
                app.loading = false;
            });
        }
    });
},
// 删除按钮操作
handleDelete(row) {
    const orderSn = row.orderSn;
    this.$confirm(this.$t('shi-fou-que-ren-shan-chu-ding-dan-hao-wei') + '"' + orderSn + '"' + this.$t('de-shu-ju-xiang') + '？')
      .then(() => {
            return deleteOrder(row.id);
        })
      .then(() => {
            this.getList();
            this.$message({
                message: this.$t('ti-shi-shan-chu-cheng-gong'),
                type: "success"
            });
        })
      .catch(() => {});
},
    // 打印小票
    handlePrint(row) {
      const app = this;
      app.orderInfo = {};
      app.storeInfo = {};
      const id = row.id || this.ids;
      getOrderInfo(id).then(response => {
        let orderInfo = response.data.orderInfo;
        if (orderInfo) {
            app.orderInfo = orderInfo;
            app.storeInfo = orderInfo.storeInfo;
        }
        app.openOrderPrintDialog = true;
      }).catch(() => {
         // empty
      });
    },
    // 发起退款
    handleRefund(row) {
      const app = this;
      app.rForm.orderId = row.id;
      app.rForm.orderSn = row.orderSn;
      app.rForm.payAmount = row.payAmount;
      app.rForm.payType = getName(app.payTypeList, row.payType);
      app.openRefundDialog = true;
    },
    // 发起支付
    handlePay(orderInfo) {
      this.$emit('doPayOrder', orderInfo);
    },
    // 关掉打印对话框
    closePrintDialog() {
      this.openOrderPrintDialog = false;
    },
    // 关掉订单详情对话框
    closeOrderDialog() {
      this.openOrderDetailDialog = false;
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleDelete":
          this.handleDelete(row);
          break;
        case "handlePrint":
          this.handlePrint(row);
          break;
        case "handleRefund":
          this.handleRefund(row);
          break;
        case "handlePay":
          this.handlePay(row);
          break;
        default:
          this.handleView(row);
          break;
      }
    },
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-table__body-wrapper::-webkit-scrollbar {
  width: 12px;
  height: 12px;
  display: block;
}
::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
  background-color: #bfbfc0;
  border-radius: 5px;
}
::v-deep .el-table--scrollable-y .el-table__body-wrapper {
  overflow: overlay !important;
}
.order-container {
    position: absolute;
    top: 50px;
    left: 165px;
    right: 10px;
    height: 68%;
    .search-form {
       border: solid 1px #cccccc;
       margin-top: 0px;
       padding: 15px 10px 0px 10px;
       background: #f5f5f5;
       margin-bottom: 5px;
       border-radius: 3px;
    }
    .pagination {
      position: fixed;
      bottom: 10px;
      right: 150px;
      height: 50px;
      min-width: 730px;
      line-height: 50px;
      margin-top: 10px;
      display: block;
      background: #fff;
      border: 1px solid #eee;
      color: #ffffff;
      border-radius: 5px;
      z-index: 99999;
    }
}
</style>
<style scoped>
::v-deep .el-pagination.is-background span {
  color: #fff;
}
</style>
