<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.level" clearable style="width: 200px" class="filter-item" placeholder="是否超级会员">
                <el-option label="超级会员" :value="1" />
                <el-option label="普通会员" :value="0" />
            </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</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="nickname" />

      <el-table-column align="center" label="手机号码" prop="tel" />

      <el-table-column align="center" label="是否为引荐人人" :formatter="formatBoolean" prop="isDis" />
      <el-table-column align="center" label="是否实名制" :formatter="formatNid" prop="customerIdcard" />
        <el-table-column align="center" label="超级会员" prop="tel"  >
             <template slot-scope="scope">
               <span v-if="scope.row.level&&scope.row.level==1" style="color:red;">超级会员</span>
               <span v-else>普通会员</span>

             </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">{{ scope.row.channelId | channelsFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="数据来源" prop="source" />

      <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="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="success" v-if="!scope.row.isDis" v-permission="['POST /admin/user/star']"  @click="handleReferee(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" />

 <!-- 引荐人添加 -->
    <el-dialog title="引荐人设置" :visible.sync="refereeFormVisible" width="600px">
      <el-form ref="refereeForm" :rules="rules" :model="refereeForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
         <el-form-item label="名称" prop="refereeName">
                <el-input v-model="refereeForm.refereeName" placeholder="请输入引荐人名称"/>
           </el-form-item>

        <el-form-item label="引荐人类型" prop="type">
          <el-select v-model="refereeForm.type" clearable style="width: 100%" class="filter-item" placeholder="请选择引荐人类型">
           <el-option  label="公司员工" value="0"/>
           <el-option  label="大团长" value="1"/>
           <!-- <el-option  label="一级引荐人" value="2"/>
           <el-option  label="其他引荐人" value="3"/>  -->
          </el-select>
        </el-form-item>

        <el-form-item label="引荐人企微id" prop="refereeWxPlugid">
                <el-input v-model="refereeForm.refereeWxPlugid" placeholder="请输入企业微信-联系我中的id"/>
           </el-form-item>

          <el-form-item label="企微客户维护" prop="refereeForm.csrType">
              <el-radio-group v-model="refereeForm.csrType">
                  <el-radio :label="0">自己</el-radio>
                  <!-- <el-radio :label="1">其他人</el-radio>  -->
                </el-radio-group>
           </el-form-item>

        

     <el-form-item label="企微服务人员" v-show="refereeForm.csrType==1" prop="csrId">
       <el-select v-model="refereeForm.csrId" style="width: 100%" class="filter-item" placeholder="请选择服务维护人员">
        <el-option v-for="item in roots" :key="item.refereeId" :label="item.refereeName" :value="item.refereeId"/>
      </el-select>
    </el-form-item>
    
 
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="refereeFormVisible = false">取消</el-button> 
        <el-button type="primary" @click="joinReferee">确定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { fetchList } from '@/api/user'
import { allChannel } from '@/api/channel'
import { joinReferee,listRoots } from '@/api/referee'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
let vm

export default {
  name: 'User',
  components: { Pagination },
  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,
      citys:[], 
      roots:[],
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        username: undefined,
        mobile: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      refereeFormVisible:false,
      refereeForm:{
        refereeName:'',
        id:null,
        type:null,
        csrType:0,
        csrType:null
      },
      downloadLoading: false,
      genderDic: ['未知', '男', '女'],
      statusDic: ['可用', '禁用', '注销'],
      disType: ['否', '是'],
      rules: {
        type: [{ required: true, message: '引荐人类型不能为空', trigger: 'blur' }], 
        refereeName: [{ required: true, message: '引荐人名称不能为空', trigger: 'blur' }], 
      },
    }
  },
  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()
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['用户名', '手机号码', '性别', '生日', '状态']
        const filterVal = ['username', 'mobile', 'gender', 'birthday', 'status']
        var exportList = []
        this.list.map(v => {
          var obj = JSON.parse(JSON.stringify(v))
          obj.gender = this.$data.genderDic[obj.gender]
          obj.status = this.$data.statusDic[obj.status]
          exportList.push(obj)
        })
        excel.export_json_to_excel2(tHeader, exportList, filterVal, '用户信息')
        this.downloadLoading = false
      })
    }, 
    joinReferee(){
        this.$refs['refereeForm'].validate((valid) => {
        if (valid) {
         this.$confirm('确定将会员设置为引荐人?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
        }).then(() => {
          console.info(this.refereeForm)
          joinReferee(this.refereeForm).then(res=>{
            this.refereeFormVisible=false
            this.getList()
          }).catch(res=>{
             this.$notify.error({
              title: '失败',
              message: res.data.errmsg
            })
          })

         })

        }
       }) 
    },
    handleReferee(row) {
      this.refereeForm={
        refereeName:row.customerName,
        id:row.id,
        type:null,
        csrType:0 
      };
      
      this.refereeFormVisible=true; 
       if(this.roots.length==0){
        listRoots().then(res=>{
          this.roots=res.data.data
        })
      }
    },
    formatBoolean: function(row, column, cellValue) {
      var ret = ''
      if (cellValue) {
        ret = '是'
      } else {
        ret = '否'
      }
      return ret
    },
    formatNid: function(row, column, cellValue) {
      var ret = ''
      if (cellValue != undefined && cellValue != null && cellValue != '') {
        ret = '是'
      } else {
        ret = '否'
      }
      return ret
    }

  }
}
</script>

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