<template>
  <el-dialog v-model="dialogVisible" title="添加产品类目" width="600px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-row>
        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="上级菜单" prop="product_type_parent_guid">
            <el-cascader class="w100" filterable :options="dataList"
              :props="{ checkStrictly: true, value: 'product_type_guid', label: 'product_type_name', emitPath: false }"
              placeholder="请选择上级菜单" clearable v-model="formData.product_type_parent_guid">
              <template #default="{ node, data }">
                <span>{{ data.product_type_name }}</span>
                <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
              </template>
            </el-cascader>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item :label-width="labelWidth" label="类目名称" prop="product_type_name">
            <el-input v-model='formData.product_type_name' placeholder='请输入类目名称'></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item :label-width="labelWidth" label="类目标题" prop="">
            <el-input v-model='formData.product_type_title' placeholder='请输入类目标题'></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item :label-width="labelWidth" label="类目链接" prop="">
            <el-input v-model='formData.product_type_link' placeholder='请输入类目链接'></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="排序" prop="product_type_order">
            <el-input-number v-model='formData.product_type_order' controls-position='right'></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span='12'>
          <el-form-item :label-width='labelWidth' label='类目图标' prop='product_type_icon'>
            <UploadImage ref='uploadRef' v-model='formData.product_type_icon' :data=uoloadData :limit='1' :fileSize='5'
              :drag='true' :isShowTip='false' />
          </el-form-item>
        </el-col>

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

    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleAddClick(formRef)">添加</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, watch } from "vue";
import { addProductType, getProductTypeTree } from "~/service/product_type";
import { useLoginStore } from "~/store";

// --业务参数



// --业务方法
const dataList = ref();
async function getList() {
  getProductTypeTree().then((res) => {
    if (res.code == 0) {
      dataList.value = res.data
    }
  })
}




// --基础参数
const store = useLoginStore();
const headers = {
  Accept: "application/json",
  ...store.headers,
};

const formRef = ref();
const labelWidth = 90;
const props = defineProps({
  modelValue: Boolean,
  data: Object,
  done: Function,
});
const emits = defineEmits(["update:modelValue"]);
const dialogVisible = ref(props.modelValue);
const formData = reactive({
  product_type_parent_guid: '0',
  product_type_order: 100,
});

const uoloadData = ref({
  dirName: "ProductType"
})

watch(props, (v) => {
  dialogVisible.value = v.modelValue;
  formData.product_type_parent_guid = v.data.product_type_guid
});

const rules = reactive({
  product_type_name: [
    {
      required: true,
      message: '产品类目名称不能为空'
    }
  ],

});


// --基础方法

// 打开弹窗时执行
const openDialog = () => {
  getList();
};

const closeDialog = () => {
  handleResetClick(formRef.value);
  dialogVisible.value = false;
  emits("update:modelValue", false);
};

const handleAddClick = async (formEl) => {

  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }

    const { code } = await addProductType(formData);
    if (code == 0) {
      closeDialog();
      props.done();
    }
  });
};

const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style lang="less" scoped></style>
