<template>
  <div class="contentx">
    <div class="desct" style="display: flex; justify-content: space-between; align-items: center;">
      <span>回访记录</span>
      <el-button type="primary" size="small" @click="handleAdd">新增回访记录</el-button>
    </div>

    <el-table v-loading="loading" :data="visitList" border>
      <el-table-column label="回访时间" align="center" prop="visitTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.visitTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="回访方式" align="center" prop="visitType" width="100" />
      <el-table-column label="回访结果" align="center" prop="visitResult" width="100" />
      <el-table-column label="回访内容" align="center" prop="visitContent" min-width="200" show-overflow-tooltip />
      <el-table-column label="下次回访时间" align="center" prop="nextVisitTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nextVisitTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提醒状态" align="center" width="90">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.reminded === 1" type="info" size="small">已提醒</el-tag>
          <el-tag v-else type="warning" size="small">待提醒</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            :disabled="scope.row.reminded === 1"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            style="color: #F56C6C"
            @click="handleDelete(scope.row)"
            :disabled="scope.row.reminded === 1"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增/修改弹窗 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="550px"
      append-to-body
      @close="resetForm"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="回访时间" prop="visitTime">
          <el-date-picker
            v-model="form.visitTime"
            type="datetime"
            placeholder="选择回访时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="回访方式" prop="visitType">
          <el-select v-model="form.visitType" placeholder="请选择回访方式" style="width: 100%">
            <el-option label="电话" value="电话" />
            <el-option label="微信" value="微信" />
            <el-option label="面访" value="面访" />
          </el-select>
        </el-form-item>
        <el-form-item label="回访结果" prop="visitResult">
          <el-select v-model="form.visitResult" placeholder="请选择回访结果" style="width: 100%">
            <el-option label="已联系" value="已联系" />
            <el-option label="未接通" value="未接通" />
            <el-option label="待跟进" value="待跟进" />
          </el-select>
        </el-form-item>
        <el-form-item label="回访内容" prop="visitContent">
          <el-input
            v-model="form.visitContent"
            type="textarea"
            :rows="4"
            placeholder="请输入回访内容/备注"
          />
        </el-form-item>
        <el-form-item label="下次回访时间" prop="nextVisitTime">
          <el-date-picker
            v-model="form.nextVisitTime"
            type="datetime"
            placeholder="选择下次回访时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="submitLoading" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCustomerVisit, getCustomerVisit, addCustomerVisit, updateCustomerVisit, delCustomerVisit } from '@/api/store/customerVisit'

export default {
  name: 'CustomerVisitSection',
  data() {
    return {
      loading: false,
      submitLoading: false,
      visitList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null
      },
      dialogVisible: false,
      dialogTitle: '',
      form: {
        id: null,
        userId: null,
        visitTime: null,
        visitType: '',
        visitResult: '',
        visitContent: '',
        nextVisitTime: null
      },
      rules: {
        visitTime: [{ required: true, message: '请选择回访时间', trigger: 'change' }],
        visitType: [{ required: true, message: '请选择回访方式', trigger: 'change' }],
        visitResult: [{ required: true, message: '请选择回访结果', trigger: 'change' }],
        nextVisitTime: [{ required: true, message: '请选择下次回访时间', trigger: 'change' }]
      },
      isEdit: false
    }
  },
  methods: {
    init(userId) {
      this.queryParams.userId = userId
      this.queryParams.pageNum = 1
      this.getList()
    },
    getList() {
      this.loading = true
      listCustomerVisit(this.queryParams).then(response => {
        this.visitList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleAdd() {
      this.isEdit = false
      this.dialogTitle = '新增回访记录'
      this.resetForm()
      this.form.userId = this.queryParams.userId
      this.dialogVisible = true
    },
    handleUpdate(row) {
      this.isEdit = true
      this.dialogTitle = '修改回访记录'
      this.resetForm()
      this.form = { ...row }
      this.dialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('确认删除该回访记录？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delCustomerVisit(row.id).then(res => {
          this.msgSuccess('删除成功')
          this.getList()
        })
      })
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (!valid) return
        this.submitLoading = true
        const api = this.isEdit ? updateCustomerVisit : addCustomerVisit
        api(this.form).then(res => {
          if (res.code === 200) {
            this.msgSuccess(this.isEdit ? '修改成功' : '新增成功')
            this.dialogVisible = false
            this.getList()
          } else {
            this.msgError(res.msg || '操作失败')
          }
        }).finally(() => {
          this.submitLoading = false
        })
      })
    },
    resetForm() {
      this.form = {
        id: null,
        userId: this.queryParams.userId,
        visitTime: null,
        visitType: '',
        visitResult: '',
        visitContent: '',
        nextVisitTime: null
      }
      if (this.$refs.form) {
        this.$refs.form.clearValidate()
      }
    }
  }
}
</script>

<style scoped>
.contentx {
  background-color: #fff;
  padding: 0px 20px 20px;
  margin: 20px;
}
.desct {
  padding-top: 20px;
  padding-bottom: 20px;
  color: #524b4a;
  font-weight: bold;
}
</style>
