<template>
  <div class="app-container page-refund-addr">
    <!-- 搜索相关区域 -->
    <div class="search-bar">
      <!-- native modifier has been removed, please confirm whether the function has been affected  -->
      <el-form
        :inline="true"
        :model="searchParam"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item label="收货人：">
            <el-input
              v-model="searchParam.consignee"
              placeholder="收货人"
              maxlength="30"
              clearable
            />
          </el-form-item>
          <el-form-item label="手机号码：">
            <el-input
              v-model="searchParam.mobile"
              placeholder="手机号码"
              maxlength="11"
              clearable
            />
          </el-form-item>
          <el-form-item label="是否默认：">
            <el-select
              v-model="searchParam.isDefault"
              clearable
              placeholder="是否为默认地址"
            >
              <el-option
                label="否"
                value="0"
              />
              <el-option
                label="是"
                value="1"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="getPage(true)"
            >
              {{ $t('table.search') }}
            </el-button>
            <el-button
              @click="clearSearchInfo()"
            >
              {{ $t("shop.resetMap") }}
            </el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <!-- 列表相关区域 -->
    <div class="main-content">
      <div class="operation-bar">
        <el-button
          v-permission="['shop:shopRefundAddr:save']"
          type="primary"
          class="filter-item"
          @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
            :label="$t('multishop.shopRefundAddr.consignee')"
            prop="consignee"
            align="left"
            width="150"
          >
            <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="left"
            width="150"
          >
            <template #default="{row}">
              <span class="table-cell-text">{{ row.mobile || '-' }}</span>
            </template>
          </el-table-column>
          <!-- 省市区地址 -->
          <el-table-column
            :label="$t('multishop.shopRefundAddr.addr')"
            prop="province"
            align="left"
            width="250"
          >
            <template #default="{row}">
              <span class="table-cell-text">{{ row.province + ' ' + row.city + ' ' + row.area + ' ' + row.addr }}</span>
            </template>
          </el-table-column>
          <!-- 邮编 -->
          <el-table-column
            :label="$t('multishop.shopRefundAddr.postCode')"
            prop="postCode"
            align="center"
            width="150"
          >
            <template #default="{row}">
              <span class="table-cell-text">{{ row.postCode || '-' }}</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="150"
            class-name="small-padding fixed-width"
          >
            <template #default="{row}">
              <div class="table-btn-con">
                <el-button
                  v-permission="['shop:shopRefundAddr:update']"
                  type="primary"
                  link
                  @click="onAddOrUpdate(row.shopRefundAddrId)"
                >
                  {{ $t('table.edit') }}
                </el-button>
                <el-button
                  v-permission="['shop:shopRefundAddr:delete']"
                  type="primary"
                  link
                  @click="onDelete(row.shopRefundAddrId, row)"
                >
                  {{ $t('table.delete') }}
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 分页条 -->
    <pagination
      v-show="pageVO.total>0"
      v-model:current-page="pageQuery.pageNum"
      v-model:page-size="pageQuery.pageSize"
      :total="pageVO.total"
      @pagination="getPage()"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="getPage()"
    />
  </div>
</template>

<script setup>
import Pagination from '@/components/Pagination/index.vue'
import AddOrUpdate from './components/add-or-update.vue'
import * as api from '@/api/multishop/shop-refund-addr'
import { ElMessage, ElMessageBox } from 'element-plus'

let tempSearchForm = ''
// 查询的参数
const pageQuery = reactive({
  pageSize: 10,
  pageNum: 1
})
// 返回参数
const pageVO = reactive({
  list: [], // 返回的列表
  total: 0, // 一共多少条数据
  pages: 0 // 一共多少页
})
// loading
const pageLoading = ref(true)
// 查询参数
const searchParam = reactive({
  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('默认地址无法删除')
    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()
    })
  })
}

// 清空搜索内容
const clearSearchInfo = () => {
  searchParam.mobile = null
  searchParam.consignee = null
  searchParam.isDefault = null
  pageQuery.pageNum = 1
  getPage()
}

</script>
