<template>
  <zj-page-container>
    <zj-page-fill>
      <zj-tab-page
        ref="tabPage"
        :defaultActives="[{ key: 'list', label: $route.meta.title + '-列表', essential: true }]"
      >
        <template slot-scope="{ activeKey, data }">
          <div
            :style="{ height: activeKey == 'list' ? '100%' : '0px' }"
            style="width: 100%; height: 100%; overflow: hidden"
          >
            <div style="width: 100%; height: 100%; box-sizing: border-box; padding: 10px">
              <zj-page-container>
                <div class="screen-container">
                  <el-form ref="screenForm" :model="screenForm" label-width="70px" size="small" label-position="left">
                    <el-row :gutter="20">
                      <el-col :xs="24" :sm="12" :lg="6">
                        <el-form-item label="订单号" prop="orderId">
                          <el-input v-model="screenForm.orderId" placeholder="请输入订单号"></el-input>
                        </el-form-item>
                      </el-col>
                      <el-col :xs="24" :sm="12" :lg="6">
                        <el-form-item label="售后编号" prop="esOrderRefundId">
                          <el-input v-model="screenForm.esOrderRefundId" placeholder="请输入售后编号"></el-input>
                        </el-form-item>
                      </el-col>
                      <el-col :xs="24" :sm="12" :lg="6">
                        <el-form-item label="商品名称" prop="esGoodsName">
                          <el-input v-model="screenForm.esGoodsName" placeholder="请输入商品名称"></el-input>
                        </el-form-item>
                      </el-col>
                      <el-col :xs="24" :sm="12" :lg="6">
                        <el-form-item label="会员昵称" prop="userName">
                          <el-input v-model="screenForm.userName" placeholder="请输入会员昵称"></el-input>
                        </el-form-item>
                      </el-col>
                      <el-col :xs="24" :sm="12" :lg="6">
                        <el-form-item label="手机号" prop="userMobile">
                          <el-input v-model="screenForm.userMobile" placeholder="请输入手机号"></el-input>
                        </el-form-item>
                      </el-col>
                      <el-col :xs="24" :sm="12" :lg="6">
                        <el-form-item label="创建时间" prop="createDate" style="height: 33px">
                          <el-date-picker
                            v-model="screenForm.createDate"
                            type="datetimerange"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                          >
                          </el-date-picker>
                        </el-form-item>
                      </el-col>
                      <el-col :xs="24" :sm="12" :lg="6">
                        <el-form-item label="支付时间" prop="payDate" style="height: 33px">
                          <el-date-picker
                            v-model="screenForm.payDate"
                            type="datetimerange"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                          >
                          </el-date-picker>
                        </el-form-item>
                      </el-col>
                      <el-col :xs="24" :sm="12" :lg="6" class="tr">
                        <el-form-item label="" label-width="0px">
                          <div style="text-align: left">
                            <el-button size="small" type="primary" @click="submitScreenForm">搜索</el-button>
                            <el-button size="small" @click="resetScreenForm">清空</el-button>
                          </div>
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </el-form>
                </div>
                <div class="tabs-container order-tabs-container clearfix" style="margin: 10px 0 !important">
                  <div class="fl">
                    <el-tabs
                      v-model="tabCurrent"
                      type="card"
                      @tab-click="changeTabs"
                      class="fl"
                      size="small"
                      style="margin-right: 10px"
                    >
                      <el-tab-pane label="全部" name="ALL"></el-tab-pane>
                      <el-tab-pane label="待商家处理" name="WAIT"></el-tab-pane>
                      <el-tab-pane label="已完成" name="OVER"></el-tab-pane>
                    </el-tabs>
                  </div>
                  <div class="fr">
                    <el-button size="small" type="primary" @click="handleExport">导出</el-button>
                  </div>
                </div>
                <zj-page-fill>
                  <div class="order-main-container">
                    <div class="table-top">
                      <el-row style="width: 100%">
                        <el-col :span="3">
                          <span class="item product">商品信息</span>
                        </el-col>
                        <el-col :span="3" style="text-align: right"><span class="item">单价(元)/数量</span></el-col>
                        <el-col :span="3" style="text-align: center"><span class="item">买家信息</span></el-col>
                        <el-col :span="3" style="text-align: center"><span class="item">收货信息</span></el-col>
                        <el-col :span="3" style="text-align: center"><span class="item">订单金额</span></el-col>
                        <el-col :span="3" style="text-align: center"><span class="item">退款金额</span></el-col>
                        <el-col :span="3" style="text-align: center"><span class="item">状态</span></el-col>
                        <el-col :span="3" style="text-align: center"><span class="item">操作</span></el-col>
                      </el-row>
                    </div>
                    <div class="order-item" v-for="order in dataList" :key="order.orderId">
                      <div class="order-top">
                        <span><b>售后编号</b>：{{ order.esOrderRefundId }}</span>
                        <span><b>订单号</b>：{{ order.esOrderId }}</span>
                        <span><b>下单时间</b>：{{ order.createTime }}</span>
                        <!-- <el-tag>商城销售</el-tag> -->
                        <div class="fr"></div>
                      </div>
                      <div class="order-content">
                        <el-row style="width: 100%">
                          <el-col :span="6" style="height: 100%">
                            <div class="col-item-pro">
                              <div class="pro-item">
                                <div class="left">
                                  <div class="image">
                                    <img :src="$showImgUrl(order.goodsUrl)" style="height: 80px; width: 80px" />
                                  </div>
                                  <div class="main">
                                    <span>{{ order.esGoodsName }}</span>
                                  </div>
                                </div>
                                <div class="right">
                                  <span>￥{{ order.refundPrice }}</span>
                                  <span>x{{ order.refundNum }}</span>
                                </div>
                              </div>
                            </div>
                          </el-col>
                          <el-col :span="3" style="height: 100%">
                            <div class="col-item">
                              <div>{{ order.userName }}</div>
                              <div>{{ order.userMobile }}</div>
                            </div>
                          </el-col>
                          <el-col :span="3" style="height: 100%">
                            <div class="col-item">
                              <div>{{ order.userName }}/{{ order.userMobile }}</div>
                              <div class="ellipsis-2">
                                {{ order.province + order.city + order.area + order.street + order.receAddress }}
                              </div>
                            </div>
                          </el-col>
                          <el-col :span="3" style="height: 100%">
                            <div class="col-item">
                              <div class="totalprice">￥{{ order.totalAmount }}</div>
                            </div>
                          </el-col>
                          <el-col :span="3" style="height: 100%">
                            <div class="col-item">
                              <div class="totalprice">￥{{ order.refundPrice }}</div>
                            </div>
                          </el-col>
                          <el-col :span="3" style="height: 100%">
                            <div class="col-item">
                              <div style="display: flex; align-items: center">
                                <span style="margin-left: 4px">{{
                                  { WAIT: '平台处理', OVER: '完成' }[order.status]
                                }}</span>
                              </div>
                            </div>
                          </el-col>
                          <el-col :span="3" style="height: 100%">
                            <div class="col-item">
                              <div class="operate">
                                <el-button @click="viewDetails(order)" type="text" size="small">查看详情</el-button>
                              </div>
                            </div>
                          </el-col>
                        </el-row>
                      </div>
                    </div>
                    <div class="no-data" v-if="dataList.length <= 0">暂无订单</div>
                  </div>
                </zj-page-fill>
                <div class="pagination clearfix" style="margin-top: 10px">
                  <div class="fr">
                    <el-pagination
                      @size-change="handleSizeChange"
                      @current-change="handleCurrentChange"
                      :current-page="currentPage"
                      :page-sizes="[10, 20, 30, 50]"
                      :page-size="10"
                      layout="total, sizes, prev, pager, next, jumper"
                      :total="listTotal"
                    >
                    </el-pagination>
                  </div>
                </div>
              </zj-page-container>
            </div>
          </div>
          <!-- 查看详情 -->
          <div v-if="activeKey == 'details'" style="width: 100%; height: 100%">
            <detailsV
              :detailsId="detailsId"
              @removeTab="
                () => {
                  getList()
                  data.removeTab()
                }
              "
            />
          </div>
        </template>
      </zj-tab-page>
    </zj-page-fill>
  </zj-page-container>
</template>

<script>
import { esOrderListRefund } from '@/api/orderManagement'
import detailsV from './details.vue'
import '@/styles/order-list.scss'
import { downloadFiles } from '@/utils/util'
export default {
  components: { detailsV },
  data() {
    return {
      screenForm: {
        // 筛选表单数据
        orderId: '', // 订单号
        esOrderRefundId: '',
        esGoodsName: '', // 商品名称
        userName: '', // 会员昵称
        userMobile: '', // 手机号
        websitId: '', // 网点名称
        status: '', // 状态
        createDate: '', // 创建时间
        payDate: '', // 支付时间
        exchangeCode: '', // 兑换码
        evaluateStatus: '', // 评价状态
        sendStatus: '', // 派单异常
        workerOrderNo: '' // 信息编号
      },
      tabCurrent: 'ALL',
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: [],
      detailsId: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    viewDetails(row, type) {
      this.$refs.tabPage.addTab({
        // 对应显示的模块
        activeKey: 'details',
        // 唯一标识
        key: 'details',
        // 页签名称
        label: '订单详情',
        // 打开时事件
        triggerEvent: () => {
          this.detailsId = row.esOrderRefundId
        },
        // 关闭时事件
        closeEvent: () => {}
      })
    },
    changeTabs(tab, event) {
      this.screenForm.status = this.tabCurrent === 'ALL' ? '' : tab.$options.propsData.name
      this.currentPage = 1
      this.getList()
    },
    // 获取数据列表
    getList() {
      let params = {
        orderId: this.screenForm.orderId, // 订单号
        esOrderRefundId: this.screenForm.esOrderRefundId, // 订单号
        esGoodsName: this.screenForm.esGoodsName, // 商品名称
        userName: this.screenForm.userName, // 会员昵称
        userMobile: this.screenForm.userMobile, // 手机号
        status: this.screenForm.status, // 状态
        startCreateTime: this.screenForm.createDate[0] ? this.screenForm.createDate[0] : null, // 创建时间
        endCreateTime: this.screenForm.createDate[1] ? this.screenForm.createDate[1] : null, // 创建时间
        startPayTime: this.screenForm.payDate[0] ? this.screenForm.payDate[0] : null, // 支付时间
        endPayTime: this.screenForm.payDate[1] ? this.screenForm.payDate[1] : null, // 支付时间
        pageNum: this.currentPage,
        pageSize: this.pageSize
      }
      esOrderListRefund(params).then(res => {
        res.data.records.forEach(item => {
          item.selected = false
        })
        this.dataList = res.data.records
        this.listTotal = res.data.total
      })
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getList()
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },
    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1
      this.getList()
    },
    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields()
      this.currentPage = 1
      this.getList()
    },
    // 导出
    handleExport() {
      let params = {
        orderId: this.screenForm.orderId, // 订单号
        esOrderRefundId: this.screenForm.esOrderRefundId, // 订单号
        esGoodsName: this.screenForm.esGoodsName, // 商品名称
        userName: this.screenForm.userName, // 会员昵称
        userMobile: this.screenForm.userMobile, // 手机号
        status: this.screenForm.status, // 状态
        startCreateTime: this.screenForm.createDate[0] ? this.screenForm.createDate[0] : null, // 创建时间
        endCreateTime: this.screenForm.createDate[1] ? this.screenForm.createDate[1] : null, // 创建时间
        startPayTime: this.screenForm.payDate[0] ? this.screenForm.payDate[0] : null, // 支付时间
        endPayTime: this.screenForm.payDate[1] ? this.screenForm.payDate[1] : null, // 支付时间
        pageNum: this.currentPage,
        pageSize: this.pageSize
      }
      downloadFiles('esOrder/listExcelRefund', params)
    }
  }
}
</script>
