<template>
  <div class="app-container">
    <!-- 顶部操作区域 -->
    <div class="filter-container">
      <div class="filter-control">
        <span>机构ID:</span>
        <el-input
          v-model="listQuery.clubId"
          style="width: 120px"
          placeholder="机构ID"
          clearable
          maxlength="10"
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>机构名称:</span>
        <el-input
          v-model="listQuery.name"
          placeholder="机构名称"
          clearable
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>手机号:</span>
        <el-input
          v-model="listQuery.contractMobile"
          placeholder="手机号"
          maxlength="11"
          clearable
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>联系人:</span>
        <el-input
          v-model="listQuery.linkMan"
          placeholder="联系人名称"
          clearable
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>机构状态:</span>
        <el-select v-model="listQuery.status" style="width: 150px" clearable @change="getList">
          <el-option value="" label="请选择" />
          <el-option :value="90" label="已上线" />
          <el-option :value="91" label="已下线" />
          <el-option :value="1" label="待审核" />
          <el-option :value="92" label="审核未通过" />
          <el-option :value="93" label="已确认" />
          <el-option :value="94" label="已冻结" />
        </el-select>
      </div>
      <div class="filter-control">
        <span>机构级别:</span>
        <el-select v-model="listQuery.userIdentity" style="width: 150px" clearable @change="getList">
          <el-option value="" label="请选择" />
          <el-option :value="4" label="个人机构" />
          <el-option :value="2" label="资质机构" />
          <el-option :value="8" label="超级会员" />
        </el-select>
      </div>
      <div class="filter-control">
        <span>协销:</span>
        <el-select v-model="listQuery.spId" style="width: 150px" filterable @change="getList">
          <el-option value="" label="请选择" />
          <el-option
            v-for="item in sellerOption"
            :key="item.serviceProviderId"
            :label="item.linkMan"
            :value="item.serviceProviderId"
          />
        </el-select>
      </div>
      <div class="filter-control">
        <span>动态标签:</span>
        <el-select v-model="listQuery.trendsLabel" filterable @change="getList">
          <el-option value="" label="请选择" />
          <el-option
            v-for="(item,index) in trendsLabelOption"
            :key="index"
            :label="item.label"
            :value="item.label"
          />
        </el-select>
      </div>
      <div class="filter-control">
        <span>静态标签:</span>
        <el-select v-model="listQuery.stateLabel" filterable @change="getList">
          <el-option value="" label="请选择" />
          <el-option
            v-for="(item,index) in stateLabelOption"
            :key="index"
            :label="item.label"
            :value="item.label"
          />
        </el-select>
      </div>
      <div class="filter-control">
        <span>注册时间:</span>
        <el-date-picker
          v-model="time"
          type="daterange"
          unlink-panels
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          @change="getList"
        />
      </div>
      <div class="filter-control">
        <span>分配时间:</span>
        <el-date-picker
          v-model="time1"
          type="daterange"
          unlink-panels
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          @change="getList"
        />
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="getList"> 查询 </el-button>
      </div>
    </div>
    <!-- 列表 -->
    <el-table v-loading="isLoading" :data="list" border style="width: 100%" height="660">
      <el-table-column prop="clubId" fixed label="机构ID" align="center" width="80" />
      <el-table-column prop="name" fixed label="机构名称" align="center" />
      <el-table-column prop="linkMan" label="联系人" align="center" width="100" />
      <el-table-column prop="spName" label="协销人员" align="center" width="120" />
      <el-table-column prop="contractMobile" label="手机号" align="center" width="150">
        <template slot-scope="{ row }">
          {{ row.contractMobile ? row.contractMobile : '---' }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="机构状态" align="center" width="100">
        <template slot-scope="{ row }">
          <el-tag v-if="row.status === 1" type="warning" size="small">待审核</el-tag>
          <el-tag v-if="row.status === 90" type="success" size="small">已上线</el-tag>
          <el-tag v-if="row.status === 91" type="info" size="small">已下线</el-tag>
          <el-tag v-if="row.status === 92" type="danger" size="small">审核未通过</el-tag>
          <el-tag v-if="row.status === 93" type="warning" size="small">已确认</el-tag>
          <el-tag v-if="row.status === 94" type="danger" size="small">已冻结</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="userIdentity" label="机构级别" align="center" width="150">
        <template slot-scope="{ row }">
          <template v-if="row.userIdentity === '4'">
            <el-tag v-if="row.userIdentity === '4' && row.svipUserFlag === 1" type="success" size="small">个人机构(超级会员)</el-tag>
            <el-tag v-else type="info" size="small">个人机构(普通会员)</el-tag>
          </template>
          <template v-if="row.userIdentity === '2'">
            <el-tag v-if="row.userIdentity === '2' && row.svipUserFlag ===1" type="success" size="small">资质机构(超级会员)</el-tag>
            <el-tag v-if="row.userIdentity === '2' && row.svipUserFlag !==1 && !row.medicalPracticeLicenseImg" type="warning" size="small">资质机构(高级会员)</el-tag>
            <el-tag v-if="row.userIdentity === '2' && row.svipUserFlag !==1 && row.medicalPracticeLicenseImg" type="danger" size="small">资质机构(医美会员)</el-tag>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="source" label="注册IP" align="center" width="180">
        <template slot-scope="{ row }">
          <el-tag v-if="row.source === '0'" effect="plain" type="warning" size="small">{{ row.registerIP }}(网站)</el-tag>
          <el-tag v-if="row.source === '1'" effect="plain" type="warning" size="small">{{ row.registerIP }}(小程序)</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="addTime" label="注册时间" align="center" width="100">
        <template slot-scope="{ row }">
          {{ row.addTime ? row.addTime : '---' }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button type="success" size="mini" style="margin: 4px" @click="handleRecordDetail(3, row)">
            机构画像
          </el-button>
          <el-button type="primary" size="mini" style="margin: 4px" @click="handleRecordDetail(2, row)">
            行为记录
          </el-button>
          <el-button type="primary" size="mini" style="margin: 4px" @click="handleRecordDetail(1, row)">
            咨询记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[10, 20, 30, 100]"
      :page-size="20"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getClubList"
    />
  </div>
</template>

<script>
import { getClubList, getClubLabel } from '@/api/user/club/club'
import { getServiceList } from '@/api/library/keyword'
import pickerOptions from '@/utils/time-picker.js'
export default {
  name: 'CustomerList',
  filters: {},
  data() {
    return {
      isLoading: true,
      pickerOptions,
      time: [],
      time1: [],
      listQuery: {
        clubId: '',
        name: '',
        contractMobile: '',
        userIdentity: '',
        startTime: '',
        endTime: '',
        allocationStartTime: '',
        allocationEndTime: '',
        newDeal: '',
        spId: '',
        trendsLabel: '',
        stateLabel: '',
        status: '',
        pageNum: 1,
        pageSize: 20
      },
      list: [],
      total: 0,
      sellerOption: [],
      trendsLabelOption: [],
      stateLabelOption: [],
      shopDialogVisible: false,
      dialogFormVisible: false,
      renewCustome: {
        id: '',
        status: ''
      },
      rules: {
        status: [{ required: true, message: '请设置统计状态', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {
    this.getTrendsLabel()
    this.getStateLabel()
    this.getServiceList()
    this.getList()
  },
  mounted() {},
  methods: {
    // 获取行为记录列表
    getList() {
      this.list = []
      this.isLoading = true
      this.listQuery.pageNum = 1
      if (this.time && this.time.length > 0) {
        this.listQuery.startTime = this.time[0]
        this.listQuery.endTime = this.time[1]
      } else {
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
      if (this.time1 && this.time1.length > 0) {
        this.listQuery.allocationStartTime = this.time1[0]
        this.listQuery.allocationEndTime = this.time1[1]
      } else {
        this.listQuery.allocationStartTime = ''
        this.listQuery.allocationEndTime = ''
      }
      this.getClubList()
    },
    // 获取机构列表
    async getClubList() {
      try {
        const res = await getClubList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 获取协销列表选项
    async getServiceList() {
      try {
        const res = await getServiceList()
        this.sellerOption = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 获取动态标签
    async getTrendsLabel() {
      try {
        const res = await getClubLabel({ dynamicStatus: 0 })
        const data = res.data
        this.trendsLabelOption = data
      } catch (error) {
        console.log(error)
      }
    },
    // 获取静态标签
    async getStateLabel() {
      try {
        const res = await getClubLabel({ dynamicStatus: 1 })
        const data = res.data
        this.stateLabelOption = data
      } catch (error) {
        console.log(error)
      }
    },
    // 操作
    handleRecordDetail(type, row) {
      switch (type) {
        case 1: // 咨询记录
          this.$router.push({
            path: '/user/consult/list',
            query: { clubName: row.name, clubId: row.clubId }
          })
          break
        case 2: // 行为记录
          this.$router.push({
            path: '/user/record-list',
            query: { type: 'first', corporateName: row.name, clubId: row.clubId }
          })
          break
        case 3: // 机构画像
          this.$router.push({
            path: '/user/club-portrait',
            query: { clubName: row.name, clubId: row.clubId }
          })
          break
      }
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  float: left;
}
.uploader-tips {
  position: absolute;
  bottom: 0;
  left: 160px;
  line-height: 28px;
  color: red;
  margin: 0;
}
</style>
