<template>
  <el-container>
    <el-main v-loading="loading" :element-loading-text="t('common.loading')">
      <el-form :model="form.model" :rules="form.rules" ref="formRef" :label-width="formLabelWidth">
        <el-form-item :label="t('modules.banner.bannerTitle')" prop="title">
          <el-input v-model="form.model.title" :placeholder="t('modules.banner.enterBannerTitle')" maxlength="50"
            show-word-limit />
        </el-form-item>

        <el-form-item :label="t('modules.banner.bannerImage')" prop="image">
          <sa-upload-image v-model="imageArray" :max-count="1" :accept="['jpg', 'jpeg', 'png']" :max-size="5"
            :direct-upload="true" :width="375" :height="200" :placeholder="t('modules.banner.uploadImage')" />
        </el-form-item>

        <el-form-item :label="t('modules.banner.linkType')" prop="linkType">
          <el-radio-group v-model="form.model.linkType">
            <el-radio :label="0">{{ t('modules.banner.internalLink') }}</el-radio>
            <el-radio :label="1">{{ t('modules.banner.externalLink') }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item :label="t('modules.banner.linkAddress')" prop="link">
          <el-input v-model="form.model.link" :placeholder="t('modules.banner.enterLinkAddress')" maxlength="200"
            show-word-limit />
        </el-form-item>

        <el-form-item :label="t('common.sort')" prop="seq">
          <el-input v-model="form.model.seq" :placeholder="t('common.enterSort')" />
        </el-form-item>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button v-if="modal.params.type == 'add'" type="primary" @click="confirm">{{ t('common.save') }}</el-button>
      <el-button v-if="modal.params.type == 'edit'" type="primary" @click="confirm">{{ t('common.update') }}</el-button>
    </el-footer>
  </el-container>
</template>

<script setup>
import { onMounted, reactive, ref, unref, computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { useFormConfig } from '@/hooks/useFormConfig';
import api from '../../api';
import { cloneDeep } from 'lodash';

const { t } = useI18n();

// 使用表单配置hooks
const { formLabelWidth } = useFormConfig({ enWidth: '140px' });

const emit = defineEmits(['modalCallBack']);
const props = defineProps({
  modal: {
    type: Object,
  },
});

// 添加 编辑 form
let formRef = ref(null);
const loading = ref(false);

// 图片数组和字符串的双向绑定
const imageArray = computed({
  get() {
    return form.model.image ? [form.model.image] : [];
  },
  set(value) {
    form.model.image = value.length > 0 ? value[0] : '';
  },
});

const form = reactive({
  model: {
    title: '',
    image: '',
    linkType: 0,
    link: '',
    seq: 0,
  },
  rules: {
    title: [{ required: true, message: t('modules.banner.titleRequired'), trigger: 'blur' }],
    image: [{ required: true, message: t('modules.banner.imageRequired'), trigger: 'change' }],
    link: [{ required: true, message: t('modules.banner.linkRequired'), trigger: 'blur' }],
  },
});

// 获取详情
async function getDetail(id) {
  loading.value = true;
  const { code, data } = await api.banner.detail(id);
  if (code == 200) {
    form.model = data;
  }
  loading.value = false;
}

// 表单关闭时提交
async function confirm() {
  unref(formRef).validate(async (valid) => {
    if (!valid) return;
    let submitForm = cloneDeep(form.model);
    if (props.modal.params.type == 'edit') {
      submitForm.id = props.modal.params.id;
    }

    const { code } =
      props.modal.params.type == 'add'
        ? await api.banner.add(submitForm)
        : await api.banner.edit(submitForm);

    if (code == 200) {
      emit('modalCallBack', { event: 'confirm' });
    }
  });
}

async function init() {
  if (props.modal.params.id) {
    await getDetail(props.modal.params.id);
  }
}

onMounted(() => {
  init();
});
</script>

<style lang="scss" scoped>
.banner-edit {
  .form-tip {
    margin-top: 4px;
    font-size: 12px;
    color: #909399;
    line-height: 1.4;
  }
}
</style>
