<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="1300px"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-left-panel :span="16">
        <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.order_id"
              clearable
              :placeholder="__('请输入订单编号')"
            />
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="orderTimes"
              :end-placeholder="__('添加时间')"
              :range-separator="__('至')"
              :start-placeholder="__('添加时间')"
              type="daterange"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="8">
        <el-button icon="el-icon-plus" type="primary">
          {{ __('导出') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column
        align="center"
        :label="__('退单号')"
        prop="return_id"
        show-overflow-tooltip
        width="180"
      >
        <template #default="{ row }">
          <el-link @click="showTiem(row)">
            {{ row.return_id }}
          </el-link>
        </template>
      </el-table-column>
      <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="180"
      >
        <template #default="{ row }">
          <el-link @click="showOrderItemTable(row)">
            {{ row.order_id }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('退款额度')"
        prop="return_refund_amount"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('买家编号')"
        prop="buyer_user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('客户名称')"
        prop="userBase.user_nickname"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatAddTime"
        :label="__('添加时间')"
        prop="return_add_time"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('退款理由')"
        prop="orderReturnReason.return_reason_name"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('买家退货备注')"
        prop="return_buyer_message"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :formatter="formatState"
        :label="__('卖家处理状态')"
        prop="return_state_id"
        show-overflow-tooltip
        width="120"
      />
      <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="formatShipped"
        :label="__('卖家发货状态')"
        prop="shopOrderInfo.order_is_shipped"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :formatter="formatFinishTime"
        :label="__('退款完成时间')"
        prop="return_finish_time"
        show-overflow-tooltip
        width="180"
      />
      <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="180"
      />
      <el-table-column
        align="center"
        :label="__('渠道退款单号')"
        prop="return_channel_trans_id"
        show-overflow-tooltip
        width="180"
      />
      <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>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <item ref="item" />
    <Table ref="Table" />
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import Item from './OrderReturnItem'
  import Table from './OnlineOrderItem'
  import { getList } from '@/api/order/return'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'OrderReturnList',
    components: { Item, Table },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        orderTimes: [],
        list: [],
        dialogFormVisible: false,
        title: '',
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        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: 3100,
            label: this.__('提交退单'),
          },
          {
            value: 3105,
            label: this.__('退单审核'),
          },
          {
            value: 3110,
            label: this.__('收货确认'),
          },
          {
            value: 3115,
            label: this.__('退款确认'),
          },
          {
            value: 3120,
            label: this.__('收款确认'),
          },
          {
            value: 3125,
            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.__('微信'),
          },
        ],
        shipped: [
          {
            value: 3030,
            label: this.__('未发货'),
          },
          {
            value: 3031,
            label: this.__('已发货'),
          },
          {
            value: 3032,
            label: this.__('部分发货'),
          },
        ],
        channelFlag: [
          {
            value: 0,
            label: this.__('待退'),
          },
          {
            value: 1,
            label: this.__('已退'),
          },
        ],
      }
    },
    created() {},
    methods: {
      showOrderItemTable(row) {
        this.$refs['Table'].showEdit(row)
      },

      showTable(row) {
        this.queryForm.order_id = row.order_id
        this.dialogFormVisible = true
        this.fetchData()
      },
      formatShipped(row) {
        const obj = this.shipped.find((item) => {
          let shopOrderInfo = row.shopOrderInfo
          if (!shopOrderInfo) return false
          return item.value === row.shopOrderInfo.order_is_shipped
        })
        return obj ? obj.label : ''
      },
      showTiem(row) {
        this.$refs['item'].showEdit(row)
      },
      formatChannel(row) {
        const obj = this.channelCode.find(
          (item) => item.value === row.return_channel_code
        )
        return obj ? obj.label : ''
      },
      formatItemState(row) {
        const obj = this.itemState.find(
          (item) => item.value === row.return_item_state_id
        )
        return obj ? obj.label : ''
      },
      formatReturnType(row) {
        const obj = this.returnType.find(
          (item) => item.value === row.return_type
        )
        return obj ? obj.label : ''
      },
      formatFlag(row) {
        const obj = this.returnFlag.find(
          (item) => item.value === row.return_flag
        )
        return obj ? obj.label : ''
      },
      formatPaid(row) {
        return row.return_is_paid ? this.__('已退') : this.__('未退')
      },
      formatState(row) {
        const obj = this.returnState.find(
          (item) => item.value === row.return_state_id
        )
        return obj ? obj.label : ''
      },
      formatFee(row) {
        const obj = this.shippingFee.find(
          (item) => item.value === row.return_is_shipping_fee
        )
        return obj ? obj.label : ''
      },
      formatType(row) {
        const obj = this.serviceType.find(
          (item) => item.value === row.service_type_id
        )
        return obj ? obj.label : ''
      },
      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) {
        const return_channel_time = row.return_channel_time
        return return_channel_time ? renderTime(return_channel_time) : ''
      },
      formatReturnChannelFlag(row) {
        const obj = this.channelFlag.find(
          (item) => item.value === row.return_channel_flag
        )
        if (obj) return obj.label
        return ''
      },
      __,
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      close() {
        this.queryForm = {}
        this.dialogFormVisible = false
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        if (this.orderTimes && this.orderTimes.length > 0) {
          this.queryForm.startDate = renderTime(this.orderTimes[0])
          this.queryForm.endDate = renderTime(this.orderTimes[1])
        } else {
          this.queryForm.startDate = ''
          this.queryForm.endDate = ''
        }
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
