<!--
 * @Descripttion: (售后单记录表/tb_order_refund)
 * @version: (1.0)
 * @Author: (lwh)
 * @Date: (2023-08-29)
 * @LastEditors: (lwh)
 * @LastEditTime: (2023-08-29)
-->
<template>
  <div class="app-container">
    <el-row :gutter="24">
      <!-- 搜索框 queryParams.需要搜索的字段 -->
      <el-form :model="queryParams" label-position="left" style="margin:15px;"  inline ref="queryForm" v-show="showSearch"
        @submit.prevent>
      <el-form-item label="售后类型(1退货退款 2退货(无需退款) 3换货)" prop="refundType">
        <el-select v-model="queryParams.refundType" placeholder="请选择售后类型(1退货退款 2退货(无需退款) 3换货)" clearable @change="handleQuery">
          <el-option v-for="item in  refund_type " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="客户退货原因" prop="refundReason">
        <el-select v-model="queryParams.refundReason" placeholder="请选择客户退货原因" clearable @change="handleQuery">
          <el-option v-for="item in  refund_reason " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品收货状态(1未收到货 2已收到货)" prop="goodsReceiveStatus">
        <el-select v-model="queryParams.goodsReceiveStatus" placeholder="请选择商品收货状态(1未收到货 2已收到货)" clearable @change="handleQuery">
          <el-option v-for="item in  goods_receive_status " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商家审核状态(1待审核 2已同意 3已拒绝)" prop="merchantAuditStatus">
        <el-select v-model="queryParams.merchantAuditStatus" placeholder="请选择商家审核状态(1待审核 2已同意 3已拒绝)" clearable @change="handleQuery">
          <el-option v-for="item in  merchant_audit_status " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户是否发货(1未发货 2已发货)" prop="isCustomerSend">
        <el-select v-model="queryParams.isCustomerSend" placeholder="请选择用户是否发货(1未发货 2已发货)" clearable @change="handleQuery">
          <el-option v-for="item in  is_customer_send " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="售后单状态(1进行中 2已拒绝 3已完成 4已取消)" prop="orderRefundStatus">
        <el-select v-model="queryParams.orderRefundStatus" placeholder="请选择售后单状态(1进行中 2已拒绝 3已完成 4已取消)" clearable @change="handleQuery">
          <el-option v-for="item in  order_refund_status " :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
      </el-form>
    </el-row>

    <!-- 工具按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['business:orderrefund:addOrUpdate']" plain icon="plus" @click="AddDialogVisible = true">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['business:orderrefund:delete']" plain icon="delete" @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="download" @click="handleExport" v-hasPermi="['business:orderrefund:export']">
          {{ $t('btn.export') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格渲染  prop="对应的字段"-->
    <el-table v-loading="loading" :data="dataList" ref="tableRef" border highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
	  
      <el-table-column prop="refundType" label="售后类型(1退货退款 2退货(无需退款) 3换货)" align="center">
        <template #default="scope">
          <dict-tag :options=" refund_type " :value="scope.row.refundType" />
        </template>
      </el-table-column>
      <el-table-column prop="refundReason" label="客户退货原因" align="center">
        <template #default="scope">
          <dict-tag :options=" refund_reason " :value="scope.row.refundReason" />
        </template>
      </el-table-column>
      <el-table-column prop="customerRefundDesc" label="客户退款说明" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="customerRefundImg" label="客户退款凭证" align="center">
        <template #default="scope">
          <el-image preview-teleported :hide-on-click-modal="true" lazy class="table-td-thumb" fit="contain" :src="scope.row.customerRefundImg?.split(',')[0]" :preview-src-list="scope.row.customerRefundImg?.split(',')">
            <div><el-icon :size="15"><document /></el-icon></div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="goodsReceiveStatus" label="商品收货状态(1未收到货 2已收到货)" align="center">
        <template #default="scope">
          <dict-tag :options=" goods_receive_status " :value="scope.row.goodsReceiveStatus" />
        </template>
      </el-table-column>
      <el-table-column prop="merchantAuditStatus" label="商家审核状态(1待审核 2已同意 3已拒绝)" align="center">
        <template #default="scope">
          <dict-tag :options=" merchant_audit_status " :value="scope.row.merchantAuditStatus" />
        </template>
      </el-table-column>
      <el-table-column prop="merchantRefuseDesc" label="商家拒绝原因(说明)" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="refundMoney" label="实际退款金额" align="center" />
      <el-table-column prop="isCustomerSend" label="用户是否发货(1未发货 2已发货)" align="center">
        <template #default="scope">
          <dict-tag :options=" is_customer_send " :value="scope.row.isCustomerSend" />
        </template>
      </el-table-column>
      <el-table-column prop="customerSendTime" label="客户发货时间" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="expressNo" label="客户发货物流单号" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="orderRefundStatus" label="售后单状态(1进行中 2已拒绝 3已完成 4已取消)" align="center">
        <template #default="scope">
          <dict-tag :options=" order_refund_status " :value="scope.row.orderRefundStatus" />
        </template>
      </el-table-column>

      <el-table-column label="操作" width="350" fixed="right">
        <template #default="scope">
          <el-button type="primary" size="small" icon="edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:orderrefund:addOrUpdate']">编辑</el-button>
          <el-button type="danger" size="small" icon="delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:orderrefund:delete']">删除</el-button>
          <el-button size="small" icon="view" @click="handleDetail(scope.row)">查看</el-button>
        </template>
      </el-table-column>

    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>
  
  <!-- 添加 -->
  <AddDialog v-model="AddDialogVisible" :done="() => resetQuery()"></AddDialog>
  <!-- 编辑 -->
  <EditDialog v-model="EditDialogVisible" :data="EditDialogRow" :done="() => resetQuery()"></EditDialog>
  <!-- 详情 -->
  <DetailDialog v-model="DetailDialogVisible" :data="DetailDialogRow" :done="() => resetQuery()"></DetailDialog>
</template>
<script setup name="orderrefund">
import { ElMessageBox } from 'element-plus'
import modal from '@/plugins/modal.js'
import {  exportOrderRefund,    orderRefundList , delOrderRefund } from '@/api/business/OrderManage/OrderRefunds/orderRefund.js'
import AddDialog from "./components/AddDialog.vue";
import EditDialog from "./components/EditDialog.vue";
import DetailDialog from "./components/DetailDialog.vue";

const AddDialogVisible = ref(false);
const EditDialogVisible = ref(false);
const EditDialogRow = ref({});
const DetailDialogVisible = ref(false);
const DetailDialogRow = ref({});

const { proxy } = getCurrentInstance()

// 选中categoryId数组数组
const ids = ref([])
// 非单选禁用
const single = ref(true)
// 非多个禁用
const multiple = ref(true)
// 显示搜索条件
const showSearch = ref(true)
// 数据列表
const dataList = ref([])
// 总记录数
const total = ref(0)
// 是否加载
const loading = ref(true)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
})
const { queryParams } = toRefs(data)

// 业务参数


// 业务方法

// 字典获取
const refund_type = ref([]);
async function getrefund_type() {
  await proxy.getDicts('refund_type').then((res) => {
    refund_type.value = res.data
  })
}
getrefund_type()
// 字典获取
const refund_reason = ref([]);
async function getrefund_reason() {
  await proxy.getDicts('refund_reason').then((res) => {
    refund_reason.value = res.data
  })
}
getrefund_reason()
// 字典获取
const goods_receive_status = ref([]);
async function getgoods_receive_status() {
  await proxy.getDicts('goods_receive_status').then((res) => {
    goods_receive_status.value = res.data
  })
}
getgoods_receive_status()
// 字典获取
const merchant_audit_status = ref([]);
async function getmerchant_audit_status() {
  await proxy.getDicts('merchant_audit_status').then((res) => {
    merchant_audit_status.value = res.data
  })
}
getmerchant_audit_status()
// 字典获取
const is_customer_send = ref([]);
async function getis_customer_send() {
  await proxy.getDicts('is_customer_send').then((res) => {
    is_customer_send.value = res.data
  })
}
getis_customer_send()
// 字典获取
const order_refund_status = ref([]);
async function getorder_refund_status() {
  await proxy.getDicts('order_refund_status').then((res) => {
    order_refund_status.value = res.data
  })
}
getorder_refund_status()




//基础方法

// 查询数据
function getList() {
  loading.value = true

                                                                                                                                    
  orderRefundList(queryParams.value).then((res) => {
    if (res.code == 200) {
      loading.value = false;
      dataList.value = res.data.result;
      total.value = res.data.totalNum;
    }
  });
}


// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.orderRefundId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 重置查询操作 */
function resetQuery() {
  proxy.resetForm('queryForm')
  handleQuery()
}
/** 搜索按钮操作 */
function handleQuery() {
  getList()
}
/** 删除按钮操作 */
function handleDelete(row) {
  const Ids = row.orderRefundId || ids.value

    ElMessageBox.confirm("是否确认删除？", "系统提示", {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: "warning",
    })
    .then(function () {
      return delOrderRefund(Ids)
    })
    .then(() => {
      handleQuery()
      modal.msgSuccess("删除成功")
    })
    .catch(() => { })
}

/** 导出按钮操作 */
function handleExport(row) {
  const Ids = row.orderRefundId || ids.value
  const name = ref("所有")

  if (Ids.length != 0) {
    let str = ''
    for (const key in Ids) {
      str += Ids[key] + ','
    }
    str = str.slice(0, str.length - 1)
    queryParams.value.ids = str
    name.value = "选中"
  }

  ElMessageBox.confirm('是否确认导出' + name.value + '数据?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(function () {
      return exportOrderRefund(queryParams.value)
    })
    .then((response) => {
      proxy.download(response.data.path)
    })
}


// 修改
function handleUpdate(row) {
  EditDialogVisible.value = true
  EditDialogRow.value = row
}

// 详情
function handleDetail(row) {
  DetailDialogVisible.value = true
  DetailDialogRow.value = row
}

handleQuery()
</script>
