<!--新增特价促销-->
<template>
  <el-main class="wrap_box bg_white">
    <el-row class="title_box head_box">
      <el-col :span="24">
        <el-button type="primary" @click="saveedit()">保存</el-button>
      </el-col>
    </el-row>
    <!-- 表单-->
    <div style="margin: 24px;">
      <el-form ref="tjFormRef" :model="tjdata" label-width="auto" class="demo-ruleForm" label-position="right">
        <el-row class="tjrow">
          <el-form-item label="编号" >
            <el-input v-model="tjdata.promotion_sn" class="tjit-w-px" disabled></el-input>
          </el-form-item>
          <el-form-item label="部门" required>
            <el-input v-model="tjdata.depart" class="tjit-w-px"></el-input>
          </el-form-item>
        </el-row>
        <el-row class="tjrow">
          <el-form-item label="规格型号(SKU)" required>
            <el-input v-model="tjdata.skuname" class="tjit-w-px" disabled></el-input>
          </el-form-item>
          <el-form-item label="类型" required>
            <el-select v-model="tjdata.type" placeholder="" class="tjit-w-px">
              <el-option v-for="item in typelist" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-row>
        <el-row class="tjrow">
          <el-form-item label="名称" required>
            <el-input v-model="tjdata.nameFromSku" class="tjit-w-px" disabled></el-input>
          </el-form-item>
          <el-form-item label="出厂价" required>
            <el-input v-model="tjdata.factory_price" class="tjit-w-px"
              @input="validateNumber($event, 'factory_price')"></el-input>
          </el-form-item>
        </el-row>
        <el-row class="tjrow">
          <el-form-item label="产品分类" required>
            <el-input v-model="tjdata.cpflfromSku" class="tjit-w-px" disabled></el-input>
          </el-form-item>
          <el-form-item label="清库核算价格/清库特价" >
            <el-input v-model="tjdata.price" class="tjit-w-px" @input="validateNumber($event, 'price')"></el-input>
          </el-form-item>
        </el-row>
        <el-row class="tjrow">
          <el-form-item label="建议国内零售限价(￥)" >
           <el-input v-model="tjdata.domestic_retail_price" class="tjit-w-px" @input="validateNumber($event, 'domestic_retail_price')"></el-input>
          </el-form-item>
          <el-form-item label="建议海外零售限价($)">
            <el-input v-model="tjdata.overseas_retail_price" class="tjit-w-px" @input="validateNumber($event, 'overseas_retail_price')"></el-input>
          </el-form-item>
        </el-row>
        <el-row class="tjrow">
          <el-form-item label="库存变现生效/失效日期" required>
            <el-date-picker v-model="stocktimeArry"  type="daterange"
              range-separator="-" start-placeholder="请选择生效日期" end-placeholder="请选择失效日期" class="tjit-w-px" />
          </el-form-item>
          <el-form-item label="清库折扣力度">
            <el-input v-model="tjdata.ckldNum" class="tjit-w-px" disabled></el-input>
          </el-form-item>
        </el-row>
        <el-row class="tjrow">
          <el-form-item label="特价生效/失效日期" required>
            <el-date-picker v-model="timeArry"  type="daterange"
              range-separator="-" start-placeholder="请选择生效日期" end-placeholder="请选择失效日期" class="tjit-w-px" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="tjdata.remark" type="textarea" class="tjit-w-px" :autosize="{ minRows: 4 }"></el-input>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
  </el-main>
</template>
<script setup name="editspeciapr">
import { reactive, ref, getCurrentInstance, onActivated } from 'vue'
import { ElMessage } from 'element-plus'
import { useRoute } from 'vue-router'
const tjdata = reactive({
  promotion_sn: '',//编号
  depart:'',//部门
  skuname: '',//型号
  type: '',//类型
  factory_price: '',//出厂价
  price: '',//清库核算价格/清库特价
  domestic_retail_price: '',// 建议国内零售限价($)
  overseas_retail_price:'',// 建议海外零售限价($)
  start_time: '',//特价失效开始时间
  end_time: '',//特价失效结束时间
  stock_start_time: '',//库存变现开始时间
  stock_end_time: '',//库存变现结束时间
  remark: [],//备注
  cpflfromSku: '',//产品分类
  nameFromSku: '',//名称
  ckldNum: '',// 折扣力度

})

let route = useRoute()
const timeArry = ref([])// 特价生效/失效时间
const stocktimeArry = ref([])// 库存变现生效/失效时间
onActivated(() => {
  init()
})
function init () {
  proxy.$http.get('promotion/info', { id: route.query.id }).then(res => {
    if (res.errno == 200) {
      tjdata.promotion_sn = res.data.promotion_sn
      tjdata.depart = res.data.depart
      tjdata.skuname = res.data.sku
      tjdata.nameFromSku = res.data.name
      tjdata.cpflfromSku = res.data.cate_name
      tjdata.type = res.data.type
      tjdata.remark = res.data.remark
      tjdata.factory_price = res.data.factory_price
      tjdata.price = res.data.price
      tjdata.start_time = res.data.start_time
      tjdata.end_time = res.data.end_time
      tjdata.stock_start_time = res.data.stock_start_time
      tjdata.stock_end_time = res.data.stock_end_time
      tjdata.ckldNum = res.data.discount
      tjdata.id = route.query.id
      tjdata.domestic_retail_price = res.data.domestic_retail_price
      tjdata.overseas_retail_price = res.data.overseas_retail_price
      timeArry.value = [res.data.start_time, res.data.end_time]
      stocktimeArry.value = [res.data.stock_start_time, res.data.stock_end_time]
    }
  })
}
const typelist = [
  {
    label: '停产促销',
    value: 1,
  }, {
    label: '仅第三方仓促销',
    value: 2
  }, {
    label: '不停产促销',
    value: 3
  },{
    label: '战略产品特价',
    value: 4
  }
]
const { proxy } = getCurrentInstance()
// 出厂价输入
function validateNumber (value, key) {
  tjdata[key] = value.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/, "$1")
  if (key !== 'domestic_retail_price' && key !== "overseas_retail_price") {
    //计算折扣
    if (tjdata.factory_price && tjdata.price) {
      // 定义一个非常小的数作为阈值，用于判断价格是否“几乎为0”  
      const EPSILON = 1e-9;
      // 检查价格是否几乎为0  
      if (Math.abs(tjdata.price) < EPSILON) {
        // 如果价格为0或非常接近于0，你可以将ckldNum设置为0，或者一个你认为合适的默认值  
        tjdata.ckldNum = 0.00; // 或者你想要的任何默认值  
      } else {
        // 否则，进行正常的计算  特价促销的公式  10x特价/出厂价
        if (tjdata.factory_price !== '0' && tjdata.price !== '0') {
          tjdata.ckldNum = parseFloat(((tjdata.price * 10) / tjdata.factory_price).toFixed(2));
        } 
      } 
    } else { 
      tjdata.ckldNum = tjdata.price == '0' || !tjdata.price ||tjdata.factory_price == '0' || !tjdata.factory_price ? null : tjdata.ckldNum
    }
  }
}
//时间格式处理
function setTime (date) {
  if (!(date instanceof Date)) {
        date = new Date(date);
    }
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
}
// 保存编辑
function saveedit () {
  if (!tjdata.depart) {
    ElMessage({
      type: 'error',
      message: '请输入部门!'
    })
    return
  }
  if (!tjdata.type) {
    ElMessage({
      type: 'error',
      message: '请选择类型！'
    })
    return
  }
  if (!tjdata.factory_price) {
    ElMessage({
      type: 'error',
      message: '请设置出厂价！'
    })
    return
  }
  if (!timeArry.value || timeArry.value.length !== 2) {
    ElMessage({
      type: 'error',
      message: '请设置特价生效/失效时间！'
    })
    return
  } else {
    tjdata.start_time =  setTime(timeArry.value[0])
    tjdata.end_time =setTime(timeArry.value[1])  
  }
  if (!stocktimeArry.value || stocktimeArry.value.length !== 2) {
    ElMessage({
      type: 'error',
      message: '请设置库存变现生效/失效时间！'
    })
    return
  } else {
    tjdata.stock_start_time = setTime(stocktimeArry.value[0]) 
    tjdata.stock_end_time = setTime(stocktimeArry.value[1])
  }

  proxy.$http.post('promotion/edit', tjdata).then(res => {
    if (res.errno == 200) {
      ElMessage({
        type: 'success',
        message: '编辑成功',
        duration: 1500
      })
    } else {
      ElMessage({
        type: 'error',
        message: res.errmsg,
        duration: 1500
      })
    }
  })
}
</script>
<style scoped>
.tjit-w-px {
  width: 500px;
}

.tjrow {
  margin-bottom: 15px;
}

::v-deep(.el-range-editor.el-input__inner) {
  width: 500px;
}
::v-deep(.el-form-item){
  margin-right: 70px;
}
</style>