<template>
  <el-dialog v-model="props.modelValue" title="编辑产品" width="900px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-row>
        <el-col :lg="12">
          <el-form-item :label-width="labelWidth" label="产品类目" prop="product_type_guid">
            <el-cascader class="w100" filterable :options="dataList"
              :props="{ checkStrictly: false, value: 'product_type_guid', label: 'product_type_name', emitPath: false }"
              placeholder="请选择产品类目" clearable v-model="formData.product_type_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-col :span="12">
          <el-form-item :label-width="labelWidth" label="语言类型" prop="i18n_lang_type">
            <el-select v-model="formData.i18n_lang_type_guid" clearable placeholder="请选择">
              <el-option v-for="item in i18n_lang_type" :key="item.i18n_lang_type_guid"
                :label="item.i18n_lang_type_code" :value="item.i18n_lang_type_guid"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>

        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="产品名称" prop="product_name">
            <el-input v-model='formData.product_name' type="text" placeholder='请输入产品名称'></el-input>
          </el-form-item>
        </el-col>
        <el-col :span='24'>
          <el-form-item :label-width='labelWidth' label='产品图片' prop='product_img'>
            <UploadImage ref='uploadRef' v-model='formData.product_img' :data=uoloadData :limit='5' :fileSize='5'
              :drag='true' :isShowTip='true' />
          </el-form-item>
        </el-col>

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

        <el-col :span="12">
          <el-form-item :label-width="labelWidth + 20" label="是否为热门产品" prop="">
            <el-switch v-model="formData.product_is_hot" class="ml-2" :active-value=1 :inactive-value=0
              style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="跳转链接" prop="product_link">
            <el-input v-model='formData.product_link' type="text" placeholder='请输入跳转链接'></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item :label-width="labelWidth" label="产品描述" prop="product_description">
            <el-input v-model='formData.product_description' type="textarea" :rows="5" placeholder='请输入产品描述'></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item :label-width="labelWidth" label="产品详情" prop="product_details">
            <RichText v-model='formData.product_details' :min-height='196'></RichText>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleEditClick(formRef)" :loading="isBtnLod">保存</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
        <el-button @click="closeDialog()">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, watch } from "vue";
import { editProduct } from "~/service/product";
import { getProductTypeTree } from "~/service/product_type";
import { getI18nLangTypeList } from '~/service/i18n_lang_type';
import { useLoginStore } from "~/store";

// --业务参数



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

const i18n_lang_type = ref();
async function geti18n_lang_typeList() {
  getI18nLangTypeList({ limit: 9999 }).then((res) => {
    if (res.code == 0) {
      i18n_lang_type.value = res.data
    }
  })
}


// --基础参数
const store = useLoginStore();
const headers = {
  Accept: "application/json",
  ...store.headers,
};
const isBtnLod = ref(false);
const formRef = ref();
const labelWidth = 100;
const props = defineProps({
  modelValue: Boolean,
  data: Object,
  done: Function,
});
const emits = defineEmits(["update:modelValue"]);
const formData = ref({
  ...props.data,
});
const uoloadData = ref({
  dirName: "Product"
})


// --基础方法
watch(props, (v) => {
  formData.value = v.data;


});

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

const closeDialog = () => {
  props.done();
  emits("update:modelValue", false);
};

const rules = reactive({
  product_type_guid: [
    {
      required: true,
      message: '产品类目不能为空'
    }
  ],
  product_name: [
    {
      required: true,
      message: '产品名称不能为空'
    }
  ],
  product_img: [
    {
      required: true,
      message: '产品图片不能为空'
    }
  ],
  product_sort: [
    {
      required: true,
      message: '产品排序不能为空'
    }
  ],

});

const handleEditClick = async (formEl) => {
  console.log(formData.value);
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }
    isBtnLod.value = true;


    const { code } = await editProduct(formData.value);
    if (code == 0) {
      closeDialog();
      props.done();
    }
    isBtnLod.value = false;
  });
};
const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

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