<template>
  <div>
    <!-- 收退货仓库信息-->
    <el-card class="box-card" v-if="refundInfo.refundType == '3'">
      <el-select
        @change="handelChangeReturnWarehouse"
        v-model="selectedWarehouseCode"
        style="width: 60%"
        placeholder="The warehouse to receive return goods"
        :disabled="action != 'cus_check'"
      >
        <el-option
          v-for="warehouse in wareHouses"
          :key="warehouse.returnWarehouseCode"
          :label="warehouse.returnWarehouseName"
          :value="warehouse.returnWarehouseCode"
        >
        </el-option>
      </el-select>
      <el-divider></el-divider>
      <div>
        AddressInfo: {{ returnWarehouseAddress
        }}<i
          style="color:blue;"
          class="el-icon-document-copy"
          :title="$t('title.copy')"
          :copy-value="returnWarehouseAddress"
          v-on:click="copy"
        ></i>
      </div>
    </el-card>
    <el-card
      class="box-card"
      v-if="refundInfo.status == 0"
      v-loading="btnLoading"
    >
    <!-- TODO 暂时关闭SL -->
      <el-form
        v-if="refundInfo.refundType == '3' &&
            this.isShopifyLabel == true && addressInfo.receiverCountryName == 'US'"
        label-width="120px"
      >
        <el-form-item label="ShippingLabel">
          <el-radio-group v-model="slUseAble">
            <el-radio label="0">{{ $t("view.notEnabled") }}</el-radio>
            <el-radio label="1">{{ $t("view.enabled") }}</el-radio>
          </el-radio-group>
          <el-tag
            type="danger"
            v-if="this.slUseAble === '1' && this.shippingLabelMessage"
            >{{ this.shippingLabelMessage }}</el-tag
          >
        </el-form-item>
      </el-form>
      <!-- 审核-->
      <el-input
        type="textarea"
        :placeholder="$t('place.customerServiceRemarks')"
        v-model="checkData.notes"
        maxlength="150"
        :disabled="action != 'cus_check'"
        show-word-limit
      ></el-input>
      <!-- <el-select
        :placeholder="$t('place.rejectionReason')"
        clearable
        style="width: 530px;margin-top:20px;"
        class="filter-item"
        :disabled="action != 'cus_check'"
        v-model="checkData.rejectReason"
      >
        <el-option
          v-for="item in refundReasons"
          :key="item.id"
          :label="item.reasonContentChinese"
          :value="item.id"
        >
        </el-option>
      </el-select> -->
      <div
        style="float:right;margin-top:20px;margin-bottom:20px;"
        :style="hideStyleBtn"
        v-if="action == 'cus_check'"
      >
        <el-button type="primary" size="mini" @click="handleKfCheck(1)">{{
          $t("view.approved")
        }}</el-button>
        <el-button type="primary" size="mini" @click="handleKfCheck(2)">{{
          $t("view.rejected")
        }}</el-button>
      </div>
    </el-card>
    <el-card
      class="box-card"
      v-if="refundInfo.status == 5"
      v-loading="btnLoading"
    >
      <!-- 审核-->
      <el-form
        v-if="action == 'f_check'"
        ref="cwForm"
        :model="refundInfo"
        label-width="80px"
      >
        <el-form-item :label="$t('label.refundMethod')">
          <el-radio-group v-model="refundInfo.refundMethod" >
            <el-radio label="system">{{ $t("view.systemRefund") }}</el-radio>
            <el-radio label="manual">{{ $t("view.manualRefund") }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item
          v-if="refundInfo.refundMethod == 'manual'"
          :label="$t('label.accountType')"
        >
          <el-radio-group
            v-model="refundInfo.refundAccountType"
          >
            <el-radio label="PayPal">PayPal</el-radio>
            <el-radio label="bankCard">银行卡</el-radio>
            <el-radio label="Gift Card">Gift Card</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item
          v-if="
            refundInfo.refundMethod == 'manual' &&
              refundInfo.refundAccountType == 'PayPal'
          "
          :label="$t('label.refundAccount')"
        >
          <el-input
            v-model="refundInfo.refundAccount"
            :placeholder="$t('place.pleaseEnterRefundAccount')"
          >
          </el-input>
        </el-form-item>

        <!-- <el-form-item
          v-if="refundInfo.refundMethod == 'system'"
          :label="$t('label.rejectionOfReturns')"
        >
          <el-radio-group v-model="refundInfo.rejectedGoods">
            <el-radio label="false">{{ $t("label.no") }}</el-radio>
            <el-radio label="true">{{ $t("label.yes") }}</el-radio>
          </el-radio-group>
        </el-form-item> -->

        <el-form-item
          v-if="
            refundInfo.refundMethod == 'system' &&
              refundInfo.rejectedGoods == 'true'
          "
          :label="$t('label.rejectionReason')"
        >
          <el-select
            :placeholder="$t('place.rejectionReason')"
            clearable
            style="width: 200px;"
            class="filter-item"
            :disabled="action != 'f_check'"
            v-model="refundInfo.rejectReason"
          >
            <el-option
              v-for="item in refundReasons"
              :key="item.id"
              :label="item.reasonContentChinese"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-input
        v-if="action == 'f_check'"
        type="textarea"
        :placeholder="$t('place.financeAuditRemarks')"
        v-model="checkData.cwnotes"
        maxlength="150"
        :disabled="btndisableed"
        show-word-limit
      ></el-input>
      <div
        style="float:right;margin-top:20px;margin-bottom:20px;"
        :style="hideStyleBtn"
        v-if="action == 'f_check'"
      >
        <el-button
          type="success"
          size="mini"
          v-if="
            refundInfo.channelCode == 'SHOPIFY' &&
              refundInfo.refundMethod == 'system'
          "
          @click="handleCalculatesCheck"
          style="margin-right:20px;"
          >{{ $t("view.verification") }}</el-button
        >
        <el-button
          type="primary"
          size="mini"
          :disabled="
            refundInfo.channelCode == 'SHOPIFY' &&
              btndisabled &&
              refundInfo.refundMethod == 'system'
          "
          @click="handleCheck(6)"
          >{{ $t("view.approved") }}</el-button
        >
        <el-button type="primary" size="mini" @click="handleCheck(0)">{{
          $t("view.rejected")
        }}</el-button>
      </div>
    </el-card>
    <el-card class="box-card">
      <!-- 基本信息-->
      <el-descriptions
        :title="$t('title.basicInformation')"
        :colon="false"
        border
      >
        <el-descriptions-item :label="$t('label.refundOrderNumber')"
          >{{ refundInfo.refundNo }}
          <i
            style="color:blue;"
            class="el-icon-document-copy"
            :title="$t('title.copy')"
            :copy-value="refundInfo.refundNo"
            v-on:click="copy"
          ></i
        ></el-descriptions-item>
        <el-descriptions-item :label="$t('label.totalRefundAmount')">{{
          refundInfo.refundAmount
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('label.SLFee')" span="1">
          {{ refundInfo.slFee || '0.00'}}
        </el-descriptions-item>
        <el-descriptions-item :label="$t('label.onlineShopOrder')"
          >{{ refundInfo.ormorder }}
          <i
            style="color:blue;"
            class="el-icon-document-copy"
            :title="$t('title.copy')"
            :copy-value="refundInfo.ormorder"
            v-on:click="copy"
          ></i
        ></el-descriptions-item>
        <!--        添加shopify单号-->
        <el-descriptions-item label="实际退款金额">{{
          refundInfo.actualRefundAmount
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('label.shopifyNumber')" span="2"
          >{{ refundInfo.cutUser }}
          <i
            style="color:blue;"
            class="el-icon-document-copy"
            :title="$t('title.copy')"
            :copy-value="refundInfo.cutUser"
            v-on:click="copy"
          ></i
        ></el-descriptions-item>
        <el-descriptions-item :label="$t('label.afterSalesType')">{{
          refundInfo.refundType | refundTypeFilter(that)
        }}</el-descriptions-item>

        <!-- <el-descriptions-item label="退款原因(中文)" span="2">{{
          refundInfo.reasonContentChinese
        }}</el-descriptions-item>

        <el-descriptions-item label="退款原因(English)" span="1">{{
            refundInfo.reasonContentEnglish
          }}</el-descriptions-item>

        <el-descriptions-item
          v-if="refundInfo.otherReason"
          label="其他原因说明"
          span="3">{{
            refundInfo.otherReason
        }}</el-descriptions-item> -->
        <el-descriptions-item
          :label="$t('label.receivingReturnWarehouse')"
          span="1"
          v-if="refundInfo.warehouseName"
        >
          {{ refundInfo.warehouseName }}
        </el-descriptions-item>
        <el-descriptions-item
          :label="$t('label.warehouseReceivingStatus')"
          span="1"
          v-if="refundInfo.warehouseName"
        >
          {{ refundInfo.warehouseReceivingStatus }}
        </el-descriptions-item>
        <el-descriptions-item
          :label="$t('label.returnLogisticsCompany')"
          v-if="refundInfo.shipmodeId"
          >{{ refundInfo.shipmodeId }}</el-descriptions-item
        >
        <el-descriptions-item
          :label="$t('label.returnExpressNumber')"
          v-if="refundInfo.shippingNo"
          >{{ refundInfo.shippingNo
          }}<i
            style="color:blue;"
            v-if="refundInfo.shippingNo"
            class="el-icon-document-copy"
            :title="$t('title.copy')"
            :copy-value="refundInfo.shippingNo"
            v-on:click="copy"
          ></i
        ></el-descriptions-item>
        <el-descriptions-item
          :label="$t('label.customerServiceNote')"
          span="3"
          >{{ refundInfo.notes }}</el-descriptions-item
        >
        <el-descriptions-item :label="$t('label.buyersNote')" span="1">{{
          refundInfo.buyNotes
        }}</el-descriptions-item>
        <el-descriptions-item
          :label="$t('label.customerReturnCountry')"
          span="2"
          >{{ addressInfo.receiverCountryName }}</el-descriptions-item
        >

        <el-descriptions-item :label="$t('label.refundMethod')" span="1">
          {{ refundInfo.refundMethod | refundMethodFilter(that) }}
        </el-descriptions-item>
        <el-descriptions-item :label="$t('label.accountType')" span="2">
          {{ refundInfo.refundAccountType | refundAccountTypeFilter(that) }}
        </el-descriptions-item>
        <el-descriptions-item :label="$t('label.refundAccount')" span="2">
          {{ refundInfo.refundAccount }}
        </el-descriptions-item>
        <el-descriptions-item :label="$t('label.isSelfPickupOrder')" span="1">
          <span v-if="refundInfo.isSelfPickupOrder === false">{{ $t("no") }}</span>
          <span v-else>{{ $t("yes") }}</span>
        </el-descriptions-item>
        <!-- TODO 暂时关闭SL -->
         <el-descriptions-item :label="$t('label.openShippingLabel')" span="1">
            {{ refundInfo.slUseable | SlstatusFilter }}
        </el-descriptions-item>
        <el-descriptions-item :label="$t('label.useShippingLabel')" span="2">
          {{ refundInfo.slFlag | SlstatusFilter }}
        </el-descriptions-item>
        <el-descriptions-item :label="$t('label.shippingRefundAmount')" span="1">
          {{refundInfo.refundPostageAmount}}
        </el-descriptions-item>
        <el-descriptions-item :label="$t('label.sumGiftCardRefundAmount')" span="1">
          {{refundInfo.sumGiftCardRefundAmount}}
        </el-descriptions-item>
        <el-descriptions-item :label="$t('label.sumAiGiftCardRefundAmount')" span="1">
          {{refundInfo.sumAiGiftCardRefundAmount}}
        </el-descriptions-item>
      </el-descriptions>
    </el-card>

    <!--
    <el-divider><i class="el-icon-magic-stick"></i></el-divider>
-->

    <el-card class="box-card">
      <el-descriptions
        :title="$t('title.returnProducts') + '-' + (index + 1)"
        v-for="(item, index) in orderItemInfo"
        :key="index"
        :colon="false"
        :style="titleColor(item)"
        border
      >
        <el-descriptions-item :label="$t('label.isNoHeadFlag')" span="1">
          <span v-if="item.isHeadFlag == 1">
            是
          </span>
          <span v-if="item.isHeadFlag == 0">
            否
          </span>
        </el-descriptions-item>
        <el-descriptions-item  :label="$t('label.noHeadFlag')" span="2">
          <span v-if="item.isHeadFlag == 1 ">
            {{item.ordersIdS}}
          </span>
        </el-descriptions-item>
        <el-descriptions-item :label="$t('label.uniqueCode')" span="3">{{
            item.uniqueCode
          }}</el-descriptions-item>
        <el-descriptions-item :label="$t('label.skuCode')"
          >{{ item.skuCode }}
          <i
            style="color:blue;"
            class="el-icon-document-copy"
            :title="$t('title.copy')"
            :copy-value="item.skuCode"
            v-on:click="copy"
          ></i
        ></el-descriptions-item>
        <el-descriptions-item :label="$t('label.shopifyNumber')" span="2"
          >{{ item.orderOnId }}
          <i
            style="color:blue;"
            class="el-icon-document-copy"
            :title="$t('title.copy')"
            :copy-value="item.orderOnId"
            v-on:click="copy"
          ></i
        ></el-descriptions-item>
        <el-descriptions-item :label="$t('label.orderStatus')"
          >{{ item.orderStatus | statusFilterStatus }}
        </el-descriptions-item>
        <el-descriptions-item :label="$t('label.orderItemStatus')" span="2"
          >{{ item.itemStatus | statusFilterStatus }}
        </el-descriptions-item>

        <el-descriptions-item :label="$t('label.exchangeGoods')" span="1">
          {{ item.exchangeFlag === "1" ? $t("label.yes") : $t("label.no") }}
        </el-descriptions-item>
        <el-descriptions-item :label="$t('label.originalSKU')" span="2">
          {{ item.oldSku }}
        </el-descriptions-item>

        <el-descriptions-item :label="$t('label.productName')" span="2">{{
          item.productName
        }}</el-descriptions-item>
        <el-descriptions-item
          :label="$t('label.quantity')"
          labelStyle="color: red;"
          contentStyle="color: red;"
          v-if="item.quantity > 1"
          >{{ item.quantity }}
        </el-descriptions-item>

        <el-descriptions-item
          :label="$t('label.quantity')"
          v-if="item.quantity <= 1"
          >{{ item.quantity }}
        </el-descriptions-item>

        <el-descriptions-item :label="$t('label.refundSubtotal')">{{
          item.refundAmount
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('label.salePrice')">{{
          item.price
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('label.paymentAmount')"
          >{{ item.totalFee }}
        </el-descriptions-item>
        <el-descriptions-item :label="$t('label.discountAmount')">{{
          item.discountFee
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('label.sellerDiscount')">{{
            item.sellerDiscount
          }}</el-descriptions-item>
        <el-descriptions-item :label="$t('label.platformDiscount')">{{
            item.platformDiscount
          }}</el-descriptions-item>
        <el-descriptions-item :label="$t('label.loanAmount')" span="2">{{
            item.loanAmount
          }}</el-descriptions-item>
        <el-descriptions-item :label="$t('label.status')"
          >{{ item.returnStatus | statusFiltr2 }}
        </el-descriptions-item>
        <el-descriptions-item :label="$t('inboundStatus')">
          {{ item.inboundStatus | inboundStatusFilter }}
        </el-descriptions-item>
        <el-descriptions-item :label="$t('label.receivedGoodsType')">{{
          item.receiveType | receiveTypeFilter
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('label.currencyInformation')">{{
          refundInfo.currency
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('label.refundReason')">{{
          item.refundReason | reasonFilter(item.refundReasonList)
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('label.otherReasons')">{{
          item.otherReason
        }}</el-descriptions-item>
      </el-descriptions>
    </el-card>
    <!--
    <el-divider><i class="el-icon-magic-stick"></i></el-divider>
-->
    <el-card class="box-card">
      <el-descriptions
        :title="$t('title.buyerInformation')"
        :colon="false"
        border
      >
        <el-descriptions-item :label="$t('country')" span="3">{{
          addressInfo.receiverCountryName
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('state')">{{
          addressInfo.receiverState
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('city')">{{
          addressInfo.receiverCity
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('suburb')">{{
          addressInfo.receiverDistrict
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('street')" span="3">{{
          addressInfo.receiverStreet
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('address')" span="3">{{
          addressInfo.receiverAddress
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('dooeNo')" span="2">{{
          addressInfo.receiverAddress2
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('recipient')">{{
          addressInfo.receiverName
        }}</el-descriptions-item>
        <el-descriptions-item :label="$t('telephone')" span="3"
          >{{ addressInfo.receiverPhone
          }}<i
            style="color:blue;"
            v-if="addressInfo.receiverPhone"
            class="el-icon-document-copy"
            :title="$t('title.copy')"
            :copy-value="addressInfo.receiverPhone"
            v-on:click="copy"
          ></i
        ></el-descriptions-item>
        <el-descriptions-item :label="$t('moPhone')" span="3"
          >{{ addressInfo.receiverMobile
          }}<i
            style="color:blue;"
            v-if="addressInfo.receiverMobile"
            class="el-icon-document-copy"
            :title="$t('title.copy')"
            :copy-value="addressInfo.receiverMobile"
            v-on:click="copy"
          ></i
        ></el-descriptions-item>
        <el-descriptions-item :label="$t('post')" span="3"
        >{{ addressInfo.email
          }}<i
            style="color:blue;"
            v-if="addressInfo.email"
            class="el-icon-document-copy"
            :title="$t('title.copy')"
            :copy-value="addressInfo.email"
            v-on:click="copy"
          ></i
          ></el-descriptions-item>
        <el-descriptions-item :label="$t('refundImg')" span="3">
          <el-image
            style="width: 40px; height: 40px"
            :src="refundInfo.annexPath"
            :preview-src-list="[refundInfo.annexPath]"
          >
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </el-descriptions-item>
      </el-descriptions>
    </el-card>
    <!--
    <el-divider><i class="el-icon-magic-stick"></i></el-divider>
-->
    <el-card class="box-card">
      <el-descriptions :title="$t('title.operationLog')"> </el-descriptions>
      <el-timeline>
        <el-timeline-item
          type="primary"
          v-for="(log, index) in logs"
          :key="index"
          :timestamp="log.timeplaced"
        >
          {{ log.userName }} : {{ log.statusNotes }}
        </el-timeline-item>
      </el-timeline>
    </el-card>
  </div>
</template>

<script>
import { handleClipboard } from "@/api/oms/copy";
import {
  getRefundInfo,
  checkRefundCalculates,
  checkCw,
  checkKf,
  queryShippingAmount
} from "@/api/oms/refund/refund";
import { fetchList as reasonList } from "@/api/oms/refund/refundReason";
import global from "@/views/oms/global";
import { queryByOrdersId, queryByOrmorder } from "@/api/oms/order/address";
import { returnWarehouseList } from "@/api/oms/inventory/warehouse";
import { statusKeyValue } from "@/api/oms/order/order";
import i18n from "@/lang";
import { red } from "stylus/lib/functions";

const refundMethodOptions = [
  { key: "system", display_name: "view.systemRefund" },
  { key: "manual", display_name: "view.manualRefund" }
];
const refundMethodKeyValue = refundMethodOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name;
  return acc;
}, {});

const refundAccountTypeOptions = [
  { key: "PayPal", display_name: "PayPal" },
  { key: "bankCard", display_name: "view.bankCard" },
  { key: "other", display_name: "view.other" }
];
const refundAccountTypeKeyValue = refundAccountTypeOptions.reduce(
  (acc, cur) => {
    acc[cur.key] = cur.display_name;
    return acc;
  },
  {}
);

export default {
  name: "refundInfoDialog",
  props: ["refundId", "action"],
  components: {},
  data() {
    return {
      that: this,
      active: 1,
      isShopifyLabel: false,
      activeName: "orderItemList",
      afterSaleStatusSuccess: global.afterSaleStatusSuccess,
      refundInfo: {},
      orderItemInfo: {},
      addressInfo: {},
      logs: [],
      checkData: {
        refundMethod: "system",
        rejectReason: "",
        notes: ""
      },
      btndisabled: true,
      btnLoading: false,
      successbtndisabled: false,
      hideStyleBtn: "",
      btndisableed: false,
      wareHouses: [], //收退货仓库列表
      selectedWarehouseCode: "",
      returnWarehouseAddress: "", //收退货的地址
      returnWcodeAndAddress: {},
      refundPriority: "",
      refundReasons: [],
      slUseAble: "0",
      shippingLabelMessage: "",
      showSlChooseLabel: 0 //是否展示选择使用sl标签 0 否 1 是
    };
  },
  filters: {
    refundAccountTypeFilter(key, that) {
      return that.$t(refundAccountTypeKeyValue[key]);
    },
    refundMethodFilter(key, that) {
      return that.$t(refundMethodKeyValue[key]);
    },
    statusFilterStatus(key) {
      return i18n.t(statusKeyValue[key]);
    },
    statusFilter(key) {
      var value = "";
      global.afterSaleStatus.forEach(status => {
        if (status.value == key) {
          value = status.label;
        }
      });
      return value;
    },
    refundTypeFilter(key,that) {
      var value = "";
      global.afterSaleType.forEach(refundType => {
        if (refundType.value == key) {
          value = that.$t(refundType.label);
        }
      });
      return value;
    },
    statusFiltr2(key) {
      var value = "";
      if(key != null){
        var keys = key.split(",");
        global.returnDeatilStatus.forEach(status => {
          for (var i = 0; i < keys.length; i++) {
            if (status.value == keys[i]) {
              value += status.label + ",";
            }
          }
        });
      }
      return value;
    },
    inboundStatusFilter(key) {
      var value = "";
      if(key != null){
        var keys = key.split(",");
        global.inboundStatus.forEach(type => {
          for (var i = 0; i < keys.length; i++) {
            if (type.value == keys[i]) {
              value += type.label + ",";
            }
          }
        });
      }
      return value;
    },
    reasonFilter(key, refundReasonList) {
      var value = "";
      refundReasonList.forEach(type => {
        if (type.id == key) {
          value = type.reasonContentChinese;
        }
      });
      return value;
    },
    receiveTypeFilter(key) {
      var value = "";
      if(key != null){
        var keys = key.split(",");
        global.receiveType.forEach(type => {
          for (var i = 0; i < keys.length; i++) {
            if (type.value == keys[i]) {
              value += type.label + ",";
            }
          }
        });
      }

      return value;
    },
    SlstatusFilter(key) {
      if (key == "1") {
        return "YES";
      } else if (key == "0") {
        return "NO";
      }
    },
  },
  created() {
    //获取退货详情信息
    this.initialization();
    //获取仓库列表
    //      this.getReturnWarehouseList();
  },
  methods: {
    getRefundReasonList(type) {
      reasonList({ reasonType: type, limit: 1000 }).then(res => {
        if (200 == res.code) {
          this.refundReasons = res.data.list;
        }
      });
    },

    //选择物流公司编码，带出地址
    handelChangeReturnWarehouse() {
      if (this.selectedWarehouseCode !== "PSJ-H0000024"
      && this.selectedWarehouseCode !== "PSJ-H0000019"
      && this.selectedWarehouseCode !== "PSJ-H0000011"
      && this.selectedWarehouseCode !== "PSJ-G001001"
      && this.selectedWarehouseCode !== "PSJ-G001003"
      && this.selectedWarehouseCode !== "PSJ-G001004") {
        this.slUseAble = "0";
        this.showSlChooseLabel = 0; //展示sl的选择标签
        // 触发预估运费查询，目前只有一个仓库使用，可暂时不添加查询逻辑，后续若多仓库支持，则需要增加实时查询预估运费逻辑
      } else {
        this.slUseAble = "1";
        this.showSlChooseLabel = 1; //展示sl的选择标签
      }
      //TODO 暂时关闭SL
      this.slUseAble = "0";
      this.showSlChooseLabel = 1; //展示sl的选择标签

      this.returnWarehouseAddress = this.returnWcodeAndAddress[
        this.selectedWarehouseCode
      ];
      this.checkPSJWarehouse(this.returnWarehouseAddress,  this.selectedWarehouseCode);
    },
    //获取退货仓库列表
    getReturnWarehouseList() {
      this.wareHouses = [];
      this.returnWcodeAndAddress = {};
      returnWarehouseList({'limit':1000}).then(res => {
        if (200 == res.code) {
          if (res.data.list != null && res.data.list.length > 0) {
            res.data.list.forEach(item1 => {
              var addressInfo =
                item1.contacts +
                " " +
                item1.countryCode +
                " " +
                item1.province +
                " " +
                item1.city +
                " " +
                item1.district +
                " " +
                item1.street +
                " " +
                item1.address1 +
                " " +
                item1.address2 +
                " " +
                item1.phoneNo +
                " " +
                item1.mobileNo +
                " " +
                item1.email;
              this.wareHouses.push(item1);

              this.returnWcodeAndAddress[
                item1.returnWarehouseCode
              ] = addressInfo;

              if (item1.returnWarehouseCode == this.refundInfo.warehouseCode) {
                this.selectedWarehouseCode = this.refundInfo.warehouseCode;
                this.returnWarehouseAddress = this.returnWcodeAndAddress[
                  this.selectedWarehouseCode
                ];
              }
            });
          }
          //展示sl的选择标签
          this.checkPSJWarehouse(this.returnWarehouseAddress,  this.selectedWarehouseCode);
          // TODO 暂时关闭SL
          // if (this.selectedWarehouseCode == "PSJ-H0000024") {
          //   this.slUseAble = "1";
          //   this.showSlChooseLabel = 1; //展示sl的选择标签
          // }
        }
        this.listLoading = false;
      });
    },
    //获取售后详情
    initialization() {
      getRefundInfo(this.refundId)
        .then(res => {
          if (200 == res.code) {
            this.refundInfo = res.data;
            this.orderItemInfo = res.data.items;
            this.logs = res.data.logItems;

            //获取收退货仓库列表
            this.getShippingLabelAmount(this.refundInfo, "");
            // this.getRefundReasonList(res.data.refundType);
            return Promise.resolve(res.data.ordersNo);
          }
          return Promise.reject();
        })
        .then(ordersId => {
          return queryByOrdersId(ordersId);
        })
        .then(res => {
          if (200 == res.code) {
            this.addressInfo = res.data;
            this.getReturnWarehouseList();
            if(this.addressInfo.receiverCountryCode == 'GB'){
              this.refundInfo.warehouseCode = 'PSJ-H0000044';
              this.selectedWarehouseCode = 'PSJ-H0000044';
            }

            if(this.addressInfo.receiverCountryCode == 'JP'){
              this.refundInfo.warehouseCode = 'JPNW01';
              this.selectedWarehouseCode = 'JPNW01';
            }

            if(this.addressInfo.receiverCountryCode == 'TW'){
              this.refundInfo.warehouseCode = 'G00886001';
              this.selectedWarehouseCode = 'G00886001';
            }

            if(this.addressInfo.receiverCountryCode == 'HK'){
              this.refundInfo.warehouseCode = 'HKW002';
              this.selectedWarehouseCode = 'HKW002';
            }
          }
        });
    },
    //复制
    copy(event) {
      var text = event.target.attributes["copy-value"].value;
      handleClipboard(text, event);
    },
    handleCheck(status) {
      this.btnLoading = true;
      this.btndisabled = true;
      let msg = "";
      if (status === 6) {
        msg = "确认审核通过？";
      } else {
        msg = "确认审核驳回？";
      }
      this.$confirm(msg, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          var params = {
            id: this.refundInfo.id,
            status: status,
            cwnotes: this.checkData.cwnotes,
            refundMethod: this.refundInfo.refundMethod,
            refundAccountType: this.refundInfo.refundAccountType,
            refundAccount: this.refundInfo.refundAccount,
            refundPriority: this.refundPriority
          };
          checkCw(params).then(
            res => {
              if (200 == res.code) {
                this.$message({
                  message: "操作成功",
                  type: "success"
                });
                this.hideStyleBtn = "display:none";
                this.btndisabled = false;
                this.btndisableed = true;
                this.btnLoading = false;
                this.$emit("refresh");
              }
            },
            error => {
              this.btnLoading = false;
              this.btndisabled = true;
            }
          );
        })
        .catch(() => {
          this.btnLoading = false;
          this.btndisabled = true;
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    // 审核是否是派速捷仓库 美国地址
    checkPSJWarehouse(house, code){
      let shu = house.split(" ");
      if (this.addressInfo.receiverCountryCode == 'US' && (code == "PSJ-G001003" || code == "PSJ-G001001" || code == "PSJ-H0000024"
      || code == "PSJ-H0000019" || code == "PSJ-H0000011" || code == "PSJ-G001004")){
        this.isShopifyLabel = true;
        this.refundInfo.warehouseCode = code;
        this.slUseAble = "1";
        this.getShippingLabelAmount(this.refundInfo);
      }else{
        this.isShopifyLabel = false;
      }

    },
    //客服审核
    handleKfCheck(status) {
      this.btnLoading = true;
      this.btndisabled = true;
      let msg = "";
      if (status === 1) {
        msg = "确认审核通过？";
      } else {
        msg = "确认审核驳回？";
      }
      // if (this.slUseAble == "1") {
      //   if (
      //     this.refundInfo.shippingNo &&
      //     this.refundInfo.shippingNo != "/" &&
      //     (this.refundInfo.shipmodeId && this.refundInfo.shipmodeId != "/")
      //   ) {
      //     this.$message.error("当前退货物流信息已维护，无法使用SL渠道");
      //     this.btnLoading = false;
      //     this.btndisabled = false;
      //     return;
      //   }
      // }
      this.$confirm(msg, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          //如果是退货退款 客服审核且当前的仓库编码是空的提示让客服选择一个收退货仓库编码
          if (
            this.refundInfo.refundType == "3" &&
            !this.selectedWarehouseCode
          ) {
            this.$message({
              type: "info",
              message: "Please choose a warehouse to receive return goods"
            });
            return false;
          }
          var params = {
            id: this.refundInfo.id,
            status: status,
            rejectReason: this.checkData.rejectReason,
            notes: this.checkData.notes,
            warehouseCode: this.selectedWarehouseCode, //选择的收退货仓库编码
            // slUseable: this.slUseAble //是否使用SL
            slUseable: this.slUseAble //TODO 暂时关闭SL
          };
          checkKf(params).then(
            res => {
              if (200 == res.code) {
                this.$message({
                  message: "操作成功",
                  type: "success"
                });
                this.hideStyleBtn = "display:none";
                this.btndisabled = false;
                this.btndisableed = true;
                this.btnLoading = false;
                this.$emit("refresh");
              }
            },
            error => {
              this.btnLoading = false;
              this.btndisabled = true;
            }
          );
        })
        .catch(() => {
          this.btnLoading = false;
          this.btndisabled = true;
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    handleCalculatesCheck() {
      this.btnLoading = true;
      var params = {
        id: this.refundInfo.id
      };
      checkRefundCalculates(params)
        .then(
          res => {
            if (200 === res.code) {
              if(res.msg==="exist"){
                this.refundPriority="gift";
                this.$confirm("当前订单存在礼品卡支付，执行退款将优先退还礼品卡金额", "温馨提示", {
                  confirmButtonText: "确认",
                  cancelButtonText: "关闭",
                  showCancelButton:false,
                  cancelButtonClass: 'btnCancel',
                  type: 'warning',
                  showClose:false
                })
              }else if(res.msg==="none"){
                this.refundPriority="";
                this.$confirm("当前售后没有可退款的支付流水，请您选择手工退款方式", "温馨提示", {
                  confirmButtonText: "确认",
                  cancelButtonText: "关闭",
                  showCancelButton:false,
                  cancelButtonClass: 'btnCancel',
                  type: 'warning',
                  showClose:false
                })
              }else{
                this.refundPriority="";
              }
              this.$message({
                message: "验证通过",
                type: "success"
              });
              this.btnLoading = false;
              this.btndisabled = false;
            }
          },
          error => {
            this.btnLoading = false;
            this.btndisabled = true;
          }
        )
        .catch(() => {
          this.btnLoading = false;
          this.btndisabled = true;
        });
    },
    titleColor(item) {
      if (item.quantity > 1) {
        return "color: red !important;";
      }
    },
    getShippingLabelAmount(refundInfo) {
      if (refundInfo.refundType == "3" && this.selectedWarehouseCode) {
        queryShippingAmount(refundInfo).then(res => {
          if (201 == res.errorKey) {
            this.shippingLabelMessage = res.msg;
          } else if (200 == res.code) {
            this.shippingLabelMessage =
              "当前退款金额为" +
              refundInfo.refundAmount +
              ",开启后退款金额为" +
              res.shippingLabelAmount;
          }
        });
      }
    }
  }
};
</script>

<style lang="scss">
//.btnCancel{
//  float: right;
//  margin-left: 10px;
//  color: #fff !important;
//  background-color: #AE8877 !important;
//  border-color: #AE8877 !important;
//}
//.btnCancel:hover, .btnCancel:focus {
//  background: #bea092 !important;
//  border-color: #bea092 !important;
//  color: #fff !important;
//}
</style>
