<template>
  <a-modal
    centered
    class="chooseAddressList-modal"
    :footer="null"
    :maskClosable="false"
    title="收货地址"
    v-model="isShow"
    @cancel="isShow=false"
    :width="960">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 操作按钮 -->
      <div class="table-operator">
        <a-button id="chooseAddressList-add" type="primary" class="button-error" @click="handleEdit()">新增</a-button>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: 500 }"
        :showPagination="false"
        :defaultLoadData="false"
        bordered>
        <!-- 收货人 -->
        <template slot="consignee" slot-scope="text, record">
          <div class="item-box">
            <a-tooltip placement="top">
              <template slot="title">
                <span>{{ record.consigneeName }}</span>
              </template>
              <p class="ellipsis-txt">{{ record.consigneeName }}</p>
            </a-tooltip>
            <span v-if="record.defaultFlag==1" class="badge-txt">默认</span>
          </div>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            type="primary"
            class="button-success"
            v-if="record.defaultFlag!=1"
            @click="handleDefault(record)"
            id="chooseAddressList-default-btn">设为默认</a-button>
          <a-button
            size="small"
            type="primary"
            class="button-info"
            v-if="record.readOnlyFlag!=1"
            @click="handleEdit(record)"
            id="chooseAddressList-edit-btn"
          >编辑</a-button>
          <a-button
            size="small"
            type="primary"
            class="button-error"
            v-if="record.readOnlyFlag!=1"
            @click="handleDel(record)"
            id="chooseAddressList-del-btn"
          >删除</a-button>
          <span v-if="!(record.defaultFlag!=1) && !(record.readOnlyFlag!=1)">--</span>
        </template>
        <!-- 选择 -->
        <template slot="choose" slot-scope="text, record">
          <a-button
            size="small"
            type="primary"
            class="button-primary"
            @click="handleChoose(record)"
            id="chooseAddressList-choose-btn">选择</a-button>
        </template>
      </s-table>
    </a-spin>
    <!-- 新增/编辑地址 -->
    <edit-address-modal ref="addrModal" :openModal="openAddrModal" @ok="handleOk" @close="openAddrModal=false" />
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import editAddressModal from './editAddressModal'
import { shippingAddressQuery, shippingAddressSet, shippingAddressDel } from '@/api/shippingAddress.js'
export default {
  name: 'ChooseAddressModal',
  components: { STable, VSelect, editAddressModal },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      queryParam: {},
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '收货人', scopedSlots: { customRender: 'consignee' }, width: '24%', align: 'center', ellipsis: true },
        { title: '联系电话', dataIndex: 'consigneeTel', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '收货地址', dataIndex: 'address', width: '25%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '24%', align: 'center' },
        { title: '选择', scopedSlots: { customRender: 'choose' }, width: '10%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return shippingAddressQuery(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = 0
            for (var i = 0; i < data.length; i++) {
              data[i].no = no + i + 1
              const provinceName = data[i].provinceName ? (data[i].provinceName + '-') : ''
              const cityName = data[i].cityName ? (data[i].cityName + '-') : ''
              const countyName = data[i].countyName ? (data[i].countyName + '-') : ''
              const addr = data[i].addr || ''
              data[i].address = provinceName + cityName + countyName + addr
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      openAddrModal: false // 选择地址弹框是否显示
    }
  },
  methods: {
    //  设为默认
    handleDefault (row) {
      this.spinning = true
      shippingAddressSet({ id: row.id }).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
          this.$refs.table.refresh(true)
          this.spinning = false
        } else {
          this.spinning = false
        }
      })
    },
    //  新增/编辑收货地址
    handleEdit (row) {
      if (row) {
        this.$refs.addrModal.setData({
          id: row.id,
          consigneeName: row.consigneeName,
          consigneeTel: row.consigneeTel,
          provinceSn: row.provinceSn,
          provinceName: row.provinceName,
          citySn: row.citySn,
          cityName: row.cityName,
          countySn: row.countySn,
          countyName: row.countyName,
          addr: row.addr
        })
      }
      this.openAddrModal = true
    },
    //  选择
    handleChoose (row) {
      this.$emit('ok', row)
    },
    //  删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '删除后不可恢复，确定要进行删除吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          shippingAddressDel({
            id: row.id
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('verify', row.id)
              _this.$refs.table.refresh(true)
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    //  地址保存成功
    handleOk (data) {
      this.$refs.table.refresh(true)
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.table.clearTable()
      } else {
        const _this = this
        setTimeout(() => {
          _this.$refs.table.refresh(true)
        }, 300)
      }
    }
  }
}
</script>

<style lang="less">
  .chooseAddressList-modal{
    .table-operator{
      margin-bottom: 10px;
    }
    .item-box{
      position: relative;
      display: flex;
      align-items: center;
      .ellipsis-txt{
        margin: 0;
        width: 100%;
        overflow: hidden;
        text-overflow:ellipsis;
        whitewhite-space: nowrap;
      }
      .badge-txt{
        line-height: 20px;
        padding: 0 7px;
        font-size: 12px;
        background-color: #fff;
        color: #ed1c24;
        border-color: #ed1c24;
        border-radius: 20px;
        box-shadow: 0 0 0 1px #d9d9d9 inset;
      }
    }
  }
</style>
