<template>
  <div class='app-container'>
    <el-form inline>
      <el-form-item label="当前状态：">
        <el-select v-model="data.form.pushStatus" placeholder="请选择当前状态" style="width: 200px" clearable>
          <el-option v-for="item in pushStatusOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="时间：">
        <el-date-picker v-model="data.time" type="datetimerange" start-placeholder="开始时间" end-placeholder="结束时间"
          value-format="YYYY-MM-DD HH:mm:ss" @change="handleChangeTime" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="getSearch">搜索</el-button>
        <el-button icon="Refresh" @click="getRefresh">重置</el-button>
        <el-button type="primary" icon="Upload" plain @click="getPush(data.idList)"
          :disabled="data.idList.length <= 0">批量推送</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="data.tableData" v-loading="loading" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" />
      <el-table-column label="交易号" prop="tradeId" align="center" />
      <el-table-column label="买家昵称" prop="buyerNick" align="center" />
      <el-table-column label="购买渠道" prop="shopNick" align="center" />
      <el-table-column label="收货人" prop="contact" align="center" />
      <el-table-column label="手机号" prop="mobile" align="center" />
      <el-table-column label="详细地址" prop="address" align="center">
        <template #default="scope">
          <span>{{ scope.row.address.replace(/\^/g, '') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品型号" prop="goodsName" align="center" />
      <el-table-column label="换新数量" prop="quantity" align="center" />
      <el-table-column label="购买凭证" align="center" width="100px">
        <template #default="scope">
          <el-image :src="scope.row.proofPurchase" />
        </template>
      </el-table-column>
      <el-table-column label="物流公司" prop="buyerExpressName" align="center" />
      <el-table-column label="快递单号" prop="buyerExpressNo" align="center" />
      <el-table-column label="当前状态" align="center" :formatter="statusFormatter" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button v-if="scope.row.pushStatus === 0" type="primary" link @click="getPush([scope.row.id])">推送</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="data.form.pageNum" v-model:limit="data.form.pageSize"
      @pagination="getList" />
  </div>
</template>

<script setup>
import { list, push } from "@/api/service/replace"
import { useStore, useCommon } from '@/hooks/index'

const { storeOptions, getStore } = useStore()
const { pushStatusOptions } = useCommon()

const { proxy } = getCurrentInstance()

const data = reactive({
  form: {
    pageNum: 1,
    pageSize: 10
  },
  idList: []
})

// 遮罩层
const loading = ref(false)
// 全数据
const total = ref(0)

// 列表
const getList = () => {
  loading.value = true
  list(data.form).then(res => {
    if (res.code === 0) {
      data.tableData = res.data.records
      total.value = res.data.total
      loading.value = false
    }
  })
}

// 时间
const handleChangeTime = (e) => {
  data.form.startTime = e[0]
  data.form.endTime = e[1]
}

// 搜索
const getSearch = () => {
  data.form.pageNum = 1
  getList()
}

// 重置
const getRefresh = () => {
  data.form = {
    pageNum: 1,
    pageSize: 10
  }
  getList()
}

// 多选
const handleSelectionChange = (e) => {
  data.idList = []
  e.map(i => data.idList.push(i.id))
}

// 推送
const getPush = (id) => {
  push({ idList: id }).then(res => {
    if (res.code === 0) {
      proxy.$modal.msgSuccess('推送成功！')
      getList()
    }
  })
}

// 字典翻译
function statusFormatter(row) {
  return proxy.selectDictLabel(pushStatusOptions.value, row.pushStatus)
}

getStore()
getList()
</script>