<!--
 * @Descripttion: (商品/tb_goods 编辑弹窗)
 * @version: (1.0)
 * @Author: (lwh)
 * @Date: (2023-06-19)
 * @LastEditors: (lwh)
 * @LastEditTime: (2023-06-19)
-->
<template>
  <el-dialog v-model="props.modelValue" title="修改商品信息" width="900px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-row :gutter="20">
	  

    
    
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="店铺guid" prop="shopGuid">
              <el-input v-model="formData.shopGuid" placeholder="请输入店铺guid" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="商品类目guid" prop="goodsCategoryGuid">
              <el-input v-model="formData.goodsCategoryGuid" placeholder="请输入商品类目guid" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="商品规格guid" prop="goodsSkuGuid">
              <el-input v-model="formData.goodsSkuGuid" placeholder="请输入商品规格guid" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="配送模板guid" prop="deliveryGuid">
              <el-input v-model="formData.deliveryGuid" placeholder="请输入配送模板guid" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="名称" prop="goodsName">
              <el-input v-model="formData.goodsName" placeholder="请输入名称" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="编码" prop="goodsCoding">
              <el-input v-model="formData.goodsCoding" placeholder="请输入编码" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="主图视频" prop="goodsMainImageVideo">
              <UploadFile v-model="formData.goodsMainImageVideo" :data=imgData />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="视频封面" prop="goodsVideoCover">
              <UploadImage ref="uploadRef" v-model="formData.goodsVideoCover" :data=imgData :limit="1" :fileSize="5"
              :drag="true" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="图片" prop="goodsPicture">
              <UploadImage ref="uploadRef" v-model="formData.goodsPicture" :data=imgData :limit="1" :fileSize="5"
              :drag="true" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="价格(最低)" prop="goodsPriceLowest">
              <el-input-number v-model.number="formData.goodsPriceLowest" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="价格(最高)" prop="goodsPriceHighest">
              <el-input-number v-model.number="formData.goodsPriceHighest" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="划线价格(最低)" prop="goodsDashedPriceLowest">
              <el-input-number v-model.number="formData.goodsDashedPriceLowest" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="库存总量" prop="goodsTotalInventory">
              <el-input-number v-model.number="formData.goodsTotalInventory" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="初始销量" prop="goodsSalesInitial">
              <el-input-number v-model.number="formData.goodsSalesInitial" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="实际销量" prop="goodsSalesActual">
              <el-input-number v-model.number="formData.goodsSalesActual" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>

          <el-col :lg="24">
            <el-form-item :label-width="labelWidth" label="详情" prop="goodsDetails">
              <editor v-model="formData.goodsDetails" :min-height="200" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="库存计算方式(1下单立减库存 2付款立减库存)" prop="goodsDeductStockType">
              <el-radio-group v-model="formData.goodsDeductStockType">
                <el-radio v-for="item in  deduct_stock_type " :key="item.dictValue" :label="parseInt(item.dictValue)">{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="是否赠送积分(1开启 0关闭)" prop="goodsIsPointsGift">
              <el-radio-group v-model="formData.goodsIsPointsGift">
                <el-radio v-for="item in  is_points_gift " :key="item.dictValue" :label="parseInt(item.dictValue)">{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="是否允许使用积分抵扣(1允许 0不允许)" prop="goodsIsPointsDiscount">
              <el-radio-group v-model="formData.goodsIsPointsDiscount">
                <el-radio v-for="item in  is_points_discount " :key="item.dictValue" :label="parseInt(item.dictValue)">{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="积分抵扣设置(0默认抵扣 1单独设置抵扣)" prop="goodsIsAlonePointsDiscount">
              <el-radio-group v-model="formData.goodsIsAlonePointsDiscount">
                <el-radio v-for="item in  is_alone_points_discount " :key="item.dictValue" :label="parseInt(item.dictValue)">{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="单独设置积分抵扣的配置" prop="goodsPointsDiscountConfig">
              <el-input v-model="formData.goodsPointsDiscountConfig" placeholder="请输入单独设置积分抵扣的配置" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="是否开启会员折扣(1开启 0关闭)" prop="goodsIsEnableGrade">
              <el-radio-group v-model="formData.goodsIsEnableGrade">
                <el-radio v-for="item in  is_enable_grade " :key="item.dictValue" :label="parseInt(item.dictValue)">{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="会员折扣设置(0默认等级折扣 1单独设置折扣)" prop="goodsIsAloneGrade">
              <el-radio-group v-model="formData.goodsIsAloneGrade">
                <el-radio v-for="item in  is_alone_grade " :key="item.dictValue" :label="parseInt(item.dictValue)">{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="单独设置折扣的配置" prop="goodsAloneGradeEquity">
              <el-input v-model="formData.goodsAloneGradeEquity" placeholder="请输入单独设置折扣的配置" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="上下架状态(1上架  2下架)" prop="goodsShelfStatus">
              <el-radio-group v-model="formData.goodsShelfStatus">
                <el-radio v-for="item in  shelf_status " :key="item.dictValue" :label="parseInt(item.dictValue)">{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item :label-width="labelWidth" label="排序" prop="goodsSort">
              <el-input-number v-model.number="formData.goodsSort" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>






      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleEditClick(formRef)">编辑</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import modal from '@/plugins/modal.js'
import { reactive, ref, watch } from "vue";
import {  addOrUpdateGoods } from "@/api/business/GoodsManager/Goodss/goods.js";


// 打开弹窗时回调
const openDialog = async () => {
   await getdeduct_stock_type()
   await getis_points_gift()
   await getis_points_discount()
   await getis_alone_points_discount()
   await getis_enable_grade()
   await getis_alone_grade()
   await getshelf_status()

}

const formData = ref({
  ...props.data,
});
watch(props, async (v) => {
  formData.value = v.data;

});
  
// 业务参数
// 库存计算方式(1下单立减库存 2付款立减库存)字典选项列表
const deduct_stock_type = ref([]);
// 是否赠送积分(1开启 0关闭)字典选项列表
const is_points_gift = ref([]);
// 是否允许使用积分抵扣(1允许 0不允许)字典选项列表
const is_points_discount = ref([]);
// 积分抵扣设置(0默认抵扣 1单独设置抵扣)字典选项列表
const is_alone_points_discount = ref([]);
// 是否开启会员折扣(1开启 0关闭)字典选项列表
const is_enable_grade = ref([]);
// 会员折扣设置(0默认等级折扣 1单独设置折扣)字典选项列表
const is_alone_grade = ref([]);
// 上下架状态(1上架  2下架)字典选项列表
const shelf_status = ref([]);

// -业务方法
//字典获取
async function getdeduct_stock_type() {
  await proxy.getDicts('deduct_stock_type').then((res) => {
    deduct_stock_type.value = res.data
  })
}
//字典获取
async function getis_points_gift() {
  await proxy.getDicts('is_points_gift').then((res) => {
    is_points_gift.value = res.data
  })
}
//字典获取
async function getis_points_discount() {
  await proxy.getDicts('is_points_discount').then((res) => {
    is_points_discount.value = res.data
  })
}
//字典获取
async function getis_alone_points_discount() {
  await proxy.getDicts('is_alone_points_discount').then((res) => {
    is_alone_points_discount.value = res.data
  })
}
//字典获取
async function getis_enable_grade() {
  await proxy.getDicts('is_enable_grade').then((res) => {
    is_enable_grade.value = res.data
  })
}
//字典获取
async function getis_alone_grade() {
  await proxy.getDicts('is_alone_grade').then((res) => {
    is_alone_grade.value = res.data
  })
}
//字典获取
async function getshelf_status() {
  await proxy.getDicts('shelf_status').then((res) => {
    shelf_status.value = res.data
  })
}



// -基础参数
const props = defineProps({
  modelValue: Boolean,
  data: Object,
  done: Function,
});

const labelWidth = 100;
const formRef = ref();
const { proxy } = getCurrentInstance()
const emits = defineEmits(["update:modelValue"]);
const imgData = ref({
  fileDir: "Goods"
})

// 验证
const rules = reactive({
    goodsGuid: [{ required: true, message: "不能为空", trigger: "blur", type: "number" }],
    shopGuid: [{ required: true, message: "店铺guid不能为空", trigger: "blur", type: "number" }],
    goodsCategoryGuid: [{ required: true, message: "商品类目guid不能为空", trigger: "blur", type: "number" }],
    goodsSkuGuid: [{ required: true, message: "商品规格guid不能为空", trigger: "blur", type: "number" }],
    deliveryGuid: [{ required: true, message: "配送模板guid不能为空", trigger: "blur", type: "number" }],
    goodsName: [{ required: true, message: "名称不能为空", trigger: "blur" }],
    goodsCoding: [{ required: true, message: "编码不能为空", trigger: "blur" }],
    goodsPicture: [{ required: true, message: "图片不能为空", trigger: "blur" }],
    goodsPriceLowest: [{ required: true, message: "价格(最低)不能为空", trigger: "blur" }],
    goodsPriceHighest: [{ required: true, message: "价格(最高)不能为空", trigger: "blur" }],
    goodsDashedPriceLowest: [{ required: true, message: "划线价格(最低)不能为空", trigger: "blur" }],
    goodsTotalInventory: [{ required: true, message: "库存总量不能为空", trigger: "blur", type: "number" }],
    goodsSalesInitial: [{ required: true, message: "初始销量不能为空", trigger: "blur", type: "number" }],
    goodsSalesActual: [{ required: true, message: "实际销量不能为空", trigger: "blur", type: "number" }],
    goodsDetails: [{ required: true, message: "详情不能为空", trigger: "blur" }],
    goodsDeductStockType: [{ required: true, message: "库存计算方式(1下单立减库存 2付款立减库存)不能为空", trigger: "blur", type: "number" }],
    goodsIsPointsGift: [{ required: true, message: "是否赠送积分(1开启 0关闭)不能为空", trigger: "blur", type: "number" }],
    goodsIsPointsDiscount: [{ required: true, message: "是否允许使用积分抵扣(1允许 0不允许)不能为空", trigger: "blur", type: "number" }],
    goodsIsAlonePointsDiscount: [{ required: true, message: "积分抵扣设置(0默认抵扣 1单独设置抵扣)不能为空", trigger: "blur", type: "number" }],
    goodsIsEnableGrade: [{ required: true, message: "是否开启会员折扣(1开启 0关闭)不能为空", trigger: "blur", type: "number" }],
    goodsIsAloneGrade: [{ required: true, message: "会员折扣设置(0默认等级折扣 1单独设置折扣)不能为空", trigger: "blur", type: "number" }],
    goodsShelfStatus: [{ required: true, message: "上下架状态(1上架  2下架)不能为空", trigger: "blur", type: "number" }],
    goodsSort: [{ required: true, message: "排序不能为空", trigger: "blur", type: "number" }],
});

// -基础方法
// 提交
const handleEditClick = async (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }


    const { code } = await addOrUpdateGoods(formData.value);
    if (code == 200) {
      modal.msgSuccess('修改成功')
      closeDialog();
    }
  });
}

const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
}
const closeDialog = () => {
  props.done();
  emits("update:modelValue", false);
};

</script>
