<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
    append-to-body
  >
    <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="用户ID" prop="buyerUserId">
        {{ formData.buyerUserId || '--' }}
      </el-form-item>
      <el-form-item label="用户头像" prop="headImage">
        <el-image
          v-if="formData.headImage" :src="formData.headImage" style="width:80px;height:80px"
          fit="cover" :preview-src-list="[ formData.headImage ]"
        />
        <span v-else>--</span>
      </el-form-item>
      <el-form-item label="用户昵称" prop="name">
        {{ formData.name || '--' }}
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        {{ formData.phone || '--' }}
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <span v-if="formData.sex === 1">男</span>
        <span v-else-if="formData.sex === 2">女</span>
        <span v-else>未知</span>
      </el-form-item>
      <el-form-item label="注册时间" prop="createTime">
        {{ formData.createTime || '--' }}
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        {{ formData.birthday || '--' }}
      </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="labels">
        <span v-if="formData.labels && formData.labels.length">{{ formData.labels }}</span>
        <span v-else>--</span>
      </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="orders">
        {{ typeof formData.orders === 'number' ? formData.orders : '--' }}
      </el-form-item>
      <el-form-item label="支付成功数" prop="pays">
        {{ typeof formData.pays === 'number' ? formData.pays : '--' }}
      </el-form-item>
      <el-form-item label="售后次数" prop="afters">
        {{ typeof formData.afters === 'number' ? formData.afters : '--' }}
      </el-form-item>
      <el-form-item label="售后单数" prop="afterOrders">
        {{ typeof formData.afterOrders === 'number' ? formData.afterOrders : '--' }}
      </el-form-item>
      <div>
        <el-tabs v-model="activeName">
          <el-tab-pane label="TA的订单" name="first">
            <div>
              <!-- 查询和其他操作 -->
              <div class="filter-container">
                <el-input
                  v-model="formData.orderFormid" clearable size="mini" class="filter-item"
                  style="width: 200px;"
                  placeholder="请输入订单编号"
                />
                <el-button
                  size="mini" class="filter-item" type="primary" icon="el-icon-search"
                  style="margin-left:10px;"
                  @click="handleSearch"
                >
                  查找
                </el-button>
              </div>
              <!-- 表格 -->
              <div>
                <el-table
                  v-loading="!formData.orderList" height="60vh" element-loading-text="暂无订单数据"
                  :data="formData.orderList"
                  v-bind="{ stripe: true, size: 'small', border: true, fit: true, highlightCurrentRow: true }"
                >
                  <el-table-column align="center" width="100" label="订单编号" prop="orderFormid" fixed="left" />
                  <el-table-column align="center" width="150" label="店铺名称" prop="shopName" fixed="left" show-overflow-tooltip />
                  <el-table-column align="center" width="120" label="商品数量" prop="products" />
                  <el-table-column align="center" width="120" label="订单金额(元)" prop="price" />
                  <el-table-column align="center" label="状态" prop="state">
                    <template slot-scope="{ row }">
                      <el-tag v-if="row.state === 1" effect="plain">待付款</el-tag>
                      <el-tag v-else-if="row.state === 2" effect="plain">待发货</el-tag>
                      <el-tag v-else-if="row.state === 3" effect="plain">待收货</el-tag>
                      <el-tag v-else-if="row.state === 4" effect="plain">已完成</el-tag>
                      <el-tag v-else-if="row.state === 5" effect="plain">已关闭</el-tag>
                      <span v-else>--</span>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="TA的评论" name="second">
            <!-- 表格 -->
            <div>
              <el-table
                v-loading="!formData.comments" height="60vh" element-loading-text="暂无评论数据"
                :data="formData.comments"
                v-bind="{ stripe: true, size: 'small', border: true, fit: true, highlightCurrentRow: true }"
              >
                <el-table-column align="center" width="150" label="店铺名称" prop="shopName" fixed="left" show-overflow-tooltip />
                <el-table-column align="center" width="120" label="商品名称" prop="productName" />
                <el-table-column align="center" width="120" label="订单金额(元)" prop="price" />
                <el-table-column align="center" label="状态" prop="state">
                  <template slot-scope="{ row }">
                    <el-tag v-if="row.state === 1" effect="plain">待付款</el-tag>
                    <el-tag v-else-if="row.state === 2" effect="plain">待发货</el-tag>
                    <el-tag v-else-if="row.state === 3" effect="plain">待收货</el-tag>
                    <el-tag v-else-if="row.state === 4" effect="plain">已完成</el-tag>
                    <el-tag v-else-if="row.state === 5" effect="plain">已关闭</el-tag>
                    <span v-else>--</span>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="操作" width="120" fixed="right" class-name="small-padding fixed-width">
                  <template slot-scope="{ row }">
                    <el-button
                      v-if="formData.comments[row.commentId]" type="success" size="mini"
                      @click="$refs.CommentDetails && $refs.CommentDetails.handleOpen(row)"
                    >
                      查看
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-tab-pane>
          <el-tab-pane label="TA的收货地址" name="third">
            <!-- 表格 -->
            <div>
              <el-table
                v-loading="!formData.receives" height="60vh" element-loading-text="暂无订单数据"
                :data="formData.receives"
                v-bind="{ stripe: true, size: 'small', border: true, fit: true, highlightCurrentRow: true }"
              >
                <el-table-column align="center" width="100" label="姓名" prop="receiveName" fixed="left" />
                <el-table-column align="center" width="120" label="电话" prop="receivePhone" />
                <el-table-column align="center" label="地址" prop="receiveAdress">
                  <template slot-scope="{ row }">
                    <span>{{ row.receiveAdress }}{{ row.address }}</span>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>

    </el-form>

    <!-- 评论详情 -->
    <CommentDetails ref="CommentDetails" @success="getList" />
  </el-dialog>
</template>

<script>
import CommentDetails from './CommentDetails'
import { customerMageGetById } from '@/api/customerMage'

export default {
  name: 'DetailModal',
  components: {
    CommentDetails
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '800px',
        title: '查看用户详情'
      },
      visible: false,
      formData: {
        buyerUserId: '',
        orderFormid: '',
        headImage: '',
        name: '',
        phone: '',
        sex: '',
        createTime: '',
        birthday: '',
        labels: [],
        orders: '',
        pays: '',
        afters: '',
        afterOrders: '',
        orderList: [],
        comments: [],
        receives: []
      },
      activeName: 'first'
    }
  },
  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 customerMageGetById({ buyerUserId: id, orderFormid: this.formData.orderFormid })
        this.formData = Object.assign(this.$options.data().formData, res.data, {
          buyerUserId: res.data.buyerUserId || '',
          headImage: res.data.headImage || '',
          name: res.data.name || '',
          phone: res.data.phone || '',
          sex: res.data.sex || '',
          createTime: res.data.createTime || '',
          birthday: res.data.birthday || '',
          labels: res.data.labels || [],
          orders: res.data.orders,
          pays: res.data.pays,
          afters: res.data.afters,
          afterOrders: res.data.afterOrders,
          orderList: res.data.orderList || [],
          comments: res.data.comments || [],
          receives: res.data.receives || []
        })
      } finally {
        loading.close()
      }
    },
    async getList() {
      this.getInfo(this.formData.buyerUserId)
    },
    handleSearch() {
      this.getInfo(this.formData.buyerUserId)
    }
  }
}
</script>

<style lang="scss" scoped>
.filter-container {
	.filter-item {
		display: inline-block;
		vertical-align: middle;
		margin-bottom: 10px;
	}
}

.small-padding {
	.cell {
		padding-left: 5px;
		padding-right: 5px;
	}
}

.fixed-width {
	.el-button--mini {
		padding: 7px 10px;
	}
}
</style>

