<template>
  <div class="detail-container">
    <el-page-header @back="goBack" content="优惠码明细"></el-page-header>
    <el-divider></el-divider>

    <!-- 筛选条件 -->
    <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="code">
              <el-input v-model="screenForm.code" placeholder="请输入优惠码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="状态" prop="status">
              <el-select v-model="screenForm.status" placeholder="全部" filterable>
                <el-option label="全部" value=""></el-option>
                <el-option :label="item.label" :value="item.value" v-for="(item, index) in selectList" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <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" class="tr">
            <el-form-item label="">
              <el-button size="small" @click="resetScreenForm">清空</el-button>
              <el-button size="small" type="primary" @click="submitScreenForm">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="mymain-container">
      <div class="btn-group clearfix">
        <div class="fr">
          <el-button size="small" type="primary" icon="el-icon-download" @click="handleExport">导出</el-button>
        </div>
      </div>

      <div class="table">
        <el-table v-loading="listLoading" :data="dataList" element-loading-text="Loading" border fit highlight-current-row stripe>
          <el-table-column align="center" label="优惠码" prop="id" min-width="120"></el-table-column>
          <el-table-column align="center" label="生成时间" prop="bindTime" min-width="160"></el-table-column>
          <el-table-column align="center" label="金额" prop="amount" min-width="100"></el-table-column>
          <el-table-column align="center" label="有效期" min-width="200">
            <template slot-scope="scope">
              {{ scope.row.startTime | dateToDayFilter }} 至 {{ scope.row.endTime | dateToDayFilter }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="状态" prop="status" min-width="120">
            <template slot-scope="scope">
              {{ scope.row.status | DISCODE_STATUS_FILTER}}
            </template>
          </el-table-column>
          <el-table-column align="center" label="使用时间" prop="useTime" min-width="160"></el-table-column>
          <el-table-column align="center" label="订单编号" prop="orderId" min-width="200"></el-table-column>
          <el-table-column align="center" label="使用人昵称" prop="useNickName" min-width="120"></el-table-column>

          <el-table-column align="center" label="操作" fixed="right" width="120">
            <template slot-scope="scope">
              <el-button type="text" v-if="scope.row.status !== 0" @click="toOrderDetail(scope.row.orderId)">订单详情</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination clearfix">
        <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>
    </div>

  </div>

</template>

<script>
import { getDetail } from "@/api/discode";
import { DISCODE_STATUS } from '@/utils/select_data'
import { downloadFiles } from '@/utils/util'

export default {
  props: ['listItem'],
  data() {
    return {
      dataList: null, // 列表数据
      listLoading: true, // 列表加载loading
      screenForm: { // 筛选表单数据
        code: '',
        status: '',
        orderId: '',
      },
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      selectList: DISCODE_STATUS,
    }
  },
  computed: {
    userInfo() {
      return JSON.parse(localStorage.getItem('settlement_user'))
    },
  },
  created() {
    this.getList();
  },
  methods: {
    // 返回主页
    goBack() {
      this.$emit('backListFormDetail');
    },

    // 获取详情
    getList() {
      this.listLoading = true;

      let params = {
        userId: this.listItem.userId,
        id: this.screenForm.code,
        status: this.screenForm.status,
        orderId: this.screenForm.orderId,
        pageNo: this.currentPage,
        pageSize: this.pageSize
      }

      getDetail(params).then(res => {
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
      })
    },

    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1;
      this.getList();
    },

    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields();
      this.currentPage = 1;
      this.getList();
    },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getList();
    },

    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },

    toOrderDetail(orderId) {
      let {href} = this.$router.resolve({path: `/order/detail?orderId=${orderId}`});
      window.open(href, '_blank');
    },

    // 导出
    handleExport() {
      let screenData = {
        userId: this.listItem.userId,
        code: this.screenForm.code,
        status: this.screenForm.status,
        orderId: this.screenForm.orderId,
      };
      downloadFiles('exchange/code/export', screenData);
    },

  }
}

</script>

<style scoped lang="scss">
  .detail-container {
    width:100%;
    height: 100%;
  }
</style>