<template>
  <el-dialog v-model="dialogVisible" title="添加海报" width="900px" @closed="closeDialog" @open="openDialog">
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-row>


        <el-col :span="12">
          <el-form-item :label-width='labelWidth' label="位置" prop="poster_location">
            <el-select v-model="formData.poster_location" clearable placeholder="请选择">
              <el-option v-for="item in poster_location" :key="item.dictionary_guid" :label="item.dictionary_name"
                :value="item.dictionary_value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span='24'>
          <el-form-item :label-width='labelWidth' label='图片' prop='poster_img'>
            <UploadImage ref='uploadRef' v-model='formData.poster_img' :data=uoloadData :limit='1' :fileSize='20'
              :drag='true' :isShowTip='false' />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="标题" prop="poster_title">
            <el-input v-model='formData.poster_title' type="text" placeholder='请输入标题'></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="描述" prop="poster_describe">
            <el-input v-model='formData.poster_describe' type="text" placeholder='请输入描述'></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="排序" prop="poster_sort">
            <el-input-number v-model='formData.poster_sort' controls-position='right' :min="1"></el-input-number>
          </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 { addPoster, getDictionary } from "~/service/poster";
import { useLoginStore } from "~/store";

// --业务参数



// --业务方法

// 字典获取
const poster_location = ref([]);
async function get_poster_location() {
  await getDictionary({ dictionary_value: 'poster_location' }).then((res) => {
    poster_location.value = res
  })
}




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

const formRef = ref();
const labelWidth = 90;
const props = defineProps({
  modelValue: Boolean,
  done: Function,
});
const emits = defineEmits(["update:modelValue"]);
const dialogVisible = ref(props.modelValue);
const formData = reactive({
  // poster_sort: 1
});

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

watch(props, (v) => {
  dialogVisible.value = v.modelValue;
});

const rules = reactive({
  poster_location: [
    {
      required: true,
      message: '位置不能为空'
    }
  ],
  poster_img: [
    {
      required: true,
      message: '图片不能为空'
    }
  ],

});


// --基础方法

// 打开弹窗时执行
const openDialog = () => {
  get_poster_location()

};

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

const handleAddClick = async (formEl) => {
  console.log(formData);
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }

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

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

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