<template>
  <div class="app-container calendar-list-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input clearable class="filter-item" style="width: 210px;" placeholder="请输入访客姓名" v-model="listQuery.visitorName">
      </el-input>
      <el-input clearable class="filter-item" style="width: 210px;" placeholder="请输入访客联系方式" v-model="listQuery.visitorPhone">
      </el-input>
      <el-input clearable class="filter-item" style="width: 210px;" placeholder="请输入被访人姓名" v-model="listQuery.inviteName">
      </el-input>

      <el-date-picker clearable class="filter-item"
                      value-format="yyyy-MM-dd"
                      type="date"
                      placeholder="选择到访时间"
                      v-model="listQuery.visitorDatetime">
      </el-date-picker>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button class="filter-item" type="primary" :loading="downloadLoading" v-waves icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" stripe  border fit highlight-current-row>

      <el-table-column type="index" label="序号" header-align="center" align="center">
      </el-table-column>

      <el-table-column align="center" min-width="80px" label="访客头像" prop="visitorPhoto">
        <template slot-scope="scope">
          <img :src="scope.row.visitorPhoto" style="width: 50px;height: 50px;border-radius: 50%"/>
        </template>
      </el-table-column>

      <el-table-column align="center" min-width="80px" label="访客姓名" prop="visitorName">
      </el-table-column>

      <el-table-column align="center" min-width="80px" label="访客性别" prop="visitorGender">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.visitorGender == 1" type="success">女</el-tag>
          <el-tag v-else type="danger">男</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" min-width="100px" label="联系方式" prop="visitorPhone">
      </el-table-column>

      <el-table-column align="center" min-width="60px" label="人数" prop="visitorSum">
      </el-table-column>

      <el-table-column align="center" min-width="100px" label="访客机构" prop="visitorOrganization">
      </el-table-column>

      <el-table-column align="center" min-width="120px" label="到访时间" prop="visitorDatetime">
      </el-table-column>

      <el-table-column align="center" min-width="100px" label="事由" prop="visitorReason">
      </el-table-column>



      <el-table-column align="center" min-width="100px" label="被访人姓名" prop="inviteName">
      </el-table-column>

      <el-table-column align="center" min-width="120px" label="被访人机构" prop="inviteOrganization">
      </el-table-column>

      <el-table-column align="center" min-width="120px" label="状态" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 2" type="danger">邀请中</el-tag>
          <el-tag v-else-if="scope.row.status == 3" type="warning">待下发到终端</el-tag>
          <el-tag v-else-if="scope.row.status == 4" type="success">邀请成功</el-tag>
          <el-tag v-else type="info">默认</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == '3'" class="filter-item" type="primary" @click="handleVisitorUpload(scope.row)">下发终端</el-button>
          <!--<el-button type="danger" size="mini"  @click="handleVisitorDelete(scope.row)">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
                     :page-sizes="[10,20,30,50,500]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

  </div>
</template>

<script>
  import { fetchList, deleteVisitor, uploadVisitor } from '@/api/visitor'
  import waves from '@/directive/waves' // 水波纹指令

  export default {
    name: 'visitorList',
    directives: {
      waves
    },
    data() {
      return {
        list: null,
        allList: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 10,
          visitorName: undefined,
          visitorSum: undefined,
          visitorGender: undefined,
          visitorPhone: undefined,
          visitorDatetime: undefined,
          inviteName: undefined,
          status: undefined,
          sort: '+id'
        },
        dialogFormVisible: false,
        dialogStatus: '',
        downloadLoading: false,
        downloadLoading1: false
      }
    },
    created() {
      this.getList();
    },
    methods: {
      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
          this.list = response.data.data.items
          console.log(this.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()
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      handleVisitorDelete(row){
        this.$confirm('确认删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteVisitor(row).then(response => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
        }).catch(() => {

        })

      },
      handleVisitorUpload(row){
        this.$confirm('确认上传吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          uploadVisitor(row).then(response => {
            this.$notify({
              title: '成功',
              message: '上传成功',
              type: 'success',
              duration: 2000
            })
          })
        }).catch(() => {

        })
      },
      handleDownload() {
        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['访客姓名', '访客性别', '访客联系方式', '访客人数', '访问时间', '被访人姓名']
          const filterVal = ['visitorName','visitorGender', 'visitorPhone', 'visitorSum', 'visitorDatetime', 'inviteName']
          excel.export_json_to_excel2(tHeader, this.list, filterVal, '访客信息')
          this.downloadLoading = false
        })
      }
    }
  }
</script>
