<script setup lang="ts">
import { FormRules, ElForm } from "element-plus";
import { reactive, ref } from "vue";
import { httpUpdate, httpAdd, httpDetail } from "/@/api/parameter/video";
import { responseHandle } from "/@/utils/responseHandle";
import { useNav } from "/@/layout/hooks/useNav";
import { ImageUpload } from "/@/components/Upload";

enum FROM_TYPE {
  create = "create",
  edit = "edit",
  view = "view"
}
const { logout } = useNav();
const showModel = ref(false);
const currentMenuId = ref("");
const TYPE = ref<FROM_TYPE>(FROM_TYPE.create);
const formRef = ref<InstanceType<typeof ElForm>>(null);
const loading = ref(false);
const titleType = ref("");
const id = ref("");
const emit = defineEmits(["reload"]);
const initform = {
  video_img: "",
  video_name: "",
  id: "",
  video_url: "",
  remark: ""
};
const ruleForm = ref({ ...initform });
const rules = reactive<FormRules>({
  video_img: [{ required: true, trigger: "change", message: "请选择封面图" }],
  video_name: [{ required: true, trigger: "bulr", message: "请输入视频名称" }],
  video_url: [
    { required: true, trigger: "bulr", message: "请输入视频链接" },
    {
      validator: (rule, value, callback) => {
        if (value === "") {
          callback(new Error("12"));
        } else {
          callback();
        }
      }
    }
  ],
  remark: [{ required: true, trigger: "bulr", message: "请输入视频描述" }]
});
const initData = async () => {
  const { code, data, message } = await httpDetail({ id: id.value });
  responseHandle({
    code,
    message,
    logout,
    handler: () => {
      console.log(data);
      Object.keys(ruleForm.value).forEach(key => {
        ruleForm.value[key] = data[key] ? data[key] + "" : "";
      });
    }
  });
};
async function show(node: any, did: string, isCreate: string) {
  id.value = did;
  Object.keys(ruleForm.value).forEach(key => {
    ruleForm.value[key] = initform[key] ? initform[key] + "" : "";
  });
  TYPE.value = isCreate;
  switch (TYPE.value) {
    case "create":
      titleType.value = "新建视频";
      break;
    case "edit":
      titleType.value = "编辑视频";
      break;
    case "view":
      titleType.value = "视频详情";
      break;
    default:
      titleType.value = "新建视频";
  }
  if (TYPE.value !== "create") {
    await initData();
  }

  showModel.value = true;
}

function handleUpdate() {
  const data = {
    ...ruleForm.value
  };

  return {
    data,
    api: httpUpdate
  };
}

function handleCreate() {
  const data = {
    ...ruleForm.value
  };

  return {
    data,
    api: httpAdd
  };
}

function handleSave() {
  formRef.value.validate(async vaild => {
    if (vaild) {
      if (loading.value) return;
      const handler =
        TYPE.value === FROM_TYPE.create ? handleCreate : handleUpdate;

      loading.value = true;
      const { api, data } = handler();
      const { message, code } = await api(data);
      loading.value = false;
      responseHandle({
        code,
        message,
        logout,
        handler: () => {
          showModel.value = false;
          emit("reload");
        }
      });
    }
  });
}

defineExpose({
  show,
  changeCurrentMenuId: (id: string) => (currentMenuId.value = id)
});
</script>

<template>
  <el-dialog
    v-model="showModel"
    :close-on-press-escape="false"
    center
    append-to-body
    destroy-on-close
    :width="'800px'"
    :title="titleType"
    v-loading="loading"
  >
    <el-form
      ref="formRef"
      :model="ruleForm"
      :rules="rules"
      label-width="90px"
      style="margin-top: -10px"
      class="demo-ruleForm"
      status-icon
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="封面图" prop="video_img">
            <ImageUpload
              v-model:url="ruleForm.video_img"
              :disabled="TYPE === 'view'"
              :descs="['小于1Mb']"
              :types="['png', 'jpg', 'jpeg']"
            /> </el-form-item
        ></el-col>
        <el-col :span="24">
          <el-form-item label="视频名称" prop="video_name">
            <ElInput
              placeholder="视频名称"
              :disabled="TYPE === 'view'"
              maxlength="50"
              v-model="ruleForm.video_name"
            /> </el-form-item
        ></el-col>
        <el-col :span="24">
          <el-form-item label="视频链接" prop="video_url">
            <ElInput
              placeholder="视频链接"
              :disabled="TYPE === 'view'"
              maxlength="50"
              v-model="ruleForm.video_url"
            /> </el-form-item
        ></el-col>
        <el-col :span="24">
          <el-form-item label="视频描述" prop="remark">
            <ElInput
              placeholder="视频描述"
              :disabled="TYPE === 'view'"
              type="textarea"
              maxlength="600"
              v-model="ruleForm.remark"
            /> </el-form-item
        ></el-col>
      </el-row>
      <div class="flex justify-end">
        <el-button
          v-if="TYPE !== 'view'"
          :loading="loading"
          :disabled="loading"
          type="primary"
          @click="handleSave"
          >保存</el-button
        >
        <el-button @click="showModel = false">取消</el-button>
      </div>
    </el-form>
  </el-dialog>
</template>
