<template>
  <div class="divBox">
    <el-card class="box-card">
      <!--顶部搜索-->
      <div slot="header" class="clearfix">
        <el-form inline size="small">
          <el-form-item>
            <el-input v-model="tableFrom.uid" placeholder="请输入用户UID" class="selWidth" />
            <el-input v-model="tableFrom.nickname" placeholder="请输入用户名称" class="selWidth" @keyup.enter.native="getList(1)">
              <el-button slot="append" icon="el-icon-search" class="el-button-solt" @click="getList(1)" />
            </el-input>
          </el-form-item>
          <el-form-item v-if="config.merchant_type == 0">
            <span style="font-size: 12px;color: #C0C4CC;">注：将用户添加为员工时,请确保用户先关注本店铺</span>
          </el-form-item>
        </el-form>
      </div>
      <!--表单-->
      <el-table v-loading="loading" :data="tableData.data" style="width: 100%" size="small">
        <el-table-column label="选择" min-width="65" align="center">
          <template scope="scope">
            <el-radio v-model="templateRadio" :label="scope.row.uid" @change.native="getTemplateRow(scope.$index,scope.row)">&nbsp;</el-radio>
          </template>
        </el-table-column>
        <el-table-column prop="uid" label="ID" min-width="60" align="center"/>
        <el-table-column prop="nickname" label="微信用户名称" min-width="130" align="center"/>
        <el-table-column label="员工头像" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image class="tabImage" :src="scope.row.avatar ? scope.row.avatar : moren" :preview-src-list="[scope.row.avatar || moren]" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="用户类型" min-width="130" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.user_type | statusFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否已成为员工" min-width="150" align="center" v-if="config.merchant_type == 0">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_store_service == 1" type="success" effect="dark">是</el-tag>
            <el-tag v-else type="info" effect="dark">否</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>

import { userLstApi } from '@/api/system'

export default {
  name: 'UserList',
  filters: {
    saxFilter(status) {
      const statusMap = {
        0: '未知',
        1: '男',
        2: '女'
      }
      return statusMap[status]
    },
    statusFilter(status) {
      const statusMap = {
        'wechat': '微信用户',
        'routine': '小程序用户',
        'h5': 'H5用户',
        'app': 'APP用户',
        'pc': 'PC用户'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      config: {},
      moren: require('@/assets/images/f.png'),
      templateRadio: 0,
      loading: false,
      tableData: {
        data: [],
        total: 0
      },
      tableFrom: {
        page: 1,
        limit: 20,
        nickname: '',
        uid: ''
      }
    }
  },
  mounted() {
    this.getList(1)
    // 获取配置信息
    this.$root.getConfig().then((res) => {
      this.config = res
    })
  },
  methods: {
    getTemplateRow(idx, row) {
      /* eslint-disable */
      form_create_helper.set(this.$route.query.field, {src:row.avatar || this.moren,id:row.uid})
      form_create_helper.close(this.$route.query.field);
    },
    // 列表
    getList(num) {
      this.loading = true
      this.tableFrom.page = num ? num : this.tableFrom.page
      userLstApi(this.tableFrom).then(res => {
        this.tableData.data = res.data.list
        this.tableData.total = res.data.count
        this.loading = false
      }).catch(res => {
        this.$message.error(res.message)
        this.loading = false
      })
    },
    pageChange(page) {
      this.tableFrom.page = page
      this.getList('')
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val
      this.getList('')
    }
  }
}
</script>

<style scoped>
.selWidth{
  width: 300px!important;
  margin-bottom: 10px!important;
}
</style>
