<template>
  <el-form :model="formCallback" label-width="120px" ref="form" v-loading="loading" class="pr-4">
    <el-form-item label="小程序类型" prop="miniprogram_type" :rules="[{ required: true, message: '选择小程序类型' }]"
      label-width="120px">
      <el-select v-model="formCallback.miniprogram_type" clearable filterable placeholder="请选择小程序类型">
        <el-option v-for="(item, index) in miniprogramTypelist" :key="index" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item label="轮播图名称" prop="title" :rules="[{ required: true, message: '轮播图名称必须填写' }]" label-width="120px">
      <el-input v-model="formCallback.title" size="default" clearable></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort" :rules="[{ required: true, message: '排序必须填写' }]" label-width="120px">
      <el-input v-model.number="formCallback.sort" min="1" type="number" size="default" clearable></el-input>
    </el-form-item>
    <el-form-item label="封面图" prop="img_url" :rules="[{ required: true, message: '请上传封面图' }]">
      <my-upload acceptType=".png,.jpg,.jpeg" :isCheckMM="true" :isMultiple="true" @fileRemove="fileRemove"
        @fileSuccess="fileSuccess" :fileList="formCallback.fileList" action="/qiniu/upload/image"></my-upload>
    </el-form-item>
    <el-form-item label="链接短剧信息" prop="duanjus" :rules="[{ required: false, message: '选择链接短剧信息', trigger: 'change' }]">
      <div>
        <div class="flex flex-wrap items-center mb-5" v-if="formCallback?.duanjus?.name">
          <el-input v-model="formCallback.duanjus.name" disabled placeholder="短剧名称" style="width:200px;"
            class="input-with-select">
          </el-input>
          <el-icon class="ml-3 cursor-pointer" @click="chooseDel">
            <Delete />
          </el-icon>
        </div>
        <div>
          <el-button type="primary" size="default" @click="chooseShort">选择短剧</el-button>
        </div>
      </div>
    </el-form-item>
    <el-form-item label="状态" prop="status" label-width="120px" :rules="[{ required: true, message: '选择状态' }]">
      <el-switch v-model="formCallback.status" :active-value="1" :inactive-value="0"></el-switch>
    </el-form-item>
    <div class="flex justify-end">
      <el-button type="primary" @click="submitForm(form)">{{
        $t('system.confirm')
      }}</el-button>
    </div>
  </el-form>
  <Dialog v-model="videoVisible" title="选择短剧" width="90%" :alignCenter="true" destroy-on-close>
    <videoList @confirm="confirm" :primary="formCallback.duanjus"></videoList>
  </Dialog>
</template>

<script lang="ts" setup>
import videoList from './videoList.vue';
import { Close, Delete } from '@element-plus/icons-vue';
import { FormInstance } from 'element-plus';
import {
  operationBannerEdit,
  operationBannerAdd
} from '@/api/pageLayout/viewpagerPage/index';
import { useRouter, useRoute } from 'vue-router'
import { optionsCommonParams } from '@/api/common/index'
const props = defineProps({
  primary: Object,
});
const router = useRouter()
const route = useRoute()
const emit = defineEmits(['close']);
const loading = ref(false)
const videoVisible = ref(false)
const promotion = ref('')
const form = ref()
const formCallback = ref({})
const miniprogramTypelist = ref([])
const init = () => {
  optionsCommonParams().then(res => {
    console.log(res, 'optionsCommonParams');
    miniprogramTypelist.value = res.data.miniprogramType
  })
}

const chooseDel = () => {
  formCallback.value.duanjus = ''
}
const fileSuccess = (e) => {
  console.log(e);
  formCallback.value.img_url = e;
}
const fileRemove = (e) => {
  console.log(e);
  formCallback.value.img_url = '';
}
const chooseShort = () => {
  videoVisible.value = true
}

const confirm = (e) => {
  formCallback.value.duanjus = e
  videoVisible.value = false
}
// 提交回传配置
const submitForm = (formEl: FormInstance | undefined) => {
  console.log(formCallback.value, 'formCallbackformCallback');
  if (!formEl) return;
  loading.value = true;
  formEl
    .validate(valid => {
      if (valid) {
        const params = {
          title: formCallback.value.title,
          video_id: formCallback.value.duanjus?.id,
          miniprogram_type: formCallback.value.miniprogram_type,
          sort: formCallback.value.sort,
          img_url: formCallback.value.img_url,
          status: formCallback.value.status,
        }
        if (props.primary?.id) {
          operationBannerEdit(formCallback.value.id, params).then(res => {
            ElMessage.success(res.message)
            emit('close')
          })
        } else {
          operationBannerAdd(params).then(res => {
            ElMessage.success(res.message)
            emit('close')
          })
        }
        loading.value = false;
      } else {
        loading.value = false;
      }
    })
    .then(() => { });
}

if (props.primary.id) {
  console.log(props.primary, 'props.primaryprops.primary');
  formCallback.value = JSON.parse(JSON.stringify(props.primary))
  formCallback.value.duanjus = JSON.parse(JSON.stringify(props.primary.videoInfo))
  formCallback.value.fileList = [{ uid: props.primary.id, url: props.primary.img_url }]
} else {
  formCallback.value.fileList = []
}
onMounted(() => {
  init()
});
</script>

<style lang="scss" scoped>
:deep(.input-with-select .el-input-group__prepend) {
  background-color: #FFFFFF;
  padding: 0;
  border: 1px solid #dcdfe6;
}
</style>
