<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
  >
    <el-form
      ref="formData"
      :model="formData"
      size="mini"
      label-position="left"
      label-suffix=":"
      label-width="200px"
    >
      <!-- 客户信息 -->
      <div style="display: flex;padding-bottom: 14px;font-size: 16px;font-weight: bold;">
        <div
          style="width: 4px;height: 14px;margin-left: 6px;margin-right: 6px;background-color: #0519D4;border-radius: 2px;"
        >
        </div>
        <div>基本信息</div>
      </div>
      <el-form-item label="客户手机号" prop="phone">
        {{ formData.phone || '--' }}
      </el-form-item>
      <el-form-item label="客户昵称" prop="name">
        {{ formData.name || '--' }}
      </el-form-item>
      <el-form-item label="成为客户时间" prop="time">
        {{ formData.time || '--' }}
      </el-form-item>
      <el-form-item label="来源方式" prop="source">
        {{ formData.source || '--' }}
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        {{ formData.sex || '--' }}
      </el-form-item>
      <el-form-item label="客户标签" prop="labelName">
        {{ formData.labelName || '--' }}
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        {{ formData.remark || '--' }}
      </el-form-item>

      <div style="display: flex;padding-bottom: 14px;font-size: 16px;font-weight: bold;">
        <div
          style="width: 4px;height: 14px;margin-left: 6px;margin-right: 6px;background-color: #0519D4;border-radius: 2px;"
        >
        </div>
        <div>交易信息</div>
      </div>
      <el-form-item label="累积消费金额（元）" prop="total">
        {{ formData.total || 0 }}
      </el-form-item>
      <el-form-item label="累计消费订单数" prop="frequency">
        {{ formData.frequency || 0 }}
      </el-form-item>
      <el-form-item label="最近下单时间" prop="lastTime">
        {{ formData.lastTime || '--' }}
      </el-form-item>
      <el-form-item label="累计退款金额" prop="refundMoney">
        {{ formData.refundMoney || 0 }}
      </el-form-item>
      <el-form-item label="累计退款订单数" prop="refunds">
        {{ formData.refunds || 0 }}
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getCustomerDetail } from '@/api/customer'

export default {
  name: 'DetailModal',
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '800px',
        title: '查看客户详情'
      },
      visible: false,
      formData: {
        buyerUserId: '',
        phone: '',
        name: '',
        time: '',
        source: '',
        sex: '',
        birthday: '',
        remark: '',
        total: '',
        frequency: '',
        lastTime: '',
        refundMoney: '',
        refunds: '',
        labelName: '',
        ids: []
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    handleOpen(params = {}) {
      this.formData = Object.assign(this.$options.data().formData, params)
      if (params.buyerUserId) {
        this.getInfo(params.buyerUserId)
      }
      this.visible = true
    },
    async getInfo(id) {
      const loading = this.$loading({ text: '加载中' })
      try {
        const res = await getCustomerDetail({ buyerUserId: id })
        this.formData = Object.assign(this.$options.data().formData, res.data, {
          buyerUserId: res.data.buyerUserId || '',
          phone: res.data.phone || '',
          name: res.data.name || '',
          time: res.data.time || '',
          source: res.data.source || '',
          sex: res.data.sex,
          birthday: res.data.birthday || '',
          remark: res.data.remark || '',
          total: res.data.total,
          frequency: res.data.frequency,
          lastTime: res.data.lastTime || '',
          refundMoney: res.data.refundMoney,
          refunds: res.data.refunds,
          labelName: res.data.labelName || '',
          ids: res.data.ids || []
        })
      } finally {
        loading.close()
      }
    }
  }
}
</script>

