<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-control">
        <span style="width: auto;">机构名称:</span>
        <el-input v-model="listQuery.userName" placeholder="请输入机构名称" clearable @keyup.enter.native="getList" @clear="getList" />
      </div>
      <div class="filter-control">
        <span style="width: auto;">联系人:</span>
        <el-input v-model="listQuery.linkMan" style="width:130px;" placeholder="请输入联系人姓名" clearable @keyup.enter.native="getList" @clear="getList" />
      </div>
      <div class="filter-control">
        <span style="width: auto;">手机号:</span>
        <el-input v-model="listQuery.bindMobile" placeholder="请输入手机号" clearable @keyup.enter.native="getList" @clear="getList" />
      </div>
      <div class="filter-control">
        <span style="width: auto;">状态:</span>
        <el-select v-model="listQuery.clubStatus" style="width:120px;" clearable @change="getList">
          <el-option value="" label="全部" />
          <el-option label="待审核" :value="1" />
          <el-option label="已上线" :value="90" />
          <el-option label="已上线" :value="91" />
          <el-option label="审核未通过" :value="92" />
        </el-select>
      </div>
      <div class="filter-control">
        <span>注册时间:</span>
        <el-date-picker
          v-model="registTime"
          type="daterange"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          @change="getList"
        />
      </div>
      <div class="filter-control">
        <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
      </div>
    </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 label="机构名称" align="center" prop="userName" />
      <el-table-column label="联系人" align="center" prop="linkMan" />
      <el-table-column label="手机号" align="center" prop="bindMobile" />
      <el-table-column align="center" label="机构地址" prop="address">
        <template slot-scope="{row}">
          <span>
            {{ row.province }}
            {{ row.city }}
            {{ row.town ? row.town : '' }}
            {{ row.address ? row.address :'' }}
          </span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="采购员" align="center" prop="linkMan" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{row}">
          <template v-if="row.clubStatus*1===1">
            <el-tag type="warning" size="small">待审核</el-tag>
          </template>
          <template v-else-if="row.clubStatus*1===92">
            <el-tag type="danger" size="small">审核未通过</el-tag>
          </template>
          <template v-else>
            <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*1 === 91" type="primary" size="mini" @click="handOnline(row)">上线</el-button>
            <el-button v-else type="primary" size="mini" @click="handOffline(row)">下线</el-button>
          </template>
        </template>
      </el-table-column>
      <el-table-column align="center" label="注册时间" prop="registerTime" width="100">
        <template slot-scope="{row}">
          <span>{{ row.registerTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="checkMan" />
      <el-table-column label="审核时间" align="center" prop="auditTime" width="100" />
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleRecordDetail(1,row)">查看资料</el-button>
          <!-- <el-button type="primary" size="mini" @click="handleRecordDetail(2,row)">查看运营人员</el-button> -->
          <el-button v-if="row.clubStatus*1 === 1" type="warning" size="mini" @click="hanleVerify(row)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>10" :total="total" :page.sync="listQuery.index" :limit.sync="listQuery.pageSize" @pagination="getList" />
    <el-dialog title="审核" :visible.sync="dialogFormVisible" width="440px">
      <el-form ref="dataForm" :rules="rules" :model="auditPeople" label-position="right" label-width="120px">
        <el-form-item label="状态:" prop="auditStatus">
          <el-select v-model="auditPeople.auditStatus" placeholder="请选择">
            <el-option label="请选择" value="" />
            <el-option label="审核通过" :value="90" />
            <el-option label="审核不通过" :value="92" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item v-if="auditPeople.auditStatus===90" label="机构类型:" prop="organizeType">
          <el-select v-model="auditPeople.organizeType" placeholder="请选择">
            <el-option label="请选择" value="" />
            <el-option label="内部机构" :value="0" />
            <el-option label="外部机构" :value="1" />
          </el-select>
        </el-form-item> -->
        <el-form-item v-if="auditPeople.auditStatus===92" label="原因:" prop="auditNote">
          <el-input v-model="auditPeople.auditNote" type="textarea" placeholder="请填写审核不通过原因,50字以内" maxlength="50" :autosize="{ minRows: 5, maxRows: 5}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreateOperator">确定</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, updateStatus, clubAudit } from '@/api/club'
import pickerOptions from '@/utils/time-picker.js'
export default {
  data() {
    return {
      registTime: '',
      pickerOptions,
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        index: 1,
        pageSize: 10,
        userOrganizeId: this.$store.getters.organizeId,
        userName: '',
        linkMan: '',
        bindMobile: '',
        clubStatus: '',
        startTime: '',
        endTime: ''
      },
      startTime: '',
      endTime: '',
      auditPeople: {
        auditStatus: '',
        auditNote: '',
        userOrganizeId: this.$store.getters.organizeId,
        clubId: '',
        userId: ''
      },
      updateTemp: {},
      updateTatusType: '',
      dialogVisible: false,
      dialogVisibleText: '',
      dialogFormVisible: false,
      dialogStatus: '',
      loadingbut: false,
      rules: {
        // organizeType: [{ required: true, message: '请选择机构类型', trigger: 'blur' }],
        auditStatus: [{ required: true, message: '请选择审核状态', trigger: 'blur' }],
        auditNote: [{ required: true, message: '请填写原因', trigger: 'blur' }]
      }
    }
  },
  computed: {
    disabled() {
      return !this.addPeople.status
    },
    organizeId() {
      return this.$store.getters.organizeId
    }
  },
  created() {
    this.getList()
  },
  methods: {
    initTime() {
      // 初始化获取时间筛选值
      if (this.registTime && this.registTime.length > 0) {
        this.listQuery.startTime = this.registTime[0]
        this.listQuery.endTime = this.registTime[1]
      } else {
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
    },
    async getList() {
      this.listLoading = true
      try {
        this.initTime()
        const res = await getClubList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.listLoading = false
      } catch (error) {
        console.log('error', error)
        this.listLoading = false
      }
    },
    hanleVerify(row) {
      // 审核
      this.auditPeople.userId = row.userId
      this.auditPeople.clubId = row.clubId
      this.dialogFormVisible = true
    },
    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) {
          this.clubAudit(this.auditPeople)
        } else {
          return false
        }
      })
    },
    // 审核
    async clubAudit(params) {
      try {
        await clubAudit(params)
        this.$message.success('操作成功')
        this.getList()
        this.dialogFormVisible = false
      } catch (error) {
        console.log('error', error)
      }
    },
    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(() => {
      })
    },
    async updateClubStatus(params) {
      try {
        const res = await updateStatus(params)
        this.$message.success(res.msg)
        this.getList()
      } catch (error) {
        this.$message.error(error.msg)
      }
    },
    handleRecordDetail(type, row) {
      const map = {
        1: '/club/details',
        2: '/club/operateList'
      }
      this.$router.push({ path: map[type], query: { clubId: row.clubId, userId: row.userId, name: row.userName }})
    }
  }
}

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