<template>
  <div class="app-container">

    <div v-show="!isShowDetail">
      <!-- 筛选条件 -->
      <div class="screen-container">
        <div class="top clearfix">
          <div class="title fl">条件筛选</div>
        </div>
        <el-form ref="screenForm" :model="screenForm" label-width="90px" size="small" label-position="left">
          <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="会员名称" prop="keyword">
                <el-input v-model="screenForm.keyword" placeholder="请输入名称/电话"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="网点名称" prop="websitId">
                <el-select v-model="screenForm.websitId" placeholder="全部" filterable>
                  <el-option label="全部" value=""></el-option>
                  <el-option :label="item.name" :value="item.id" v-for="(item, index) in websiteList" :key="index"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="24" :lg="12" 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="nickName" min-width="120"></el-table-column>
            <el-table-column align="center" label="电话" prop="userPhone" min-width="120"></el-table-column>
            <el-table-column align="center" label="头像" prop="avatar">
                <template slot-scope="scope">
                  <div class="demo-image__preview">
                    <el-image
                      style="width: 40px; height: 40px"
                      :src="scope.row.avatar"
                      :preview-src-list="[scope.row.avatar]">
                      <div slot="error" class="image-slot">
                        <i class="el-icon-picture-outline"></i>
                      </div>
                    </el-image>
                  </div>
                </template>
              </el-table-column>
            <el-table-column align="center" label="所属网点" prop="websitName" min-width="120"></el-table-column>
            <el-table-column align="center" label="首次生成时间" prop="firstTime" min-width="160"></el-table-column>
            <el-table-column align="center" label="生成数量" prop="num" min-width="100"></el-table-column>
            <el-table-column align="center" label="生成总金额" prop="totalAmount" min-width="100"></el-table-column>
            <el-table-column align="center" label="使用数量" prop="useNum" min-width="100"></el-table-column>

            <el-table-column align="center" label="操作" fixed="right" width="120">
              <template slot-scope="scope">
                <el-button type="text" @click="toDetail(scope.row)">查看明细</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>
    
    <DiscodeDetail :listItem="queryItem" v-if="isShowDetail" @backListFormDetail="backList" />
    
  </div>
</template>

<script>
import { getList, getWebsiteList } from "@/api/discode";
import { downloadFiles } from '@/utils/util'
import DiscodeDetail from "@/views/activity/components/discode_detail";

export default {
  components: {
    DiscodeDetail
  },
  data() {
    return {
      dataList: null, // 列表数据
      listLoading: true, // 列表加载loading
      screenForm: { // 筛选表单数据
        keyword: '',
        websitId: '',
      },
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      websiteList: [],

      queryItem: {},
    }
  },
  computed: {
    isShowDetail() {
      return this.queryItem.hasOwnProperty('userId')
    }
  },
  created() {
    this.getList();
    this.getWebsiteList();
  },
  methods: {
    // 网点列表
    getWebsiteList() {
      getWebsiteList({
        pageNum: 1,
        pageSize: 1000
      }).then(res => {
        this.websiteList = res.data.records;
      })
    },

    getList() {
      this.listLoading = true;

      let params = {
        keyword: this.screenForm.keyword,
        adminWebsitId: this.screenForm.websitId,
        pageNo: this.currentPage,
        pageSize: this.pageSize
      }

      getList(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();
    },

    // 导出
    handleExport() {
      let screenData = {
        keyword: this.screenForm.keyword,
        adminWebsitId: this.screenForm.websitId
      };
      downloadFiles('exchange/code/count/export', screenData);
    },

    // 进入详情
    toDetail(item) {
      this.queryItem = item;
    },

    backList() {
      this.queryItem = {};
    },
    
  }
}
</script>

<style scoped lang="scss">
  .dialog-container {
    .left {
      width: 140px;
      height: 350px;
      overflow-y: scroll;
      .group {
        margin-top: 10px;
      }
      .child {
        margin-top: 5px;
        .item {
          padding-left: 18px;
        }
      }
      .item {
        cursor: pointer;
        line-height: 24px;
      }
    }
    .right {
      width: calc(100% - 140px);
      height: 350px;
      box-sizing: border-box;
      padding-left: 20px;
    }
  }
  .goods-container {
    .item {
      margin-bottom: 10px;
      &:last-child {
        margin-bottom: 0;
      }
    }
  }
  .couponDetail {
    height: 60vh;
    overflow-y: scroll;
    padding-right: 20px;
    .item {
      font-size: 16px;
      margin-bottom: 20px;
      &>span {
        float: left;
        &:first-child {
          width: 140px;
          text-align: right;
          margin-right: 10px;
          font-weight: 500;
        }
      }
      .table-span {
        width: calc(100% - 170px);
      }
    }
  }

  .addMember {
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #EBEEF5;
    border-top: none;
    height: 50px;
  }

  // .memberTable {
  //   ::v-deep .el-table__header-wrapper .el-checkbox {
  //     display: none;
  //   }
  // }
    
    
</style>
