<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 :span="12">
          <el-form-item label="轮播图位置" prop="banner_location">
            <el-select v-model="formData.banner_location" clearable placeholder="请选择">
              <el-option v-for="item in banner_location" :key="item.dictionary_guid" :label="item.dictionary_name"
                :value="item.dictionary_value"></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='banner_img'>
            <UploadImage ref='uploadRef' v-model='formData.banner_img' :data=uoloadData :limit='1' :fileSize='5'
              :drag='true' :isShowTip='false' />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="排序" prop="banner_order">
            <el-input-number v-model='formData.banner_order' 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="handleEditClick(formRef)">编辑</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, watch } from "vue";
import { editBanner, getDictionary } from "~/service/banner";
import { useLoginStore } from "~/store";

// --业务参数



// --业务方法

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




// --基础参数
const store = useLoginStore();
const headers = {
  Accept: "application/json",
  ...store.headers,
};
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: "Banner"
})


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


});

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

};

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

const rules = reactive({
  // banner_img: [
  //   {
  //     required: true,
  //     message: '轮播图图片不能为空'
  //   }
  // ],
  banner_location: [
    {
      required: true,
      message: '轮播图片位置(字典)不能为空'
    }
  ],
  banner_order: [
    {
      required: true,
      message: '排序不能为空'
    }
  ],

});

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

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

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