<template>
  <div class="edit-popup">
    <popup
        ref="popupRef"
        :title="popupTitle"
        :async="true"
        width="550px"
        :clickModalClose="true"
        @confirm="handleSubmit"
        @close="handleClose"
    >
      <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
        <el-form-item label="标题" prop="title">
          <el-input v-model="formData.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="是否显示" prop="isShow">
          <el-radio-group class="flex-1" v-model="formData.isShow" placeholder="请选择是否显示">
            <el-radio :label="1">显示</el-radio>
            <el-radio :label="0">不显示</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="banner图" prop="img">
          <material-picker  size="100px"  v-model="formData.img" type="video" />
        </el-form-item>
        <el-form-item label="排序" prop="menuSort">
          <div>
            <el-input-number v-model="formData.sort" :max="9999" />
            <div class="form-tips">数值越大越排前</div>
          </div>
        </el-form-item>
        <el-form-item label="链接地址" prop="linkAddress">
          <el-input v-model="formData.linkAddress" placeholder="请输入链接地址" />
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  bannerEdit, bannerAdd, bannerDetail } from '@/api/banner'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({})
  }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑banner信息' : '新增banner信息'
})


const formData = reactive({
  id: null,
  title: '',
  sort: 0,
  isShow: 0,
  img: '',
  linkAddress: '',
})

const formRules = {
  id: [
    {
      required: true,
      message: '请输入主键',
      trigger: ['blur']
    }
  ],
  title: [
    {
      required: true,
      message: '请输入标题',
      trigger: ['blur']
    }
  ],
  sort: [
    {
      required: true,
      message: '请输入排序编号',
      trigger: ['blur']
    }
  ],
  isShow: [
    {
      required: true,
      message: '请选择是否显示',
      trigger: ['blur']
    }
  ],
  img: [
    {
      required: true,
      message: '请选择banner图',
      trigger: ['blur']
    }
  ],
  linkAddress: [
    {
      required: true,
      message: '请输入链接地址',
      trigger: ['blur']
    }
  ],
}

const handleSubmit = async () => {
  await formRef.value?.validate()
  const data: any = { ...formData }
  console.log(data)
  mode.value == 'edit' ? await bannerEdit(data) : await bannerAdd(data)
  popupRef.value?.close()
  feedback.msgSuccess('操作成功')
  emit('success')
}

const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }
}

const getDetail = async (row: Record<string, any>) => {
  const data = await bannerDetail({
    id: row.id
  })
  setFormData(data)
}


const handleClose = () => {
  emit('close')
}

defineExpose({
  open,
  setFormData,
  getDetail
})
</script>
