<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="8">
            <el-form-item label="客户名称" prop="custName">
              <el-input
                v-model="searchForm.custName"
                placeholder="请输入客户名称"
                style="width:200px"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="button">
        <el-button type="primary" @click="handleAdd">新增接触记录</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="custName" min-width="120" />
        <el-table-column label="客户内码" prop="custIdn" min-width="120" />
        <el-table-column label="柜员名称" prop="nickName" min-width="160" />
        <el-table-column label="更新时间" prop="updateTime" min-width="160" />
        <el-table-column label="操作" prop="edit" width="120" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleViewDetail(scope.row)"
            >查看</el-button>
            <el-button
              type="text"
              size="small"
              :disabled="!scope.row.editFlag"
              @click="handleEdit(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" />
    <add-record ref="addRef" @update="getData" />
  </div>
</template>

<script>
import { taskList } from '@/api/task/custContactRecord.js'
import SearchDetail from './components/searchDetail.vue'
import AddRecord from './components/addRecord.vue'
import { downloadFiles } from '@/utils'
export default {
  components: {
    SearchDetail,
    AddRecord
  },
  data() {
    return {
      searchForm: {},
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      const params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        ...this.searchForm
      }

      taskList(params).then((res) => {
        if (res.code !== 200) return
        this.tableData = res.data.list || []
        this.total = res.data.total
      })
    },
    
    handleAdd() {
      this.$refs.addRef.openDrawer()
    },

    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)
    },
    handleEdit() {
      this.$refs.addRef.openDrawer(row)
    }
  }
}
</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>
