<template>
  <a-modal
    centered
    class="customerManagementDetail-modal"
    :footer="null"
    :maskClosable="false"
    title="客户详情"
    v-model="isShow"
    @cancel="isShow=false"
    :width="960">
    <!-- 客户详情 -->
    <div>
      <a-spin :spinning="!detailData" tip="Loading...">
        <a-descriptions :column="3" bordered size="small">
          <a-descriptions-item label="客户名称：">{{ detailData&&detailData.customerName || '--' }}</a-descriptions-item>
          <a-descriptions-item label="联系人：">{{ detailData&&detailData.contactName || '--' }}</a-descriptions-item>
          <a-descriptions-item label="联系电话：">{{ detailData&&detailData.contactTel || '--' }}</a-descriptions-item>
          <a-descriptions-item label="联系手机：">{{ detailData&&detailData.contactMobile || '--' }}</a-descriptions-item>
          <a-descriptions-item label="客户地址：" :span="3">{{ addressStr }}</a-descriptions-item>
          <a-descriptions-item label="客户传真：">{{ detailData&&detailData.fax || '--' }}</a-descriptions-item>
          <a-descriptions-item label="配送方式：">{{ detailData&&detailData.dispatchTypeDictValue || '--' }}</a-descriptions-item>
          <a-descriptions-item label="是否为下级：">{{ detailData&&detailData.dealerFlagDictValue || '--' }}</a-descriptions-item>
          <a-descriptions-item label="价格类型：">{{ detailData&&detailData.priceTypeDictValue || '--' }}</a-descriptions-item>
          <a-descriptions-item label="收款方式：">{{ detailData&&detailData.settleStyleSnDictValue || '--' }}</a-descriptions-item>
          <a-descriptions-item label="客户类型：">{{ detailData&&detailData.customerTypeName || '--' }}</a-descriptions-item>
        </a-descriptions>
        <div>
          <div style="padding:10px 0;border-bottom: 1px solid #eee;margin: 10px 0;">被合并的客户记录</div>
          <div>
            <s-table
            class="sTable"
            ref="table"
            size="small"
            :rowKey="(record) => record.id"
            :columns="columns"
            :data="loadData"
            :defaultLoadData="false"
            bordered>
            </s-table>
          </div>
        </div>
      </a-spin>
      <div class="btn-cont"><a-button id="customer-management-detail-modal-back" @click="isShow = false">返回列表</a-button></div>
    </div>
  </a-modal>
</template>

<script>
import { STable } from '@/components'
import { custFindById, custMergeLogList } from '@/api/customer'
export default {
  name: 'CustomerManagementDetailModal',
  components: { STable },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemId: {
      type: [Number, String],
      default: ''
    }
  },
  computed: {
    // 地址处理
    addressStr () {
      const provinceName = this.detailData && this.detailData.provinceName ? this.detailData.provinceName : ''
      const cityName = this.detailData && this.detailData.cityName ? this.detailData.cityName : ''
      const countyName = this.detailData && this.detailData.countyName ? this.detailData.countyName : ''
      const customerAddr = this.detailData && this.detailData.customerAddr ? this.detailData.customerAddr : ''
      const str = (provinceName || cityName || countyName || customerAddr) ? (provinceName + cityName + countyName + customerAddr) : '--'
      return str
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      detailData: null, //  网点数据
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '合并时间', dataIndex: 'createDate', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '被合并客户名称', dataIndex: 'customerNameSource', width: '40%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '合并后客户名称', dataIndex: 'customerNameTarget', width: '40%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return custMergeLogList(Object.assign(parameter, {customerSnMain:this.detailData.customerSn})).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
    }
  },
  methods: {
    //  获取详情
    getDetail () {
      custFindById({ id: this.itemId }).then(res => {
        if (res.status == 200) {
          this.detailData = res.data
          this.$refs.table.refresh()
        } else {
          this.detailData = null
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.detailData = null
        this.$emit('close')
      }
    },
    itemId (newValue, oldValue) {
      if (this.isShow && newValue) {
        this.getDetail()
      }
    }
  }
}
</script>

<style lang="less">
  .customerManagementDetail-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .ant-descriptions-item-label.ant-descriptions-item-colon{
      width: 145px;
    }
    .item-cont {
      margin-bottom: 10px;
      display: flex;
      .item-label {
        width: 115px;
        font-size: 14px;
        color: rgba(0, 0, 0);
        flex-shrink: 0;
      }
      .item-main {
        flex-grow: 1;
        word-break: break-all;
      }
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
