<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="80%"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="formData"
        label-width="90px"
        :rules="formRules"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="商品名称" prop="name">
              <el-input
                v-model="formData.name"
                clearable
                placeholder="请输入商品名称"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8"
            >
            <el-form-item label="首拼字母" prop="py">
              <el-input
                v-model="formData.py"
                clearable
                placeholder="请输入首拼字母"
              /> </el-form-item
          >
            </el-col> -->
          <el-col :span="8">
            <el-form-item label="商品编号" prop="number">
              <el-input
                v-model="formData.number"
                clearable
                placeholder="请输入商品编号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格型号" prop="spec">
              <el-input
                v-model="formData.spec"
                clearable
                placeholder="请输入规格型号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品分类" prop="class">
              <el-cascader
                style="width: 100%"
                v-model="formData.class"
                :options="classList"
                :props="{
                  value: 'id',
                  label: 'name',
                  emitPath: false,
                }"
                placeholder="请选择商品分类"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品品牌" prop="brand">
              <el-cascader
                style="width: 100%"
                v-model="formData.brand"
                :options="brandList"
                :props="{
                  value: 'id',
                  label: 'name',
                  emitPath: false,
                }"
                placeholder="请选择商品品牌"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品单位" prop="unit">
              <el-cascader
                style="width: 100%"
                v-model="formData.unit"
                :options="unitList"
                :props="{
                  value: 'id',
                  label: 'name',
                  emitPath: false,
                }"
                placeholder="请选择商品单位"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="购货价格" prop="buy">
              <el-input
                v-model="formData.buy"
                clearable
                placeholder="请输入购货价格"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="销货价格" prop="sell">
              <el-input
                v-model="formData.sell"
                clearable
                placeholder="请输入销货价格"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="零售价格" prop="retail">
              <el-input
                v-model="formData.retail"
                clearable
                placeholder="请输入零售价格"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="兑换积分" prop="integral">
              <el-input
                v-model="formData.integral"
                clearable
                placeholder="请输入兑换积分"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="税率" prop="tax_rate">
              <el-input
                v-model="formData.tax_rate"
                clearable
                placeholder="请输入税率"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="条形码" prop="code">
              <el-input
                v-model="formData.code"
                clearable
                placeholder="请输入条形码"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="默认仓库" prop="warehouse">
              <el-cascader
                style="width: 100%"
                v-model="formData.warehouse"
                :options="warehouseList"
                :props="{
                  value: 'id',
                  label: 'name',
                  emitPath: false,
                }"
                placeholder="请选择仓库"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品货位" prop="location">
              <el-input
                v-model="formData.location"
                clearable
                placeholder="请输入商品货位"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="库存阈值" prop="stocktip">
              <el-input
                v-model="formData.stocktip"
                clearable
                placeholder="请输入库存阈值"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="零售名称" prop="retail_name">
              <el-input
                v-model="formData.retail_name"
                clearable
                placeholder="请输入零售名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="扩展信息" prop="more">
              <el-input
                v-model="formData.more"
                clearable
                placeholder="请输入扩展信息"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="备注信息" prop="data">
              <el-input
                v-model="formData.data"
                clearable
                placeholder="请输入备注信息"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排序" prop="sort">
              <el-input
                v-model="formData.sort"
                clearable
                placeholder="请输入排序"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="商品图片" prop="imgs">
              <material-picker v-model="formData.imgs" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="商品详情" prop="details">
              <material-picker v-model="formData.details" :limit="9"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </popup>
  </div>
</template>

<script lang="ts" setup name="goodsEdit">
import type { FormInstance } from "element-plus";
import Popup from "@/components/popup/index.vue";
import { apiGoodsAdd, apiGoodsEdit, apiGoodsDetail } from "@/api/goods";
import { apiGoodsclassLists } from "@/api/goodsclass";
import { apiBrandLists } from "@/api/brand";
import { apiUnitLists } from "@/api/unit";
import { apiWarehouseLists } from "@/api/warehouse";
import { timeFormat } from "@/utils/util";
import type { PropType } from "vue";
defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({}),
  },
});
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");

// 弹窗标题
const popupTitle = computed(() => {
  return mode.value == "edit" ? "编辑商品表" : "新增商品表";
});

// 表单数据
const formData = reactive({
  id: "",
  name: "",
  py: "",
  number: "",
  spec: "",
  class: "",
  brand: "",
  unit: "",
  buy: "",
  sell: "",
  retail: "",
  integral: "",
  code: "",
  tax_rate: "",
  warehouse: "",
  location: "",
  stocktip: "",
  retail_name: "",
  imgs: "",
  details: "",
  data: "",
  more: "",
  sort: "",
});

// 表单验证
const formRules = reactive<any>({
  name: [
    {
      required: true,
      message: "请输入商品名称",
      trigger: ["blur"],
    },
  ],
  py: [
    {
      required: true,
      message: "请输入首拼字母",
      trigger: ["blur"],
    },
  ],
  class: [
    {
      required: true,
      message: "请输入商品分类",
      trigger: ["blur"],
    },
  ],
  brand: [
    {
      required: true,
      message: "请输入商品品牌",
      trigger: ["blur"],
    },
  ],
  unit: [
    {
      required: true,
      message: "请输入商品单位",
      trigger: ["blur"],
    },
  ],
});

const classList = ref([]);
const getClassList = () => {
  apiGoodsclassLists({}).then((res) => {
    classList.value = res.lists;
  });
};
getClassList();

const brandList = ref([]);
const getBrandList = () => {
  apiBrandLists({}).then((res) => {
    brandList.value = res.lists;
  });
};
getBrandList();

const unitList = ref([]);
const getUnitList = () => {
  apiUnitLists({}).then((res) => {
    unitList.value = res.lists;
  });
};
getUnitList();

const warehouseList = ref([]);
const getWarehouseList = () => {
  apiWarehouseLists({}).then((res) => {
    warehouseList.value = res.lists;
  });
};
getWarehouseList();

// 获取详情
const setFormData = async (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key];
    }
  }
};

const getDetail = async (row: Record<string, any>) => {
  const data = await apiGoodsDetail({
    id: row.id,
  });
  setFormData(data);
};

// 提交按钮
const handleSubmit = async () => {
  await formRef.value?.validate();
  const data = { ...formData };
  mode.value == "edit" ? await apiGoodsEdit(data) : await apiGoodsAdd(data);
  popupRef.value?.close();
  emit("success");
};

//打开弹窗
const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};

// 关闭回调
const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
  getDetail,
});
</script>
