<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"
          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 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>
        <el-button type="primary" icon="el-icon-plus" @click="handleProcureEdit(1)">添加采购员</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="name" />
      <el-table-column label="手机号" align="center" prop="mobile" width="150" />
      <el-table-column label="关联机构" align="center" prop="clubNames">
        <template slot-scope="{row}">
          <p v-for="(club,index) in row.clubNames" :key="index" style="margin: 2px 0;">{{ club }}</p>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="150">
        <template slot-scope="{row}">
          <el-tag v-if="row.status*1===90" type="success" size="small">已上线</el-tag>
          <el-tag v-else type="danger" size="small">已下线</el-tag>
          <el-button v-if="row.status*1 === 90" type="primary" size="mini" @click="handOffline(row)">下线</el-button>
          <el-button v-else type="primary" size="mini" @click="handOnline(row)">上线</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="添加时间" prop="addTime">
        <template slot-scope="{row}">
          <span>{{ row.addTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleProcureEdit(2,row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>10" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
    <template>
      <el-backtop style="right: 40px; bottom: 40px;">
        <i class="el-icon-upload2" />
      </el-backtop>
    </template>
  </div>
</template>

<script>
import { findProcureList, setStatus, clubAudit } from '@/api/club'
import pickerOptions from '@/utils/time-picker.js'
export default {
  data() {
    return {
      registTime: '',
      pickerOptions,
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        mobile: '',
        organizeId: this.$store.getters.organizeId,
        name: '',
        status: '',
        startTime: '',
        endTime: ''
      },
      auditPeople: {
        auditStatus: '',
        auditNote: '',
        userOrganizeId: this.$store.getters.organizeId,
        clubId: '',
        userId: '',
        clubs: []
      },
      updateTemp: {},
      updateTatusType: '',
      dialogVisible: false,
      dialogVisibleText: '',
      dialogFormVisible: true,
      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 findProcureList(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) { // 添加运营

    },
    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.$confirm('确定上线该采购员吗？上线后采购员可以使用微信直接登录小程序', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = { serviceProviderId: row.serviceProviderId, status: 90 }
        this.updateClubStatus(params)
      }).catch(() => {
      })
    },
    handOffline(row) {
      this.$confirm('确定下线该采购员吗？下线后采购员将不能登录小程序。', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = { serviceProviderId: row.serviceProviderId, status: 91 }
        this.updateClubStatus(params)
      }).catch(() => {
      })
    },
    async updateClubStatus(params) {
      try {
        const res = await setStatus(params)
        this.$message.success(res.msg)
        this.getList()
      } catch (error) {
        this.$message.error(error.msg)
      }
    },
    handleProcureEdit(type, row) {
      switch (type) {
        case 1:
          this.$router.push({ path: '/procure/edit', query: { type: 'add' }})
          break
        case 2:
          this.$router.push({ path: '/procure/edit', query: { type: 'edit', id: row.serviceProviderId }})
          break
      }
    }
  }
}

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