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

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.username" clearable class="filter-item" style="width: 200px;" placeholder="请输入用户名" />
      <el-input v-model="listQuery.mobile" clearable class="filter-item" style="width: 200px;" placeholder="请输入手机号" />
      <el-select v-model="listQuery.type" clearable style="width: 230px" class="filter-item" placeholder="请选择客户类型">
        <el-option v-for="type in defaultTypeOptions" :key="type.label" :label="type.value" :value="type.label" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" width="100px" label="用户ID" prop="customerNo" sortable />

      <el-table-column align="center" label="用户名" prop="customerName" />

      <el-table-column align="center" label="手机号码" prop="tel" />
 <el-table-column align="center" label="客户类型" prop="gender">
        <template slot-scope="scope">
          <el-tag>{{ customerDic[scope.row.customerType] }}</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="性别" prop="gender">
        <template slot-scope="scope">
          <el-tag>{{ genderDic[scope.row.gender] }}</el-tag>
        </template>
      </el-table-column> -->

      <el-table-column align="center" label="数据渠道" prop="channelId">
        <template slot-scope="scope" >
          <el-tag type="primary" v-if="scope.row.hasOwnProperty('channelId')">  {{ scope.row.channelId | channelsFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="数据来源" prop="source" >
<template slot-scope="scope">
          <el-tag>{{ source[scope.row.source] }}</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="状态" prop="state">
        <template slot-scope="scope">
          <el-tag>{{ statusDic[scope.row.state] }}</el-tag>
        </template>
      </el-table-column> -->

       <el-table-column align="center" label="创建时间" prop="createTime" />

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-permission="['POST /admin/user/read']" type="primary" size="mini" @click="handleRead(scope.row)">编辑</el-button>
          <el-button v-permission="['POST /admin/user/detail']" type="success" size="mini" @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <addCustomer ref="editCustomerDiaRef" />

  </div>
</template>

<script>
import { read, fetchList } from '@/api/customer'
import { allChannel } from '@/api/channel'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import addCustomer from '../components/addCustomer'

let vm

const defaultTypeMap = [
  { label: '0', value: '客户' },
  { label: '1', value: '服务者' },
  { label: '2', value: '合伙人' },
  { label: '3', value: '其他' }]

export default {
  name: 'User',
  components: { Pagination, addCustomer },
  filters: {
    channelsFilter(value) {
      const rs = vm.channels.filter((item) => {
        return item.id === value
      })

      if (rs != null && rs.length > 0) { return rs[0].name }
    }
  },
  data() {
    return {
      channels: [],
      list: null,
      total: 0,
      listLoading: true,
      defaultTypeOptions: Object.assign({}, defaultTypeMap),
      dialogFormVisible: false,
      listQuery: {
        type: undefined,
        mobile: undefined,
        page: 1,
        limit: 20,
        username: undefined,
        mobile: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      genderDic: ['未知', '男', '女'],
      statusDic: ['可用', '禁用', '注销'],
      customerDic:['客户','服务者','合伙人','其他'],
       source: {
                xinghai: "星海",
                saasmall: "saas商城",
                jzmall: "鲸致商城",
                hema: "盒马",
                IB: "进电",
                OB: "外拨",
                IM: "聊天工具",
                EMAILL: "邮件",
                APP: "APP",
                H5: "H5",
                Other: "其他",
            },
   
    }
  },
  beforeCreate() {
    vm = this
  },
  created() {
    allChannel().then(res => {
      this.channels = res.data.data
    })
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.data.list
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleRead(row) {
      this.$refs.editCustomerDiaRef.editCustomerDia(row)
    },
    handleDetail(row) {
         this.$store.dispatch('CUSTOMER_DETAIL',{id:row.id});
       this.$router.push({
        path: '/user/customerDetail',
      
      })
    }
  }
}
</script>

<style>
.el-table th.gutter {
  display: table-cell !important;
}
</style>
