<!--
 * @Descripttion: (表情包/tb_emoticon_data 添加弹窗)
 * @version: (1.0)
 * @Author: (lwh)
 * @Date: (2023-10-28)
 * @LastEditors: (lwh)
 * @LastEditTime: (2023-10-28)
-->
<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="表情包分类" prop="emotionCategoryGuid">
            <el-cascader class="w100" :options="emoticonCategoryList"
              :props="{ checkStrictly: true, value: 'emotionCategoryGuid', label: 'emotionCategoryName', emitPath: false }"
              placeholder="请选择表情包分类" clearable v-model="formData.emoticonCategoryGuid">
              <template #default="{ node, data }">
                <span>{{ data.emotionCategoryName }}</span>
                <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
              </template>
            </el-cascader>
          </el-form-item>
        </el-col>

        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="获取码" prop="getNum">
            <el-input v-model="formData.getNum" placeholder="请输入获取码" />
          </el-form-item>
        </el-col>
 
        <el-col :lg="24">
          <el-form-item :label-width="labelWidth" label="图片" prop="emoticonDataImg">
            <UploadImage ref="uploadRef" v-model="formData.emoticonDataImg" :data=imgData :limit="100" :fileSize="15"
              :drag="true" />
          </el-form-item>
        </el-col>
   

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

    <template #footer>
      <div key="dialog-footer">
        <el-button type="primary" @click="handleAddClick(formRef)" :loading="loadingStatus">添加</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
      </div>
    </template>
  </el-dialog>
</template>


<script setup>
import { reactive, ref, watch } from "vue";
import { ElMessage } from 'element-plus'
import modal from '@/plugins/modal.js'
import { addOrUpdateEmoticonData,batchAddEmoticonData } from '@/api/business/EmotionManage/EmoticonDatas/emoticonData.js';
import { emotionCategoryTreeList } from '@/api/business/EmotionManage/EmotionCategorys/emotionCategory.js';


// 打开弹窗时回调
const openDialog = async () => {
  getTreeList()

}

// 业务参数
let emoticonCategoryList = ref([])

// 业务方法
async function getTreeList() {
  emotionCategoryTreeList().then((res) => {
    if (res.code == 200) {
      emoticonCategoryList.value = res.data
    }
  })
}


// -基础参数
const loadingStatus = ref(false)
const labelWidth = 100;
const formRef = ref();
const { proxy } = getCurrentInstance()
const emits = defineEmits(["update:modelValue"]);
const formData = reactive({
  emoticonDataSort: 100
});
const props = defineProps({
  modelValue: Boolean,
  done: Function,
});
const imgData = ref({
  fileDir: "EmoticonData"
})

// 验证
const rules = reactive({
  emoticonCategoryGuid: [{ required: true, message: "分类guid不能为空", trigger: "blur", type: "number" }],
  emoticonDataImg: [{ required: true, message: "图片不能为空", trigger: "blur" }],
});

// -基础方法

// 提交
const handleAddClick = async (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }
    loadingStatus.value = true


    const { code } = await batchAddEmoticonData(formData);
    if (code == 200) {
      modal.msgSuccess('批量添加成功')
      closeDialog();
      loadingStatus.value = false
    }
  });
};
const closeDialog = () => {
  handleResetClick(formRef.value);
  props.done();
  emits("update:modelValue", false);
};
const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>