<template>
  <div class="main">
    <div class="form-box">
      <el-form
        ref="searchForm"
        :model="searchForm"
        label-position="left"
        label-width="80px"
      >
        <el-row :gutter="36">
          <el-col :span="6">
            <el-form-item label="日期" prop="startTimeMonth">
              <el-date-picker
                v-model="searchForm.startTimeMonth"
                type="month"
                value-format="yyyy-MM"
                placeholder="请选择日期"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="走访类型" prop="visitType">
              <el-select
                v-model="searchForm.visitType"
                placeholder="请选择走访类型"
                clearable
              >
                <el-option
                  v-for="(item, index) in visitTypeList"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="客户名称" prop="custName">
              <el-input
                v-model="searchForm.custName"
                placeholder="请输入客户名称"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="任务名称" prop="campaignName">
              <el-input
                v-model="searchForm.campaignName"
                placeholder="请输入任务名称"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="button">
        <el-button type="primary" @click="handleExport">导出</el-button>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleResetSearch">重置</el-button>
      </div>
    </div>

    <div class="table-box">
      <el-table
        ref="tableRef"
        :data="tableData"
        style="widht: 100%"
        ighlight-current-row
      >
        <el-table-column label="任务名称" prop="campaignName" min-width="160" />
        <el-table-column label="客户名称" prop="custName" min-width="120" />
        <el-table-column label="走访人" prop="userName" min-width="120" />
        <el-table-column label="签到时间" prop="signInTime" min-width="160" />
        <el-table-column label="签退时间" prop="signOutTime" min-width="160" />
        <el-table-column label="持续时间" prop="duration" min-width="160" />
        <el-table-column label="操作" prop="edit" width="80" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleViewDetail(scope.row)"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handlepageChange"
      />
    </div>

    <search-detail ref="detailRef" />
  </div>
</template>

<script>
import { getVisitRecord, exportTaskList } from '@/api/task/taskList.js'
import SearchDetail from './components/searchDetail.vue'
import { downloadFiles } from '@/utils'
import { Message } from 'element-ui'
export default {
  components: {
    SearchDetail
  },
  data() {
    return {
      searchForm: {},
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      visitTypeList: [
        { value: '企业走访', label: '企业走访' },
        { value: '商户走访', label: '商户走访' },
        { value: '个人走访', label: '个人走访' },
        { value: '收单商户走访', label: '收单商户走访' },
        { value: '域外商会走访', label: '域外商会走访' },
        { value: '催收走访', label: '催收走访' },
        { value: '其他走访', label: '其他走访' }
      ]
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      const params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        ...this.searchForm
      }

      getVisitRecord(params).then((res) => {
        this.tableData = res.rows || []
        this.total = res.total
      })
    },
    
    handleExport() {
      exportTaskList(this.searchForm).then((res) => {
        if (res.code === 200) {
          Message.success(res.msg)
        }
        // downloadFiles(res, `走访记录_${new Date().getTime()}.xlsx`)
      })
    },

    handleSizeChange(newSize) {
      this.pageSize = newSize
      this.getData()
    },

    handlepageChange(newPage) {
      this.currentPage = newPage
      this.getData()
    },

    handleSearch() {
      this.currentPage = 1
      this.getData()
    },

    handleResetSearch() {
      this.searchForm = {}
      this.handleSearch()
    },

    handleViewDetail(row) {
      this.$refs.detailRef.openDrawer(row.recordId)
    }
  }
}
</script>

<style lang="scss" scoped>
.main {
  padding: 30px 16px;
  .form-box {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
  }

  .control-button {
    margin-bottom: 16px;
  }

  .table-box {
    .el-pagination {
      float: right;
      margin-top: 8px;
    }
  }
}
</style>
