<template>
  <view class="container">
    <h1 style="font-weight: bold">不干胶</h1>
    <el-form>
      <el-form-item>
        <el-radio-group v-model="data.types" @change="handleChange">
          <el-radio label="常用种类" value="常用种类" size="large" border />
          <el-radio label="专版打印" value="专版打印" size="large" border />
        </el-radio-group>
      </el-form-item>
      <view class="form-title">品种</view>
      <el-form-item>
        <el-select v-model="data.material" placeholder="请选择" size="large" style="width: 300px">
          <template v-for="item in materials" :key="item.name">
            <el-option :label="item.name" :value="item.name"></el-option>
          </template>
        </el-select>
      </el-form-item>
      <view class="form-title">尺寸</view>
      <el-form-item>
        <el-input style="width: 150px" v-model="data.size[0]" type="number">
          <template #append> <text>cm</text></template>
        </el-input>
        <text style="margin: 0 10px">X</text>
        <el-input style="width: 150px" v-model="data.size[1]" type="number">
          <template #append> <text>cm</text> </template>
        </el-input>
      </el-form-item>
      <view class="form-title">个数</view>
      <el-form-item>
        <el-input style="width: 300px" v-model="data.count" type="number" placeholder="请输入数量" size="large"></el-input>
      </el-form-item>
      <view class="form-title">款数</view>
      <el-form-item>
        <el-input style="width: 300px" v-model="data.number" type="number" placeholder="请输入数量" size="large"></el-input>
      </el-form-item>
      <view class="form-title">客户旺旺</view>
      <el-form-item>
        <el-input style="width: 300px" v-model="data.customerWw" placeholder="请输入客户旺旺" size="large"></el-input>
      </el-form-item>
      <component v-if="currentCraftComponent" :is="currentCraftComponent" ref="componentRef" />
      <el-button type="primary" @click="handlesave">保存</el-button>
    </el-form>
  </view>
</template>
<script setup>
import { ref, watch, shallowRef } from "vue";
import copperplate from "./crafts/copperplate.vue";
import pvc from "./crafts/pvc.vue";
import transparent from "./crafts/transparent.vue";

const materials = [
  { name: "铜板纸不干胶", images: "" },
  { name: "pvc不干胶", images: "" },
  { name: "透明不干胶", images: "" },
  { name: "牛皮纸", images: "" },
  { name: "哑金不干胶", images: "" },
  { name: "哑银不干胶", images: "" },
  { name: "书写纸不干胶", images: "" },
  { name: "银平光", images: "" },
  { name: "拉丝金", images: "" },
  { name: "拉丝银", images: "" },
  { name: "美纹纸", images: "" },
  { name: "PP合成纸", images: "" },
  { name: "易碎纸不干胶", images: "" },
  { name: "刚古水纹超白", images: "" },
  { name: "红底散金", images: "" },
  { name: "布纹纸超白", images: "" },
  { name: "珠光超白", images: "" },
  { name: "10C静电膜", images: "" },
  { name: "树纹纸", images: "" },
  { name: "草香纸/大地纸", images: "" },
  { name: "单防热敏纸（底纸白色）", images: "" },
  { name: "三防热敏纸（底纸蓝色）", images: "" },
];

const craftMap = {
  铜板纸不干胶: copperplate,
  pvc不干胶: pvc,
  透明不干胶: transparent,
};
const currentCraftComponent = shallowRef(null);
const componentRef = ref(null);

const handlesave = () => {
  console.log(data.value);
  if (typeof componentRef.value?.verifyCraftData === "function") {
    let { valid, message } = componentRef.value.verifyCraftData();
    if (!valid) {
      console.error("工艺数据验证失败:", message);
      return;
    }
  }
};
const data = ref({
  types: "常用种类",
  material: "",
  size: [],
  count: null,
  number: 1,
  customerWw: "",
  craft: [],
});

watch(
  () => data.value.material,
  (mat) => {
    currentCraftComponent.value = craftMap[mat] || null;
    data.value.craft = [];
  },
);

const handleChange = (val) => {
  const initData = {
    types: val,
    material: "",
    size: [],
    count: null,
    number: 1,
    customerWw: "",
    craft: [],
  };
  data.value = initData;
};
</script>
<style lang="scss" scoped>
.el-form-item--default {
  margin-bottom: 10px;
}
.container {
  margin: 0 20px;
  display: flex;
  flex-direction: column;
}
.form-title {
  display: block;
  font-weight: bold;
  font-size: 16px;
  margin-bottom: 5px;
}
</style>
