<template>
  <div>
    <el-dialog  v-model="visible" :z-index="99" :title="title" width="700" align-center >
      <el-form :inline="true" :rules="rules" :model="modalForm" class="demo-form-inline" ref="ruleFormRef">
        <div class="barbox" style="margin-top: -10px;">
          <div class="bar"></div>
          <div class="name">会员配置信息</div>
        </div>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="会员时长(天数)：" prop="member_days">
              <el-input type="number" v-model.number="modalForm.member_days" style="width: 200px" placeholder="请输入会员时长" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价格：" prop="price">
              <el-input min="1" type="number" v-model="modalForm.price" style="width: 200px" placeholder="请输入会员价格" />
            </el-form-item>
          </el-col>
        </el-row>
       
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="有效算算价格(元)：" prop="reduction_amount" >
              <el-input disabled min="0" type="number" v-model="priceForm.price" style="width: 200px" placeholder="请输入折扣价格" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="priceForm.price || priceForm.price===0">
            <el-form-item label="有效算算优惠价格(元)：" prop="reduction_amount" >
              <el-input disabled min="0" type="number" v-model="priceForm.discount_price" style="width: 200px" placeholder="请输入折扣价格" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="折扣价格：" prop="discount_price" >
              <el-input min="0" type="number" v-model="modalForm.discount_price" style="width: 200px" placeholder="请输入折扣价格" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="modalForm.discount_price || modalForm.discount_price===0">
            <el-form-item label="会员截止时间：" prop="discount_end_time">
              <el-date-picker style="width:240px" v-model="modalForm.discount_end_time" type="datetime" placeholder="请选择优惠截至时间" format="YYYY-MM-DD HH:mm:ss"  value-format="YYYY-MM-DD HH:mm:ss"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="首单立减金额(元)：" prop="reduction_amount" >
              <el-input min="0" type="number" v-model="modalForm.reduction_amount" style="width: 200px" placeholder="请输入折扣价格" />
            </el-form-item>
          </el-col>
          
        </el-row>
      </el-form>
      <el-divider />
      <div class="barbox">
        <div class="bar"></div>
        <div class="name">操作</div>
      </div>
      <el-row :gutter="20">
        <el-col :span="16">
          <el-form-item label="" style="padding-left:30px">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="submit(ruleFormRef)">
              确定
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="submit">
          确定
        </el-button>
      </div>
    </template> -->
    </el-dialog>
    <el-dialog v-model="dialogVisible" title="温馨提示" width="400">
      <span>确定取消该订单？</span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmCancel">
            Confirm
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>

</template>

<script setup>
import { onBeforeMount, reactive, ref, nextTick } from 'vue';
import { useRouter, useRoute } from 'vue-router'
import userApi from "../api/user";
import { ElMessage } from "element-plus";
import { formatOrderStatus, formatRefundStatus, formatDate } from '../utils/format'

const ruleFormRef = ref(null);
const method=ref(null);
const emit=defineEmits(['refreshList']);
const modalForm = reactive({
  system_member_id: ''
})
const priceForm=reactive({
  
})
const rules = reactive({
 member_days:[
    {
      required: true,
      message: '请输入会员时长',

      trigger: 'blur',
    }
  ],
  price:[
    {
      required: true,
      message: '请输入价格',
      trigger: 'blur',

   }
  ],


});
const handleAdd = () => {
  if(!modalForm.system_member_id){
    delete modalForm.system_member_id
  }
  if(modalForm.price!==''){
     if(modalForm.price<=0){
      ElMessage.error('价格不能小于0');
      return
     }
  }
  if(modalForm.price){
    modalForm.price = Number(modalForm.price)

  }
  if(!modalForm.discount_price && modalForm.discount_price!==0){
    delete modalForm.discount_price
  }else{
    modalForm.discount_price = Number(modalForm.discount_price)
  }
  if(!modalForm.reduction_amount && modalForm.reduction_amount!==0){
    delete modalForm.reduction_amount
  }else{
    modalForm.reduction_amount = Number(modalForm.reduction_amount)
  }
  userApi.saveVipSetting(modalForm).then((res) => {
    let result = res;
   
    if (result.code == 200) {
        ElMessage.success("添加成功");
        visible.value = false;
        emit('refreshList')
    }

  })
};
const handleEdit = () => {
  if(modalForm.price){
    modalForm.price = Number(modalForm.price)

  }
  if(modalForm.price!==''){
     if(modalForm.price<=0){
      ElMessage.error('价格不能小于0');
      return
     }
  }
  if(!modalForm.discount_price && modalForm.discount_price!==0){
    delete modalForm.discount_price
  }else{
    modalForm.discount_price = Number(modalForm.discount_price)
  }
  if(!modalForm.reduction_amount && modalForm.reduction_amount!==0){
    delete modalForm.reduction_amount
  }else{
    modalForm.reduction_amount = Number(modalForm.reduction_amount)
  }
  userApi.editVipSetting(modalForm.system_member_id,modalForm).then((res) => {
    let result = res;
   
    if (result.code == 200) {
        ElMessage.success("修改成功");
        visible.value = false;
        emit('refreshList')
    }

  })
};


const submit = async(form) => {
  await form.validate((valid, fields) => {
    if (valid) {
      method.value === 'edit'?handleEdit() : handleAdd()
    } else {
      console.log('error submit!', fields)
    }
 
  });
}
const visible = ref(false);
const dialogVisible = ref(false);
const title = ref('订单详情')
const route = useRoute();
const router = useRouter();


const cancelOrder = () => {

};
const confirmCancel = () => {
  dialogVisible.value = false;
  cancelOrder()

}
const getPrice=()=>{
  userApi.getPrice().then((res) => {
       
        Object.assign(priceForm, res.data);
        console.log(priceForm)
      })
}
const init = (type, id) => {
  visible.value = true;
  method.value = type;
  modalForm.system_member_id = id;
  if (method.value === 'edit') {
    title.value = '编辑会员配置'
  } else if (method.value === 'view') {
    title.value = '新建会员配置'
  }
  getPrice();
  nextTick(() => {
    ruleFormRef.value.resetFields();
    modalForm.discount_end_time = '';
    if (method.value === 'edit') {
      userApi.getVipSettingDetail(modalForm.system_member_id).then((res) => {
        let result = res.data;
        Object.assign(modalForm, result);

      })
    }
  })

}

defineExpose({
  init
});
</script>

<style lang="scss" scoped>
.barbox {
  margin: 10px 0;
  display: flex;
  align-items: center;

  .bar {
    width: 4px;
    height: 25px;
    background: var(--el-color-primary);
  }

  .name {
    margin-left: 8px;
    font-size: 16px;
  }
}
</style>