<!--
 Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.

 https://www.mall4j.com/

 未经允许，不可做商业用途！

 版权所有，侵权必究！
-->
<template>
  <div class="Mall4j app-container">
    <!-- 列表相关区域 -->
    <div style="margin-bottom:20px">
      <el-button
        v-permission="['shop:shopRefundAddr:save']"
        type="primary"
        @click="onAddOrUpdate()"
      >
        {{ $t('table.create') }}
      </el-button>
    </div>
    <div class="table-con">
      <el-table
        v-loading="pageLoading"
        :data="pageVO.list"
        header-cell-class-name="table-header"
        row-class-name="table-row-low"
        style="width: 100%;"
      >
        <el-table-column
          min-width="80"
          type="index"
          :label="$t('table.id')"
          align="center"
        />
        <!-- 收货人 -->
        <el-table-column
          :label="$t('multishop.shopRefundAddr.consignee')"
          prop="consignee"
          align="center"
        >
          <template #default="{row}">
            <span class="table-cell-text">{{ row.consignee }}</span>
          </template>
        </el-table-column>

        <!-- 手机 -->
        <el-table-column
          :label="$t('multishop.shopRefundAddr.mobile')"
          prop="mobile"
          align="center"
        >
          <template #default="{row}">
            <span class="table-cell-text">{{ row.mobile }}</span>
          </template>
        </el-table-column>
        <!-- 邮编 -->
        <el-table-column
          :label="$t('multishop.shopRefundAddr.postCode')"
          prop="postCode"
          align="center"
        >
          <template #default="{row}">
            <span class="table-cell-text">{{ row.postCode ? row.postCode : '-' }}</span>
          </template>
        </el-table-column>
        <!-- 省 -->
        <el-table-column
          :label="$t('multishop.shopRefundAddr.province')"
          prop="province"
          align="center"
        />
        <!-- 市 -->
        <el-table-column
          :label="$t('multishop.shopRefundAddr.city')"
          prop="city"
          align="center"
        />
        <!-- 区 -->
        <el-table-column
          :label="$t('multishop.shopRefundAddr.area')"
          prop="area"
          align="center"
        />
        <!-- 省市区地址 -->
        <el-table-column
          :label="$t('multishop.shopRefundAddr.addr')"
          prop="addr"
          align="center"
        >
          <template #default="{row}">
            <span class="table-cell-text">{{ row.addr }}</span>
          </template>
        </el-table-column>
        <!-- 默认地址 1:是 0:不是 -->
        <el-table-column
          :label="$t('multishop.shopRefundAddr.isDefault')"
          prop="isDefault"
          align="center"
        >
          <template #default="{row}">
            <span>{{ row.isDefault === 1 ? '是':'否' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('table.actions')"
          align="center"
          width="200"
          class-name="small-padding fixed-width"
        >
          <template #default="{row}">
            <div class="table-btn-con">
              <el-button
                v-permission="['shop:shopRefundAddr:update']"
                link
                type="primary"
                @click="onAddOrUpdate(row.supplierRefundAddrId)"
              >
                {{ $t('table.edit') }}
              </el-button>
              <el-button
                v-permission="['shop:shopRefundAddr:delete']"
                link
                type="primary"
                @click="onDelete(row.supplierRefundAddrId, row)"
              >
                {{ $t('table.delete') }}
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页条 -->
    <pagination
      v-show="pageVO.total>0"
      v-model:page="pageQuery.pageNum"
      v-model:limit="pageQuery.pageSize"
      style="text-align: right"
      :total="pageVO.total"
      @pagination="getPage()"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="getPage()"
    />
  </div>
</template>

<script setup>
import AddOrUpdate from './components/add-or-update.vue'
import * as api from '@/api/multishop/shop-refund-addr'
import Pagination from '@/components/Pagination/index.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
let tempSearchForm = null
// 查询的参数
const pageQuery = {
  pageSize: 10,
  pageNum: 1
}
// 返回参数
const pageVO = {
  list: [], // 返回的列表
  total: 0, // 一共多少条数据
  pages: 0 // 一共多少页
}
// loading
const pageLoading = ref(true)
// 查询参数
const searchParam = {
  mobile: null,
  consignee: null,
  isDefault: null
}
const addOrUpdateVisible = ref(false)
onMounted(() => {
  getPage()
})
const getPage = (newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = { ...pageQuery, ...searchParam }
  } else {
    tempSearchForm.pageNum = pageQuery.pageNum
    tempSearchForm.pageSize = pageQuery.pageSize
  }
  pageLoading.value = true
  api.page(tempSearchForm).then(data => {
    Object.assign(pageVO, data)
    pageLoading.value = false
  }).catch(() => {
    pageLoading.value = false
  })
}

const addOrUpdateRef = ref('')
const onAddOrUpdate = (supplierRefundAddrId) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value.init(supplierRefundAddrId)
  })
}

const onDelete = (supplierRefundAddrId, row) => {
  if (row.isDefault) {
    ElMessage.error($t('multishop.shopRefundAddr.defaultAddError'))
    return
  }
  ElMessageBox.confirm($t('table.sureToDelete'), $t('table.tips'), {
    confirmButtonText: $t('table.confirm'),
    cancelButtonText: $t('table.cancel'),
    type: 'warning'
  }).then(() => deleteById(supplierRefundAddrId))
}

const deleteById = (supplierRefundAddrId) => {
  api.deleteById(supplierRefundAddrId).then(() => {
    ElMessage({
      message: $t('table.actionSuccess'),
      type: 'success',
      duration: 1500,
      onClose: () => getPage()
    })
  })
}
</script>
