<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="订单编号" prop="orderCode">
        <el-input v-model="queryParams.orderCode" placeholder="请输入订单编号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="退款类型" prop="refundType">
        <el-select v-model="queryParams.refundType" placeholder="请选择" clearable size="small">
          <el-option v-for="dict in refundTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="售后状态" prop="salesStatus">
        <el-select v-model="queryParams.salesStatus" placeholder="请选择" clearable size="small">
          <el-option v-for="dict in afterSalesStatusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="申请时间" prop="createTime">
        <el-date-picker v-model="dateRange" size="small" style="width: 220px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始" end-placeholder="结束" @change="changeDate" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['store:storeAfterSales:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table border v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="售后单号" align="center" prop="afterSalesCode" width="160" show-overflow-tooltip />
      <el-table-column label="订单编号" align="center" prop="orderCode" width="160" show-overflow-tooltip />
      <el-table-column label="所属公司" align="center" prop="companyName" width="140" show-overflow-tooltip />
      <el-table-column label="用户" align="center" prop="userName" width="100" />
      <el-table-column label="退款金额" align="center" prop="refundAmount" width="100">
        <template slot-scope="scope"><span>¥{{ scope.row.refundAmount }}</span></template>
      </el-table-column>
      <el-table-column label="退款类型" align="center" prop="refundType" width="90">
        <template slot-scope="scope"><dict-tag :options="refundTypeOptions" :value="scope.row.refundType" /></template>
      </el-table-column>
      <el-table-column label="售后状态" align="center" prop="salesStatus" width="90">
        <template slot-scope="scope"><dict-tag :options="afterSalesStatusOptions" :value="scope.row.salesStatus" /></template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="createTime" width="160" />
      <el-table-column label="处理时间" align="center" prop="updateTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)" v-hasPermi="['store:storeAfterSales:query']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['store:storeAfterSales:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { getRequest, delRequest } from "@/api/common";
export default {
  name: "StoreAfterSales",
  data() {
    return {
      loading: true, exportLoading: false, ids: [], single: true, multiple: true, showSearch: true, total: 0, dateRange: [], list: [],
      refundTypeOptions: [], afterSalesStatusOptions: [],
      queryParams: { pageNum: 1, pageSize: 10, orderCode: undefined, refundType: undefined, salesStatus: undefined, beginTime: undefined, endTime: undefined }
    };
  },
  created() {
    this.getDicts("refund_type").then(res => { this.refundTypeOptions = res.data; });
    this.getDicts("after_sales_status").then(res => { this.afterSalesStatusOptions = res.data; });
    this.getList();
  },
  methods: {
    getList() { this.loading = true; getRequest("/store/storeAfterSales/list", this.queryParams).then(res => { this.list = res.rows; this.total = res.total; this.loading = false; }); },
    changeDate(val) { this.queryParams.beginTime = val ? val[0] : undefined; this.queryParams.endTime = val ? val[1] : undefined; },
    handleQuery() { this.queryParams.pageNum = 1; this.getList(); },
    resetQuery() { this.dateRange = []; this.resetForm("queryForm"); this.handleQuery(); },
    handleSelectionChange(selection) { this.ids = selection.map(item => item.id); this.single = selection.length !== 1; this.multiple = !selection.length; },
    handleDelete(row) { const ids = row.id || this.ids; this.$modal.confirm('确认删除售后单吗？').then(() => delRequest("/store/storeAfterSales/" + ids)).then(() => { this.getList(); this.$modal.msgSuccess("删除成功"); }).catch(() => {}); },
    handleDetail(row) { this.$router.push({ path: "/storeAfterSalesDetail", query: { id: row.id } }); },
    handleExport() { this.$modal.confirm("确认导出所有售后数据吗？").then(() => { this.exportLoading = true; getRequest("/store/storeAfterSales/export", this.queryParams).then(res => { this.download(res.msg); this.exportLoading = false; }); }).catch(() => {}); }
  }
};
</script>
