<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-width="labelWidth" label="语言类型" prop="i18n_lang_type">
            <el-select v-model="formData.i18n_lang_type_guid" clearable placeholder="请选择">
              <el-option v-for="item in i18n_lang_type" :key="item.i18n_lang_type_guid"
                :label="item.i18n_lang_type_code" :value="item.i18n_lang_type_guid"></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='home_video_url'>
            <UploadImage ref='uploadRef' v-model='formData.home_video_cover' :data=uoloadData :limit='1' :fileSize='5'
              :drag='true' :isShowTip='true' />
          </el-form-item>
        </el-col>
        <el-col :span='12'>
          <el-form-item :label-width='labelWidth' label='视频' prop='home_video_url'>
            <!-- <UploadVideo ref='uploadRef1' v-model='formData.home_video_url' /> -->
            <UploadVideo ref='uploadRef1' v-model='formData.home_video_url' :data=uoloadData :limit='1' :fileSize='100'
              :drag='true' :isShowTip='true' :fileType="['mp4', 'mkv']" />
          </el-form-item>
        </el-col>


      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleEditClick(formRef)" :loading="isBtnLod">保存</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
        <el-button @click="closeDialog()">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, watch } from "vue";
import { editHomeVideo } from "~/service/home_video";
import { useLoginStore } from "~/store";
import { getI18nLangTypeList } from '~/service/i18n_lang_type';

// --业务参数



// --业务方法

const i18n_lang_type = ref();
async function geti18n_lang_typeList() {
  getI18nLangTypeList({ limit: 9999 }).then((res) => {
    if (res.code == 0) {
      i18n_lang_type.value = res.data
    }
  })
}




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


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


});

// 打开弹窗时执行
const openDialog = async () => {
  await geti18n_lang_typeList()
};

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

const rules = reactive({
  i18n_lang_type_guid: [
    {
      required: true,
      message: '语言类型guid不能为空'
    }
  ],
  home_video_url: [
    {
      required: true,
      message: '视频不能为空'
    }
  ],

});

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


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

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