<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item prop="orderNo" v-if="userInfoVO.userType == '00'">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </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="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option v-for="dict in dict.type.recharge_order_status" :key="dict.code" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item prop="orgId" v-if="userInfoVO.userType == '00'">
        <el-select v-model="queryParams.orgId" placeholder="请选择门店" filterable clearable>
          <el-option v-for="store in storeList" :key="store.id + store.sourceType" :label="store.name" :value="store.id"> </el-option>
        </el-select>
      </el-form-item>
      <div></div>
      <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-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-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="['recharge:order:export']"
          v-if="userInfoVO.userType == '00'">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <Page uri="/mapi/recharge/order/list" :request-params="queryParams" ref="pagination">
      <el-table-column label="订单编号" align="center" width="130" prop="orderNo" />
      <el-table-column label="下单门店" align="center" v-if="userInfoVO.userType == '00'">
        <template slot-scope="scope">
          {{ scope.row.storeName }}
        </template>
      </el-table-column>
      <el-table-column label="客户姓名" align="center" prop="realName" />
      <el-table-column label="客户手机号" align="center" prop="phoneNumber" width="110" />
      <el-table-column label="支付方式" align="center" prop="payType">
        <template slot-scope="scope">
          <div v-if="scope.row.payType != ''">
            <dict-tag :options="dict.type.recharge_pay_type" :value="scope.row.payType" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="支付金额（元）" align="center" prop="payAmount" width="120">
        <template slot-scope="scope">
          <span style="display: block; text-align: left">现金付款: {{ scope.row.cashPayAmount }}</span>
          <span style="display: block; text-align: left">微信付款: {{ scope.row.wxPayAmount }}</span>
          <span style="display: block; text-align: left">支付宝付款: {{ scope.row.aliPayAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充值金额（元）" align="center" prop="rechargeAmount" width="120">
        <template slot-scope="scope">
          <span style="display: block; text-align: left">现金金额: {{ scope.row.rechargeAmount }}</span>
          <span style="display: block; text-align: left">赠送金额: {{ scope.row.giveAmount }}</span>
          <span style="display: block; text-align: left">福利金额: {{ scope.row.welfareAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="赠送积分" align="center" prop="givePointAmount" v-if="userInfoVO.userType == '02'" />

      <el-table-column label="订单状态" align="center" prop="orderStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.recharge_order_status" :value="scope.row.orderStatus" />
        </template>
      </el-table-column>
      <el-table-column label="退款状态" align="center" prop="orderStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.recharge_refund_status" :value="scope.row.refundStatus" />
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createTime" />
      <el-table-column label="支付时间" align="center" prop="payTime">
        <template slot-scope="scope">
          {{ scope.row.payTime == null ? '--' : scope.row.payTime }}
        </template>
      </el-table-column>
      <el-table-column label="退款时间" align="center" prop="refundTime">
        <template slot-scope="scope">
          {{ scope.row.refundTime == null ? '--' : scope.row.refundTime }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark">
        <template slot-scope="scope">
          {{ scope.row.remark == null ? '--' : scope.row.remark }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="handleDetail(scope.row)">明细</el-button>
          <el-button type="text" @click="refundOrder(scope.row)"
            v-if="userInfoVO.userType == '00' && scope.row.refundStatus != '1' && scope.row.refundStatus != '5' && scope.row.refundStatus != '2' && scope.row.orderStatus != '0' && scope.row.orderStatus != '100'">
            撤单</el-button>
        </template>
      </el-table-column>
    </Page>

    <el-dialog :title="title" :visible.sync="open" size="70%" append-to-body>
      <el-row :gutter="15" style="margin-bottom: 80px; margin-left: 20px; width: calc(100% - 20px)">
        <el-col :span="24" style="margin-bottom: 10px">订单明细：</el-col>
        <el-col :span="24">
          <el-descriptions class="margin-top" :column="2" border>
            <el-descriptions-item>
              <template slot="label"> 订单号 </template>
              {{ orderDetail.orderNo }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label"> 支付方式 </template>
              <template v-if="orderDetail.payType == '0'">微信</template>
              <template v-if="orderDetail.payType == '1'">支付宝</template>
              <template v-if="orderDetail.payType == '2'">现金</template>
              <template v-if="orderDetail.payType == '3'">余额</template>
              <template v-if="orderDetail.payType == '9'">组合</template>
              （{{ orderDetail.payAmount }}元）
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label"> 客户姓名 </template>
              {{ orderDetail.appUserName }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label"> 客户电话 </template>
              {{ orderDetail.appUserPhone }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label"> 付款金额（元） </template>
              {{ orderDetail.payAmount }}
            </el-descriptions-item>
            <el-descriptions-item label="储值金额" v-if="orderDetail.orderStatus == '2'">{{ orderDetail.rechargeAmount }} 元</el-descriptions-item>
            <el-descriptions-item label="储值赠送金额" v-if="orderDetail.orderStatus == '2'">{{ orderDetail.giveAmount }} 元</el-descriptions-item>
            <el-descriptions-item label="储值福利金额" v-if="orderDetail.orderStatus == '2'">{{ orderDetail.welfareAmount }} 元</el-descriptions-item>
            <el-descriptions-item>
              <template slot="label"> 下单时间 </template>
              {{ orderDetail.createTime }}
            </el-descriptions-item>
            <el-descriptions-item :span="2">
              <template slot="label"> 充值门店 </template>
              {{ orderDetail.storeName ? orderDetail.storeName : '' }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label"> 备注 </template>
              {{ orderDetail.remark }}
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
        <!--<el-col :span="24" style="margin-bottom: 10px;margin-top: 20px">分段支付记录：</el-col>
         <el-col :span="24" >
          <el-table fit highlight-current-row border stripe :data="orderDetail.orderRechargeInstalments"
            v-if="orderDetail.orderRechargeInstalments">
            <el-table-column label="子订单号" align="center" prop="orderNo" />
            <el-table-column label="支付金额" align="center" prop="payAmount" />
            <el-table-column label="支付方式" align="center" prop="payType">
              <template slot-scope="scope">
                <div v-if="scope.row.payType != ''">
                  <dict-tag :options="dict.type.recharge_pay_type" :value="scope.row.payType" />
                </div>
              </template>
            </el-table-column>
            <el-table-column label="支付状态" align="center" prop="orderStatus">

              <template slot-scope="scope">
                <div v-if="scope.row.payType != ''">
                  <dict-tag :options="dict.type.recharge_order_status" :value="scope.row.payType" />
                </div>
              </template>
            </el-table-column>

            <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
          </el-table>

        </el-col> -->

        <el-col :span="24" style="margin-top: 20px; margin-bottom: 10px" v-if="orderDetail.rechargePasswordCard">储值卡明细：</el-col>
        <el-col :span="24" v-if="orderDetail.rechargePasswordCard">
          <el-descriptions class="margin-top" :column="2" border>
            <el-descriptions-item>
              <template slot="label"> 卡号 </template>
              {{ orderDetail.rechargePasswordCard.cardNo }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label"> 储值金额（元） </template>
              {{ orderDetail.rechargePasswordCard.rechargeAmount }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label"> 赠送金额（元） </template>
              {{ orderDetail.rechargePasswordCard.giveAmount }}
            </el-descriptions-item>

            <el-descriptions-item>
              <template slot="label"> 储值剩余金额（元） </template>
              {{ orderDetail.rechargePasswordCard.rechargeBalance }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label"> 赠送剩余金额（元） </template>
              {{ orderDetail.rechargePasswordCard.giveBalance }}
            </el-descriptions-item>
            <el-descriptions-item>
              <template slot="label"> 赠送积分</template>
              {{ orderDetail.rechargePasswordCard.givePointAmount }}
            </el-descriptions-item>

            <el-descriptions-item :span="2">
              <template slot="label"> 储值卡状态 </template>
              <dict-tag :options="dict.type.recharge_use_status" :value="orderDetail.rechargePasswordCard.rechargeStatus" />
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
      </el-row>

      <div slot="footer" class="dialog-footer">
        <!--        <el-button type="primary" @click="submitForm" style="margin-left: 5%">提交</el-button>-->
        <el-button @click="cancel" style="margin-left: 5%">关 闭</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="refundDescribe">
          <el-input v-model="refundVO.refundDescribe" 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 { listOrder, getOrder, delOrder, addOrder, updateOrder, getPayDetailById } from '@/api/recharge/order'
import { allOrg } from '@/api/system/store'
import { refundOrderGoods } from '@/api/order/goods'
import { refundRecharge } from '@/api/recharge/refund'

export default {
  name: 'OrderRechargeRecord',
  dicts: ['recharge_use_status', 'recharge_refund_status', 'recharge_pay_type', 'recharge_order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值订单表格数据
      orderList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openRefundPage: false,
      // 查询参数
      queryParams: {
        orderNo: null,
        sourceType: null,
        orgId: null,
        storeName: null,
        phoneNumber: null,
        realName: null,
        orderStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      queryCreateTime: null,
      queryPayTime: null,
      userInfoVO: null,
      orderDetail: {},
      storeList: [],
      payDetailList: [],
      refundVO: {
        orderId: null,
        refundDescribe: null
      }
    }
  },
  created() {
    allOrg().then((res) => {
      res.data.forEach((org) => {
        if (org.sourceType == '02') {
          this.storeList.push(org)
        }
      })
    })
    this.handleUser()
    this.getList()
  },
  methods: {
    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]
      }
    },
    handleUser() {
      this.userInfoVO = this.getUserInfo()
    },
    /** 查询充值订单列表 */
    getList() {
      this.$nextTick(() => {
        this.$refs.pagination.handleSearch(true)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      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 = '添加充值订单'
    },
    /** 支付明细详情 */
    handleDetail(row) {
      this.reset()
      const id = row.id
      getOrder(id).then((response) => {
        this.orderDetail = response.data
        this.open = true
        this.title = '支付明细详情'
      })
    },
    // 撤单
    refundOrder(row) {
      this.refundVO = {}
      this.openRefundPage = true
      this.refundVO.orderId = row.id
    },
    submitRefund() {
      this.$confirm('是否确认撤回该订单？')
        .then(() => {
          refundRecharge(this.refundVO).then((response2) => {
            this.$message.success('操作成功')
            this.getList()
            this.openRefundPage = false
            this.refundVO.orderId = null
            this.refundVO.refundReason = null
          })
        })
        .catch(() => { })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除充值订单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delOrder(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'mapi/recharge/order/export',
        {
          ...this.queryParams
        },
        `recharge_order_${new Date().getTime()}.xlsx`
      )
    },
    checkClose(done) {
      this.$confirm('是否关闭表单，关闭后数据将丢失？')
        .then(function () {
          done()
        })
        .then(() => { })
        .catch(() => { })
    }
  }
}
</script>
