<template>
  <div class="app-container">
    <div class="app-header-search">
      <el-form class="demo-form-inline">
        <el-form-item label="机构名称:" style="width:280px;float: left;">
          <el-input v-model="listQuery.userName" placeholder="请输入机构名称" maxlength="50" style="width:200px;" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="联系人:" style="width:230px;float: left;">
          <el-input v-model="listQuery.linkMan" placeholder="请输入联系人姓名" maxlength="10" style="width:150px" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="手机号:" style="width:200px;float: left;">
          <el-input v-model="listQuery.bindMobile" placeholder="请输入手机号" maxlength="11" style="width:130px" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="上线状态:" style="width:190px;float: left;">
          <el-select v-model="listQuery.clubStatus" placeholder="请选择" style="width:100px" value="" @change="handleFilter">
            <el-option label="全部" value="" />
            <el-option label="已上线" value="90" />
            <el-option label="已下线" value="91" />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间:" style="width:410px;float: left;">
          <el-date-picker
            v-model="listQuery.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            style="width: 150px;"
          />
          至
          <el-date-picker
            v-model="listQuery.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            style="width: 150px;"
          />
        </el-form-item>
        <el-form-item style="width:100px;float: left;">
          <el-button type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
        </el-form-item>
        <el-form-item style="width:100px;float: left;">
          <router-link :to="'/club/form/'">
            <el-button type="primary">上线机构</el-button>
          </router-link>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <el-table-column align="center" label="序号" width="50">
        <template slot-scope="scope">
          {{ ((pageNum-1)*listQuery.pageSize)+scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column label="账号" width="150" align="center" prop="account" />
      <el-table-column label="机构名称" width="180" align="center" prop="userName" />
      <el-table-column label="联系人" width="100" align="center" prop="linkMan" />
      <el-table-column label="手机号" width="140" align="center" prop="bindMobile" />
      <el-table-column class-name="status-col" label="上线状态" width="150" align="center" prop="status">
        <template slot-scope="{row}">
          <el-tag v-if="row.clubStatus*1===91" type="danger" size="small">已下线</el-tag>
          <el-tag v-else type="success" size="small">已上线</el-tag>
          <el-button v-if="row.clubStatus === '91'" type="primary" size="mini" @click="handOnline(row)">上线</el-button>
          <el-button v-else type="primary" size="mini" @click="handOffline(row)">下线</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="机构地址" prop="address">
        <template slot-scope="{row}">
          <span>{{ `${row.province}${row.city}${row.town}${row.address}` }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" prop="registerTime">
        <template slot-scope="{row}">
          <span>{{ row.registerTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <router-link :to="{path:'/club/editForm',query:{ clubID:row.clubID, userID: row.userID}}">
            <el-button type="primary" size="mini" style="width: 100px;margin:5px 0 0 0;">编辑</el-button>
          </router-link>
          <router-link :to="{path:'/club/operateList',query:{name:row.name,clubID:row.clubID,userID:row.userID}}">
            <el-button type="primary" size="mini" style="width: 100px;margin:5px 0 0 0;">查看运营人员</el-button>
          </router-link>
          <el-button type="primary" size="mini" style="width: 100px;margin:5px 0 0 0;" @click="handleEdit(row)">添加运营人员</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>20" :total="total" :page.sync="listQuery.index" :limit.sync="listQuery.pageSize" @pagination="fetchData" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="440px">
      <el-form ref="dataForm" :rules="rules" :model="addPeople" label-position="left" label-width="70px" style="width: 400px;">
        <el-form-item label="姓名" prop="linkName">
          <el-input v-model="addPeople.linkName" placeholder="请输入姓名" maxlength="11" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="addPeople.mobile" placeholder="请输入手机号" maxlength="11" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" :loading="loadingbut" @click="handleCreateOperator()">{{ loadingbuttext }}</el-button>
        <el-button type="primary" @click="handlePreservOperator()">保存生成邀请码</el-button>
      </div>
    </el-dialog>
    <template>
      <el-backtop style="right: 40px; bottom: 40px;">
        <i class="el-icon-upload2" />
      </el-backtop>
    </template>
  </div>
</template>

<script>
import { getClubList, createOperator, preservOperator, updateStatus } from '@/api/club'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  data() {
    return {
      pageNum: 0,
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        index: 1,
        pageSize: 20,
        userOrganizeID: this.$store.getters.organizeID,
        userName: '',
        linkMan: '',
        bindMobile: '',
        clubStatus: '',
        startTime: '',
        endTime: ''
      },
      startTime: '',
      endTime: '',
      addPeople: {
        linkName: '',
        mobile: ''
      },
      updateTemp: {},
      updateTatusType: '',
      dialogVisible: false,
      dialogVisibleText: '',
      dialogFormVisible: false,
      dialogStatus: '',
      loadingbut: false,
      loadingbuttext: '保存',
      textMap: {
        update: '添加运营人员',
        create: 'Create',
        titleText: '系统提示'
      },
      rules: {
        linkName: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        mobile: [
          { required: true, message: '手机号码不能为空', trigger: 'blur' },
          {
            required: true,
            pattern: /^\d{11}|\d{13}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    organizeID() {
      return this.$store.getters.organizeID
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getClubList(this.listQuery).then(response => {
        const data = response.data
        this.list = data.results
        this.listLoading = false
        this.total = data.totalRecord
        this.pageNum = data.index
      }).catch(() => {
        // 封装静态数据
      })
    },
    handleEdit(row) { // 添加运营
      console.log(row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.addPeople = Object.assign({}, { clubID: row.clubID, userID: row.userID })
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleCreateOperator() { // 保存
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const Formobj = { userOrganizeID: this.$store.getters.organizeID, configFlag: 1 }
          const params = Object.assign(Formobj, this.addPeople)
          this.loadingbut = true
          this.loadingbuttext = '保存中...'
          createOperator(params).then(response => {
            this.$message({ message: response.msg, type: 'success', center: true })
            this.dialogFormVisible = false
            this.loadingbut = false
            this.loadingbuttext = '保存'
          }).catch(() => {
            this.loadingbut = false
            this.loadingbuttext = '保存'
          })
        }
      })
    },
    handlePreservOperator() { // 保存并生成邀请码
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const Formobj = { userOrganizeID: this.$store.getters.organizeID, configFlag: 2 }
          const params = Object.assign(Formobj, this.addPeople)
          preservOperator(params).then(response => {
            this.dialogFormVisible = false
            this.$message({ message: response.msg, type: 'success', center: true })
          })
        }
      })
    },
    handOnline(row) {
      this.updateTemp = Object.assign({}, { clubID: row.clubID, userID: row.userID, clubStatus: row.clubStatus, userOrganizeID: this.$store.getters.organizeID })
      this.$confirm('确定上线该机构吗？', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = Object.assign({}, this.updateTemp)
        this.updateClubStatus(params)
      }).catch(() => {
      })
    },
    handOffline(row) {
      this.updateTemp = Object.assign({}, { clubID: row.clubID, userID: row.userID, clubStatus: row.clubStatus, userOrganizeID: this.$store.getters.organizeID })
      this.$confirm('确定下线该机构吗？', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = Object.assign({}, this.updateTemp)
        this.updateClubStatus(params)
      }).catch(() => {
      })
    },
    updateClubStatus(params) {
      updateStatus(params).then(response => {
        if (response.code === '1') {
          this.$message({ message: response.msg, type: 'success', center: true })
          this.list = []
          this.fetchData()
        } else {
          this.$message.error(response.msg)
        }
      })
    },
    handleFilter() {
      this.fetchData()
    }
  }
}

</script>
<style scoped>
  .el-dialog{
    width: 600px;
  }
</style>
