<template>
  <el-dialog title="选择订单" :visible.sync="show" width="1200px" :before-close="close">
    <div v-loading="loading">
      <div class="pb20">
        <el-input v-model="listParams.orderNo" class="w300" placeholder="请输入订单流水号" @change="getTableList"><i slot="prefix" class="el-input__icon el-icon-search"></i></el-input>
        <span class="fz12 gray03 pl20">*仅支持通过订单流水号精确搜索查找订单 </span>
      </div>
      <el-table tooltip-effect="dark" :data="tableList" style="width: 100%" @row-click="rowClick">
        <el-table-column :show-overflow-tooltip="false" :width="40" align="left" prop="orderId" fixed="left">
          <template slot-scope="scope">
            <div class="sms-record_left label-hidden">
              <el-radio v-model="selectedId" :label="scope.row.orderId" class="pr10"></el-radio>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="orderNo" label="订单流水号" min-width="140"> </el-table-column>
        <el-table-column label="订单时间" min-width="120">
          <span slot-scope="scope" v-html="listTime(scope.row.orderTime)"></span>
        </el-table-column>
        <el-table-column prop="date" label="会员信息" min-width="180">
          <template slot-scope="scope">
            <member-info :member-name="scope.row.name" :member-image="scope.row.memberImage" :member-phone="scope.row.phone" show-phone-tip></member-info>
          </template>
        </el-table-column>
        <el-table-column label="订单类型" min-width="120">
          <span slot-scope="scope" v-html="fotmateOrderStatus(scope.row)"></span>
        </el-table-column>
        <el-table-column prop="storeName" label="核销门店" min-width="130" show-overflow-tooltip> </el-table-column>
        <el-table-column prop="clerkName" label="导购" min-width="120"> </el-table-column>
        <el-table-column prop="paidAmount" label="实付金额" min-width="120" fixed="right"> </el-table-column>
      </el-table>
    </div>
    <!-- 不分页 -->
    <!-- <dm-pagination v-show="tableList.length" background class="dm-pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listParams.currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="listParams.pageSize" layout="total, sizes, prev, pager, next" :total="total"></dm-pagination> -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="submit">确认</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { listTime } from '@/utils/index.js';
import gicCardApi from '@/api/gic-card.js';
import { page, formate } from '@/mixins/table.js';
const { couponConsumeOrder: gicCouponConsumeOrder, couponConsume: gicCouponConsume } = gicCardApi;
export default {
  name: 'CardOrder',
  mixins: [ page, formate ],
  props: {
    obj: {
      type: Object,
      default() {
        return {
          memberId: '',
          coupCardId: '',
          orderNumber: ''
        };
      }
    },
    show: {
      type: Boolean,
      default: false
    },
    isGic: {
      type: Boolean,
      default: false, // 是否是gic卡券 默认为微信卡券
    }
  },
  data() {
    return {
      loading: false,
      listParams: {
        // currentPage: 1,
        // pageSize: 20,
        orderNo: '',
      },
      // total: 0,
      tableList: [],
      selectedId: '',
      // selectedObj: {},
    };
  },
  methods: {
    listTime,
    fotmateOrderStatus(row) {
      let str;
      // orderStatus  销售单101、退货单102、换货单103、仅退款单104
      switch (row.orderStatus) {
        case 101:
          str = '销售单';
          break;
        case 102:
          str = '退货单';
          break;
        case 103:
          str = '换货单';
          break;
        case 104:
          str = '仅退款单';
          break;
        default:
          str = '--';
          break;
      }
      return str;
    },
    // 加载列表
    async getTableList() {
      this.loading = true;
      this.selectedId = '';
      let res = {};
      try {
        if(this.isGic) {
          res = await gicCouponConsumeOrder(this.listParams);
        }
        this.tableList = res.result ? [ res.result ] : [];
        // this.total = res.result.totalCount;
      } catch (err) {
        this.$tips({ type: 'error', message: err.message || '初始化失败' });
      }
      this.loading = false;
    },
    close() {
      this.loading = false;
      this.selectedId = '';
      this.listParams.orderNo = '';
      // this.listParams.currentPage = 1;
      this.tableList = [];
      this.$emit('update:show', false);
    },
    // 点击行选择卡券
    rowClick(row) {
      this.selectedId = row.orderId;
      // this.selectedObj = row;
    },
    async submit() {
      if (!this.selectedId) {
        this.$tips({ type: 'warning', message: '未选择订单' });
        return;
      } else {
        this.loading = true;
        // 核销订单
        const params = { couponLogId: this.obj.couponLogId, orderId: this.selectedId };
        let res = {}; // eslint-disable-line
        try {
          if(this.isGic) {
            res = await gicCouponConsume(params);
          }
          this.loading = false;
          this.$emit('refresh', false);
          this.close();
          this.$tips({ type: 'success', message: '核销成功' });
        } catch (err) {
          this.$tips({ type: 'warning', message: err.message || '核销失败' });
        }
      }
    }
  },
  watch: {
    show(val) {
      this.listParams.coupLogId = this.obj.coupLogId;
      this.listParams.memberId = this.obj.memberId;
    }
  },
};
</script>
