<template>
  <div>
    <el-dialog title="修改退款金额" width="410px" v-model="show" @close="reset" :modal="modal">
      <div class="u-flex u-col-top" v-if="goodsList && goodsList.length">
        <span class="u-m-0">退款商品</span>
        <div class="u-p-l-20 goods-list">
          <div class="u-flex u-font-12 goods-list-item" v-for="(goods, index) in goodsList" :key="index">
            <span class="">
              {{ goods.productName }}
            </span>
            <span class="color-999 u-m-l-10">x{{ goods.selNumber || "" }}</span>
            <span class="color-333 u-m-l-10 u-font-600">
              {{ (goods.selNumber * goods.unitPrice).toFixed(2) }}
            </span>
          </div>
        </div>
      </div>
      <div class="u-flex u-col-top u-m-t-22">
        <span class="u-m-0">支付金额</span>
        <div class="u-p-l-20">
          {{ detail.payAmount }}
        </div>
      </div>
      <div class="u-flex u-col-top u-m-t-22">
        <span class="u-m-0">优惠金额</span>
        <div class="u-p-l-20">
          {{ detail.discountAmount }}
        </div>
      </div>
      <div class="u-p-b-16 u-m-t-22">
        <div class="flex u-row-between">
          <span class="color-red">退款金额</span>
          <div class="u-flex u-flex-1 u-p-l-20">
            <el-input-number type="number" v-model="number" :min="min" :max="canReturnMoney"></el-input-number>
            <span class="u-m-l-10">可退{{ canReturnMoney }}元</span>
          </div>
        </div>
        <div class="u-flex u-col-top u-m-t-22" v-if="detail.returnAmount * 1 > 0">
          <span class="u-m-0">退款金额</span>
          <div class="u-p-l-20">
            <span class="color-red">￥ {{ detail.returnAmount || 0 }}</span>
            <!-- <span class="color-666 u-m-l-4">(退款商品金额)</span> -->
          </div>
        </div>
        <!-- <div class="u-flex u-col-top u-m-t-22">
        <span class="u-m-0">退回优惠券</span>
        <div class="u-p-l-20">
          <span class="color-666 u-m-l-4">该订单未使用优惠券</span>
        </div>
      </div> -->
      </div>

      <div class="u-p-b-16 border-bottom">
        <div class="flex">
          <span class="u-m-r-12">是否现金</span>
          <el-switch v-model="cash"></el-switch>
        </div>
      </div>
      <div class="u-m-t-26">
        <div>
          <span>退款原因</span>
          <span class="color-red">*</span>
        </div>
      </div>

      <div class="u-flex u-flex-wrap tags">
        <div class="tag" v-for="(tag, index) in tags" @click="changeSel(tag)" :key="index"
          :class="{ active: tag.checked }">
          {{ tag.label }}
        </div>
      </div>
      <div class="u-m-t-20">
        <el-input v-model="note" placeholder="请输入自定义备注"></el-input>
      </div>
      <template #footer>
        <div>
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </div>
      </template>
    </el-dialog>
    <safe-password ref="refPassword" @confirm="pwdConfirm"></safe-password>
    <!-- 退款退菜推库存的操作弹窗 -->
    <refundConsModal ref="refundConsModalRef" :list="refundList" @success="refundConsModalSuccess" />
  </div>
</template>
<script>
import { ElMessage } from "element-plus";
import safePassword from "./password.vue";
import { useUserStore } from "@/store/modules/user";
import { useCartsStore } from "@/store/modules/carts";
import categoryApi from "@/api/product/productclassification";
import refundConsModal from "@/components/refundConsModal.vue";
const shopUser = useUserStore();
export default {
  components: {
    safePassword,
    refundConsModal
  },
  props: {
    modal: {
      type: Boolean,
      default: true,
    },
    max: {
      type: Number,
      default: 1,
    },
  },
  data() {
    return {
      detail: "",
      goodsList: [],
      cash: false,
      isFloat: false,
      min: 0,
      number: 1,
      isPrint: false,
      tagSel: -1,
      show: false,
      tags: [
        { label: "不想要了", checked: false },
        { label: "食材不足", checked: false },
        { label: "等待时间过长", checked: false },
      ],
      note: "",
      goods: {
        productId: -999,
      },
      refundList: [],
      refundStock: ''
    };
  },
  computed: {
    tuikuanJine() {
      const danjia = this.goods.price;
      return (danjia * this.number).toFixed(2);
    },
    canReturnMoney() {
      if (!this.detail) {
        return 0;
      }
      return this.detail.payAmount - this.detail.refundAmount;
    },
  },
  filters: {
    to2(val) {
      return val.toFixed(2);
    },
  },
  methods: {
    changeSel(item) {
      item.checked = !item.checked;
    },
    reset() {
      this.note = "";
      this.number = 1;
      this.isFloat = false;
    },
    delTag(index) {
      this.tags.splice(index, 1);
    },
    addNote(tag) {
      if (this.note.length <= 0) {
        return (this.note = tag);
      }
      this.note = tag + "," + this.note;
    },
    open(goods, detailInfo) {
      const totalMoney = goods.reduce((prve, cur) => {
        console.log(cur);
        return prve + cur.unitPrice * cur.selNumber;
      }, 0);
      this.number = totalMoney.toFixed(2);
      this.goodsList = goods;
      this.show = true;
      this.detail = detailInfo;
    },
    close() {
      this.show = false;
      this.number = 1;
    },
    pwdConfirm(pwd) {
      this.emitTuikuan({ pwd });
    },
    emitTuikuan(e) {
      const selTag = this.tags
        .filter((item) => item.checked)
        .map((item) => item.label)
        .join(",");
      const note = selTag + (this.note.length > 0 ? "," + this.note : "");
      console.log(note);

      const data = {
        refundAmount: this.number,
        cash: this.cash,
        refundReason: note,
        refundDetails: this.goodsList.map((v) => {
          return { id: v.id, num: v.selNumber };
        }),
        ...e,
        refundStock: this.refundStock
      }
      this.$emit("confirm", data);
      this.close();
    },
    // 退款推库存的操作
    refundConsModalSuccess(e) {
      this.refundStock = e
      this.emitTuikuan()
    },
    async confirm() {
      const selTag = this.tags
        .filter((item) => item.checked)
        .map((item) => item.label)
        .join(",");
      const note = selTag + (this.note.length > 0 ? "," + this.note : "");
      if (!note) {
        return ElMessage.error("请输入退款原因");
      }
      if (shopUser.userInfo.isReturnPwd) {
        this.$refs.refPassword.open();
        return;
      }

      // 在这里给订单的商品补全库存信息 start
      const carts = useCartsStore();
      let categorys = JSON.parse(localStorage.getItem('categorys'))
      let shopInfo = JSON.parse(localStorage.getItem('userInfo'))

      if (!categorys) {
        categorys = await categoryApi.getList({})
      }

      this.goodsList.forEach(item => {
        carts.goods.forEach(val => {
          if (item.productId == val.id) {
            if (shopInfo.refundMode == 1) {
              // 跟随分类退款模式
              categorys.forEach(v => {
                if (val.categoryId == v.id) {
                  item.refundMode = v.refundMode
                }
              })
            } else {
              // 跟随商品退款模式及
              item.refundMode = val.refundMode
            }
          }
        })
      })
      console.log('this.goodsList===', this.goodsList);
      // 在这里给订单的商品补全库存信息 end

      this.goodsList.forEach(item => {
        if (item.refundMode == 3) {
          this.refundList.push({
            name: item.productName,
            num: item.selNumber
          })
        }
      })

      if (this.refundList.length > 0) {
        this.$refs.refundConsModalRef.show()
        return
      }

      this.emitTuikuan();
    },
  },
  mounted() { },
};
</script>

<style lang="scss" scoped>
:deep(.el-tag) {
  margin-top: 10px;
  margin-right: 10px;
  margin-bottom: 5px;
  cursor: pointer;
  font-size: 15px;
  line-height: 35px;
  height: 35px;
}

.tags {
  .tag {
    margin: 10px 10px 0 0;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    padding: 10px 13px;
    font-size: 14px;
    color: #000;
    cursor: pointer;

    &.active {
      color: #1890ff;
      background: #e8f4ff;
      border-color: #a3d3ff;
    }
  }
}

:deep(.number-box .el-input__inner::-webkit-inner-spin-button) {
  -webkit-appearance: none;
  margin: 0;
}

:deep(.number-box .el-input__inner::-webkit-outer-spin-button) {
  -webkit-appearance: none;
  margin: 0;
}

.goods-list-item {
  height: 24px;
  line-height: 24px;
  background: #f0f2f5;
  border-radius: 3px;
  padding: 0 10px;
}

.goods-list-item:not(:first-child) {
  margin-top: 6px;
}
</style>