<template>
  <!-- 商品促销信息表单 -->
  <div style="margin-top: 50px">
    <el-form
      :model="value"
      ref="productSaleForm"
      label-width="120px"
      class="form-inner-container"
      size="small"
    >
      <!-- 赠送积分 -->
      <el-form-item label="赠送积分：">
        <el-input v-model="value.giftPoint"></el-input>
      </el-form-item>

      <!-- 赠送成长值 -->
      <el-form-item label="赠送成长值：">
        <el-input v-model="value.giftGrowth"></el-input>
      </el-form-item>

      <!-- 积分购买限制 -->
      <el-form-item label="积分购买限制：">
        <el-input v-model="value.usePointLimit"></el-input>
      </el-form-item>

      <!-- 预告商品开关 -->
      <el-form-item label="预告商品：">
        <el-switch
          v-model="value.previewStatus"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>

      <!-- 商品上架开关 -->
      <el-form-item label="商品上架：">
        <el-switch
          v-model="value.publishStatus"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>

      <!-- 商品推荐：新品与推荐开关 -->
      <el-form-item label="商品推荐：">
        <span style="margin-right: 10px">新品</span>
        <el-switch
          v-model="value.newStatus"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
        <span style="margin-left: 10px; margin-right: 10px">推荐</span>
        <el-switch
          v-model="value.recommandStatus"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>

      <!-- 服务保证复选框组 -->
      <el-form-item label="服务保证：">
        <el-checkbox-group v-model="selectServiceList">
          <el-checkbox :label="1">无忧退货</el-checkbox>
          <el-checkbox :label="2">快速退款</el-checkbox>
          <el-checkbox :label="3">免费包邮</el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <!-- 详细页标题 -->
      <el-form-item label="详细页标题：">
        <el-input v-model="value.detailTitle"></el-input>
      </el-form-item>

      <!-- 详细页描述 -->
      <el-form-item label="详细页描述：">
        <el-input v-model="value.detailDesc"></el-input>
      </el-form-item>

      <!-- 商品关键字 -->
      <el-form-item label="商品关键字：">
        <el-input v-model="value.keywords"></el-input>
      </el-form-item>

      <!-- 商品备注 -->
      <el-form-item label="商品备注：">
        <el-input
          v-model="value.note"
          type="textarea"
          :autoSize="true"
        ></el-input>
      </el-form-item>

      <!-- 选择优惠方式 -->
      <el-form-item label="选择优惠方式：">
        <el-radio-group v-model="value.promotionType" size="small">
          <el-radio-button :label="0">无优惠</el-radio-button>
          <el-radio-button :label="1">特惠促销</el-radio-button>
          <el-radio-button :label="2">会员价格</el-radio-button>
          <el-radio-button :label="3">阶梯价格</el-radio-button>
          <el-radio-button :label="4">满减价格</el-radio-button>
        </el-radio-group>
      </el-form-item>

      <!-- 特惠促销设置 -->
      <el-form-item v-show="value.promotionType === 1">
        <div>
          开始时间：
          <el-date-picker
            v-model="value.promotionStartTime"
            type="datetime"
            :picker-options="pickerOptions1"
            placeholder="选择开始时间"
          ></el-date-picker>
        </div>
        <div class="littleMargin">
          结束时间：
          <el-date-picker
            v-model="value.promotionEndTime"
            type="datetime"
            :picker-options="pickerOptions1"
            placeholder="选择结束时间"
          ></el-date-picker>
        </div>
        <div class="littleMargin">
          促销价格：
          <el-input
            style="width: 220px"
            v-model="value.promotionPrice"
            placeholder="输入促销价格"
          ></el-input>
        </div>
      </el-form-item>

      <!-- 会员价格设置 -->
      <el-form-item v-show="value.promotionType === 2">
        <div v-for="(item, index) in value.memberPriceList" :key="index" class="littleMargin">
          {{ item.memberLevelName }}：
          <el-input v-model="item.memberPrice" style="width: 200px"></el-input>
        </div>
      </el-form-item>

      <!-- 阶梯价格设置 -->
      <el-form-item v-show="value.promotionType === 3">
        <el-table :data="value.productLadderList" style="width: 80%" border>
          <el-table-column label="数量" align="center" width="120">
            <template slot-scope="scope">
              <el-input v-model="scope.row.count"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="折扣" align="center" width="120">
            <template slot-scope="scope">
              <el-input v-model="scope.row.discount"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="handleRemoveProductLadder(scope.$index)">
                删除
              </el-button>
              <el-button type="text" @click="handleAddProductLadder(scope.$index)">
                添加
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>

      <!-- 满减价格设置 -->
      <el-form-item v-show="value.promotionType === 4">
        <el-table :data="value.productFullReductionList" style="width: 80%" border>
          <el-table-column label="满" align="center" width="120">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fullPrice"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="立减" align="center" width="120">
            <template slot-scope="scope">
              <el-input v-model="scope.row.reducePrice"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="handleRemoveFullReduction(scope.$index)">
                删除
              </el-button>
              <el-button type="text" @click="handleAddFullReduction(scope.$index)">
                添加
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>

      <!-- 操作按钮 -->
      <el-form-item style="text-align: center">
        <el-button size="medium" @click="handlePrev">上一步，填写商品信息</el-button>
        <el-button type="primary" size="medium" @click="handleNext">
          下一步，填写商品属性
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { fetchList as fetchMemberLevelList } from '@/api/memberLevel'; // 获取会员等级API

export default {
  name: 'ProductSaleDetail',
  props: {
    value: Object, // 父组件传入的数据对象
    isEdit: {
      type: Boolean, // 是否编辑模式
      default: false,
    },
  },
  data() {
    return {
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() < Date.now(); // 禁用过去时间
        },
      },
    };
  },
  created() {
    fetchMemberLevelList({ defaultStatus: 0 }).then((response) => {
      this.value.memberPriceList = response.data.map((item) => ({
        memberLevelId: item.id,
        memberLevelName: item.name,
        memberPrice: 0,
      }));
    });
  },
  computed: {
    // 选中的服务保证
    selectServiceList: {
      get() {
        return this.value.serviceIds ? this.value.serviceIds.split(',').map(Number) : [];
      },
      set(newValue) {
        this.value.serviceIds = newValue.join(',');
      },
    },
  },
  methods: {
    handleRemoveProductLadder(index) {
      this.value.productLadderList.splice(index, 1);
    },
    handleAddProductLadder() {
      this.value.productLadderList.push({ count: 0, discount: 0 });
    },
    handleRemoveFullReduction(index) {
      this.value.productFullReductionList.splice(index, 1);
    },
    handleAddFullReduction() {
      this.value.productFullReductionList.push({ fullPrice: 0, reducePrice: 0 });
    },
    handlePrev() {
      this.$emit('prevStep');
    },
    handleNext() {
      this.$emit('nextStep');
    },
  },
};
</script>

<style scoped>
.littleMargin {
  margin-top: 10px;
}
</style>
