<template>
  <el-dialog title="退菜" width="410px" v-model="show" @close="reset" :modal="modal">
    <div class="u-p-b-16">
      <div class="flex u-row-between">
        <span>菜品名称</span>
        <div class="u-flex">
          {{ goods.name || "" }}
        </div>
      </div>
      <div class="u-font-12 color-999 u-m-t-8" v-if="isSeatFee">
        <div>
          <span class="color-red">*</span>
          <span>客座费只能全退</span>
        </div>
      </div>
    </div>
    <div class="u-p-b-16 border-bottom">
      <div class="flex u-row-between">
        <span>退菜数量</span>
        <div class="u-flex" v-if="!isSeatFee">
          <el-input-number v-model="number" :min="1" step-strictly :max="max"></el-input-number>
        </div>
        <div class="u-flex" v-else>
          {{ number }}
        </div>
      </div>
      <div class="u-font-12 color-999 u-m-t-8" v-if="isSeatFee">
        <div>
          <span class="color-red">*</span>
          <span>客座费只能全退</span>
        </div>
      </div>
    </div>

    <div class="u-m-t-10 u-font-12 color-999">菜品已点数量 {{ max }} 份</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>
    <div class="u-m-t-20">
      <el-checkbox v-model="isPrint">打印退菜单</el-checkbox>
    </div>
    <template #footer>
      <div>
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </template>
  </el-dialog>
  <!-- 退款退菜推库存的操作弹窗 -->
  <refundConsModal ref="refundConsModalRef" :list="refundList" @success="refundConsModalSuccess" />
</template>
<script>
import { ElMessage } from "element-plus";
import { useCartsStore } from '@/store/modules/carts'
import refundConsModal from "@/components/refundConsModal.vue";
export default {
  components: { refundConsModal },
  props: {
    modal: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      max: 1,
      number: 1,
      isPrint: false,
      tagSel: -1,
      show: false,
      tags: [
        { label: "不想要了", checked: false },
        { label: "食材不足", checked: false },
        { label: "等待时间过长", checked: false },
      ],
      note: "",
      goods: {
        productId: -999,
      },
      refundList: [],
      refundStock: '',
      note: ''
    };
  },
  computed: {
    isSeatFee() {
      return false;
    },
  },
  methods: {
    changeSel(item) {
      item.checked = !item.checked;
    },
    reset() {
      this.note = "";
      this.number = 1;
      this.tags.map((v) => {
        v.checked = false;
      });
      console.log(this.number);
    },
    delTag(index) {
      this.tags.splice(index, 1);
    },
    addNote(tag) {
      if (this.note.length <= 0) {
        return (this.note = tag);
      }
      this.note = tag + "," + this.note;
    },
    open(item) {
      this.goods = item ? item : this.goods;
      this.max = item.num - item.returnNum;
      this.show = true;
      if (item.productId != "-999") {
        this.number = 1;
      } else {
        this.number = this.max;
      }
    },
    close() {
      this.show = false;
      this.number = 1;
    },
    // 选择退库存模式后
    refundConsModalSuccess(e) {
      this.refundStock = e
      this.refundNext()
    },
    refundNext() {
      this.$emit("confirm", {
        refundReason: this.note,
        refundAmount: 0,
        refundDetails: [{ id: this.goods.id, num: this.number, returnAmount: 0 }],
        refundStock: this.refundStock
      });
      this.close();
    },
    // 确认退菜
    confirm() {
      const selTag = this.tags
        .filter((item) => item.checked)
        .map((item) => item.label)
        .join(",");
      this.note = selTag + (this.note.length > 0 ? "," + this.note : "");

      if (!this.note) {
        return ElMessage.error("请输入退菜原因");
      }

      const carts = useCartsStore();
      let categorys = JSON.parse(localStorage.getItem('categorys'))
      let shopInfo = JSON.parse(localStorage.getItem('userInfo'))

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

      console.log('this.goods===', this.goods);
      if (this.goods.refundMode == 3) {
        this.refundList = [
          {
            name: this.goods.product_name,
            num: this.number
          }
        ]
        this.$refs.refundConsModalRef.show()
        return
      }
      // 在这里给订单的商品补全库存信息 end
      this.refundNext()
    },
  },
  mounted() { },
};
</script>

<style lang="scss" scoped>
:deep(.el-dialog__body) {
  margin-bottom: 14px;
  margin-top: 14px;
  padding: 0 20px;
}

: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;
}
</style>