<template>
  <div class="NCC-common-layout">
    <div class="NCC-common-layout-center">
      <!-- 筛选条件 -->
      <el-row class="NCC-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="会员">
              <el-select
                v-model="query.memberIds"
                placeholder="请选择会员"
                multiple
                filterable
                remote
                :remote-method="remoteSearchMembers"
                :loading="memberLoading"
                clearable
                :style='{"width":"100%"}'>
                <el-option
                  v-for="member in memberOptions"
                  :key="member.id"
                  :label="member.label"
                  :value="member.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否升医美">
              <el-select
                v-model="query.hasUpgradeMedicalBeauty"
                placeholder="请选择"
                clearable
                :style='{"width":"100%"}'>
                <el-option label="是" :value="true"></el-option>
                <el-option label="否" :value="false"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否升科美">
              <el-select
                v-model="query.hasUpgradeTechBeauty"
                placeholder="请选择"
                clearable
                :style='{"width":"100%"}'>
                <el-option label="是" :value="true"></el-option>
                <el-option label="否" :value="false"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否升生美">
              <el-select
                v-model="query.hasUpgradeLifeBeauty"
                placeholder="请选择"
                clearable
                :style='{"width":"100%"}'>
                <el-option label="是" :value="true"></el-option>
                <el-option label="否" :value="false"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
              <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>

      <!-- 数据表格 -->
      <div class="NCC-common-layout-main NCC-flex-main">
        <NCC-table v-loading="listLoading" :data="list" has-c>
          <!-- <el-table-column prop="MemberId" label="会员ID" min-width="180">
            <template slot-scope="scope">
              <i class="el-icon-user-solid" style="margin-right: 4px; color: #409EFF;"></i>
              <span>{{ scope.row.MemberId || '无' }}</span>
            </template>
          </el-table-column> -->
          <el-table-column prop="MemberName" label="会员名称" min-width="120">
            <template slot-scope="scope">
              <i class="el-icon-user" style="margin-right: 4px; color: #409EFF;"></i>
              <span>{{ scope.row.MemberName || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="MemberPhone" label="会员手机" min-width="130">
            <template slot-scope="scope">
              <i class="el-icon-phone" style="margin-right: 4px; color: #67C23A;"></i>
              <span>{{ scope.row.MemberPhone || '无' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="HasUpgradeMedicalBeauty" label="是否升医美" min-width="120">
            <template slot-scope="scope">
              <el-tag 
                :type="scope.row.HasUpgradeMedicalBeauty === '是' ? 'success' : 'info'" 
                size="small">
                {{ scope.row.HasUpgradeMedicalBeauty || '无' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="HasUpgradeTechBeauty" label="是否升科美" min-width="120">
            <template slot-scope="scope">
              <el-tag 
                :type="scope.row.HasUpgradeTechBeauty === '是' ? 'success' : 'info'" 
                size="small">
                {{ scope.row.HasUpgradeTechBeauty || '无' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="HasUpgradeLifeBeauty" label="是否升生美" min-width="120">
            <template slot-scope="scope">
              <el-tag 
                :type="scope.row.HasUpgradeLifeBeauty === '是' ? 'success' : 'info'" 
                size="small">
                {{ scope.row.HasUpgradeLifeBeauty || '无' }}
              </el-tag>
            </template>
          </el-table-column>
        </NCC-table>
        <pagination 
          v-show="total > 0" 
          :total="total" 
          :page.sync="listQuery.pageIndex"
          :limit.sync="listQuery.pageSize" 
          @pagination="handlePagination" 
        />
      </div>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'
import Pagination from '@/components/Pagination'

export default {
  name: 'MemberUpgradeStatistics',
  components: {
    Pagination
  },
  data() {
    return {
      query: {
        memberIds: [],
        hasUpgradeMedicalBeauty: null,
        hasUpgradeTechBeauty: null,
        hasUpgradeLifeBeauty: null
      },
      memberOptions: [],
      memberLoading: false,
      list: [],
      listLoading: false,
      total: 0,
      listQuery: {
        pageIndex: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.initMemberOptions()
    this.search()
  },
  methods: {
    // 初始化会员选项（加载前10个）
    initMemberOptions() {
      request({
        url: '/api/Extend/LqKhxx',
        method: 'GET',
        data: {
          currentPage: 1,
          pageSize: 10
        }
      }).then(res => {
        if (res.code == 200 && res.data && res.data.list) {
          this.memberOptions = res.data.list.map(item => ({
            id: item.id,
            label: item.khmc || item.id
          }))
        } else {
          this.memberOptions = []
        }
      }).catch(() => {
        this.memberOptions = []
      })
    },

    // 远程搜索会员
    remoteSearchMembers(query) {
      if (query !== '') {
        this.memberLoading = true
        request({
          url: '/api/Extend/LqKhxx',
          method: 'GET',
          data: {
            currentPage: 1,
            pageSize: 10,
            keyword: query
          }
        }).then(res => {
          this.memberLoading = false
          if (res.code == 200 && res.data && res.data.list) {
            this.memberOptions = res.data.list.map(item => ({
              id: item.id,
              label: item.khmc || item.id
            }))
          } else {
            this.memberOptions = []
          }
        }).catch(() => {
          this.memberLoading = false
          this.memberOptions = []
        })
      } else {
        this.memberOptions = []
        this.initMemberOptions()
      }
    },

    // 查询数据
    search() {
      this.listLoading = true

      const params = {
        pageIndex: this.listQuery.pageIndex,
        pageSize: this.listQuery.pageSize
      }

      if (this.query.memberIds && this.query.memberIds.length > 0) {
        params.memberIds = this.query.memberIds
      }

      if (this.query.hasUpgradeMedicalBeauty !== null && this.query.hasUpgradeMedicalBeauty !== undefined) {
        params.hasUpgradeMedicalBeauty = this.query.hasUpgradeMedicalBeauty
      }

      if (this.query.hasUpgradeTechBeauty !== null && this.query.hasUpgradeTechBeauty !== undefined) {
        params.hasUpgradeTechBeauty = this.query.hasUpgradeTechBeauty
      }

      if (this.query.hasUpgradeLifeBeauty !== null && this.query.hasUpgradeLifeBeauty !== undefined) {
        params.hasUpgradeLifeBeauty = this.query.hasUpgradeLifeBeauty
      }

      request({
        url: '/api/Extend/lqstatistics/get-member-upgrade-statistics-list',
        method: 'POST',
        data: params
      }).then(res => {
        if (res.data && res.data.list) {
          this.list = res.data.list
          this.total = (res.data.pagination && res.data.pagination.total) || res.data.list.length || 0
        } else {
          this.list = []
          this.total = 0
        }
        this.listLoading = false
      }).catch(err => {
        console.error('查询失败:', err)
        this.$message({
          type: 'error',
          message: '查询失败，请重试',
          duration: 1500
        })
        this.listLoading = false
        this.list = []
        this.total = 0
      })
    },

    // 处理分页变化
    handlePagination({ page, limit }) {
      this.listQuery.pageIndex = page
      this.listQuery.pageSize = limit
      this.search()
    },

    // 重置查询条件
    reset() {
      this.query = {
        memberIds: [],
        hasUpgradeMedicalBeauty: null,
        hasUpgradeTechBeauty: null,
        hasUpgradeLifeBeauty: null
      }
      this.listQuery.pageIndex = 1
      this.listQuery.pageSize = 10
      this.list = []
      this.total = 0
      this.search()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

