<template>
  <el-dialog
    append-to-body
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="80%"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-left-panel :span="6">
        <el-button icon="el-icon-plus" type="primary" @click="exportCsv">
          {{ __('导出') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="18">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-select
              v-model="queryForm.return_state_id"
              clearable
              :placeholder="__('请选择卖家处理状态')"
            >
              <el-option
                v-for="item in returnState"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.return_id"
              clearable
              :placeholder="__('请输入退单编号')"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.order_id"
              clearable
              :placeholder="__('请输入订单编号')"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
            <el-button
              icon="el-icon-check"
              type="primary"
              @click="ordersReturnConfirm"
            >
              {{ __('批量确认') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="35" />
      <el-table-column
        align="center"
        fixed="left"
        :label="__('退单确认')"
        prop="return_withdraw_confirm"
        show-overflow-tooltip
        width="80"
      >
        <template #default="{ row }">
          <el-button v-if="row.return_withdraw_confirm === 1" type="success">
            {{ __('已确认') }}
          </el-button>
          <el-button
            v-if="row.return_withdraw_confirm === 0"
            type="danger"
            @click="ordersReturnConfirm(row)"
          >
            {{ __('未确认') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        fixed="left"
        :label="__('退单号')"
        prop="return_id"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :formatter="formatType"
        :label="__('服务类型')"
        prop="service_type_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('订单编号')"
        prop="order_id"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('退款额度')"
        prop="return_refund_amount"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatFee"
        :label="__('是否退运费订单')"
        prop="return_is_shipping_fee"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('店铺编号')"
        prop="store_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('店铺名称')"
        prop="store_name"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('买家编号')"
        prop="buyer_user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('客户名称')"
        prop="buyer_user_nickname"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :formatter="formatAddTime"
        :label="__('添加时间')"
        prop="return_add_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('买家退货备注')"
        prop="return_buyer_message"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('收货人')"
        prop="return_addr_contacter"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('联系电话')"
        prop="return_tel"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('收货地址详情')"
        prop="return_addr"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('邮编')"
        prop="return_post_code"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('物流公司编号')"
        prop="express_id"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('物流单号')"
        prop="return_tracking_number"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('卖家处理状态')"
        prop="return_state_id"
        show-overflow-tooltip
        width="120"
      >
        <template #default="{ row }">{{ __(row.return_state_name) }}</template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatPaid"
        :label="__('退款完成')"
        prop="return_is_paid"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :formatter="formatFlag"
        :label="__('退货类型')"
        prop="return_flag"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatReturnType"
        :label="__('申请类型')"
        prop="return_type"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatItemState"
        :label="__('物流状态')"
        prop="return_item_state_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatStoreTime"
        :label="__('商家处理时间')"
        prop="return_store_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('商家备注')"
        prop="return_store_message"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('退还佣金')"
        prop="return_commision_fee"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatFinishTime"
        :label="__('退款完成时间')"
        prop="return_finish_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :formatter="formatChannel"
        :label="__('退款渠道')"
        prop="return_channel_code"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatReturnChannelFlag"
        :label="__('渠道是否退款')"
        prop="return_channel_flag"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('渠道退款时间')"
        prop="return_channel_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('渠道退款单号')"
        prop="return_channel_trans_id"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('交易号')"
        prop="deposit_trade_no"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('支付渠道')"
        prop="payment_channel_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('实付金额')"
        prop="trade_payment_amount"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import {
    exportFile,
    getList,
    doForceRefund,
    doUpdateReturns,
  } from '@/api/order/return'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'OrderReturn',
    components: {},
    data() {
      return {
        title: '退单列表',
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        multipleSelection: [],
        listLoading: true,
        dialogFormVisible: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
        serviceType: [
          {
            value: 1,
            label: this.__('换货'),
          },
          {
            value: 2,
            label: this.__('退货'),
          },
          {
            value: 3,
            label: this.__('维修'),
          },
        ],
        shippingFee: [
          {
            value: 0,
            label: this.__('否'),
          },
          {
            value: 1,
            label: this.__('是'),
          },
        ],
        returnState: [
          {
            value: 3105,
            label: this.__('退单审核'),
          },
          {
            value: 3110,
            label: this.__('收货确认'),
          },
          {
            value: 3115,
            label: this.__('退款确认'),
          },
          {
            value: 3120,
            label: this.__('收款确认'),
          },
          {
            value: 3125,
            label: this.__('完成'),
          },
          {
            value: 3130,
            label: this.__('已拒绝退款'),
          },
          {
            value: 3135,
            label: this.__('已取消'),
          },
        ],
        returnFlag: [
          {
            value: 0,
            label: this.__('不用退货'),
          },
          {
            value: 1,
            label: this.__('需要退货'),
          },
        ],
        returnType: [
          {
            value: 1,
            label: this.__('退款申请'),
          },
          {
            value: 2,
            label: this.__('退货申请'),
          },
          {
            value: 3,
            label: this.__('虚拟退款'),
          },
        ],
        itemState: [
          {
            value: 2030,
            label: this.__('待发货'),
          },
          {
            value: 2040,
            label: this.__('已发货/待收货确认'),
          },
          {
            value: 2060,
            label: this.__('已完成/已签收'),
          },
          {
            value: 2070,
            label: this.__('已取消/已作废'),
          },
        ],
        channelCode: [
          {
            value: 'money',
            label: this.__('余额'),
          },
          {
            value: 'alipay',
            label: this.__('支付宝'),
          },
          {
            value: 'wx_native',
            label: this.__('微信'),
          },
        ],
        channelFlag: [
          {
            value: 0,
            label: this.__('待退'),
          },
          {
            value: 1,
            label: this.__('已退'),
          },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      showReturnTable(row) {
        if (row.return_id) {
          this.queryForm.return_id = row.return_id
        }
        this.dialogFormVisible = true
        this.fetchData()
      },
      formatChannel(row) {
        const obj = this.channelCode.find(
          (item) => item.value === row.return_channel_code
        )
        if (obj) return obj.label
        return ''
      },
      formatReturnChannelFlag(row) {
        const obj = this.channelFlag.find(
          (item) => item.value === row.return_channel_flag
        )
        if (obj) return obj.label
        return ''
      },
      formatItemState(row) {
        const obj = this.itemState.find(
          (item) => item.value === row.return_item_state_id
        )
        if (obj) return obj.label
        return ''
      },
      formatReturnType(row) {
        const obj = this.returnType.find(
          (item) => item.value === row.return_type
        )
        if (obj) return obj.label
        return ''
      },
      formatFlag(row) {
        const obj = this.returnFlag.find(
          (item) => item.value === row.return_flag
        )
        if (obj) return obj.label
        return ''
      },
      formatPaid(row) {
        return row.return_is_paid ? this.__('已退') : this.__('未退')
      },
      formatState(row) {
        const obj = this.returnState.find(
          (item) => item.value === row.return_state_id
        )
        if (obj) return obj.label
        return ''
      },
      formatFee(row) {
        const obj = this.shippingFee.find(
          (item) => item.value === row.return_is_shipping_fee
        )
        if (obj) return obj.label
        return ''
      },
      formatType(row) {
        const obj = this.serviceType.find(
          (item) => item.value === row.service_type_id
        )
        if (obj) return obj.label
        return ''
      },
      formatFinishTime(row) {
        const finishTime = row.return_finish_time
        if (!finishTime) return ''
        return renderTime(finishTime)
      },
      formatStoreTime(row) {
        const storeTime = row.return_store_time
        if (!storeTime) return ''
        return renderTime(storeTime)
      },
      formatAddTime(row) {
        const addTime = row.return_add_time
        if (!addTime) return ''
        return renderTime(addTime)
      },
      formatTime(row) {
        return renderTime(row.return_channel_time)
      },
      __,
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
      exportCsv() {
        exportFile(this.queryForm).then((data) => {
          const blob = new Blob(['\uFEFF' + data])
          const downloadElement = document.createElement('a')
          const href = window.URL.createObjectURL(blob) //创建下载的链接
          downloadElement.href = href
          downloadElement.download = decodeURIComponent('售后数据.csv') //下载后文件名
          document.body.appendChild(downloadElement)
          downloadElement.click() //点击下载
          document.body.removeChild(downloadElement) //下载完成移除元素
          window.URL.revokeObjectURL(href) //释放掉blob对象
        })
      },
      ordersReturnConfirm(row) {
        if (row.return_id) {
          this.$baseConfirm(
            this.__('您要确认选中退款单吗？'),
            null,
            async () => {
              const { msg, status } = await doUpdateReturns({
                return_ids: row.return_id,
              })
              this.$baseMessage(msg, 'success', false, 'vab-hey-messa-success')
              await this.fetchData()
            }
          )
        } else {
          if (this.multipleSelection.length == 0) {
            this.$baseAlert('您未选中任何行！')
            return
          }
          const return_ids = this.multipleSelection
            .map((item) => item.return_id)
            .join()
          this.$baseConfirm(
            this.__('您要确认选中退款单吗？'),
            null,
            async () => {
              const { msg, status } = await doUpdateReturns({
                return_ids: return_ids,
              })
              this.$baseMessage(msg, 'success', false, 'vab-hey-msg-success')
              await this.fetchData()
            }
          )
        }
      },
      close() {
        this.dialogFormVisible = false
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      doForceRefund(return_id) {
        if (return_id) {
          this.$baseConfirm(
            this.__('退款操作不可逆，确认强制退款？'),
            null,
            async () => {
              const { msg, status } = await doForceRefund({
                return_id: return_id,
              })
              if (200 == status) {
                this.$baseMessage(msg, 'success')
              } else {
                this.$baseMessage(msg, 'error')
              }
              await this.fetchData()
            }
          )
        } else {
          this.$baseMessage(this.__('退单id异常！'), 'error')
        }
      },
    },
  }
</script>
