<script setup lang="ts">
import { FormRules, ElForm } from "element-plus";
import { reactive, ref, unref } from "vue";
import { httpAdd } from "/@/api/operate/batchSetVideo";
import { responseHandle } from "/@/utils/responseHandle";
import { useNav } from "/@/layout/hooks/useNav";
import { ComCard, Video } from "/@/components/RemoteSelect";
enum FROM_TYPE {
  create = "create",
  edit = "edit",
  view = "view"
}
const { logout } = useNav();
const showModel = ref(false);
const Video_id = 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 = {
  group_id: "",
  video_ids: []
};
const ruleForm = ref({ ...initform });
const rules = reactive<FormRules>({
  group_id: [
    { required: true, trigger: "change", message: "请选择公司卡类型" }
  ],
  video_ids: [
    {
      required: true,
      type: "array",
      trigger: "change",
      message: "请选择直播视频"
    }
  ]
});

async function show(node: any, did: string, isCreate: string) {
  id.value = did;
  Video_id.value = "";
  Object.keys(ruleForm.value).forEach(key => {
    ruleForm.value[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 = "新建批量设置视频";
  }
  showModel.value = true;
}

function videoChange(e) {
  if (e) {
    const { id, video_name } = e;
    const index = ruleForm.value.video_ids.findIndex(
      e => e.id + "" === id + ""
    );
    if (index == -1) {
      ruleForm.value.video_ids.push({ id, video_name });
    }
  }
}

function handleSave() {
  formRef.value.validate(async vaild => {
    if (vaild) {
      if (loading.value) return;
      loading.value = true;
      const { group_id, video_ids } = ruleForm.value;
      const params = {
        group_id,
        video_ids: []
      };
      video_ids.forEach(item => {
        params.video_ids.push(item.id);
      });
      const { message, code } = await httpAdd(params);
      loading.value = false;
      responseHandle({
        code,
        message,
        logout,
        handler: () => {
          showModel.value = false;
          emit("reload");
        }
      });
    }
  });
}

defineExpose({
  show
});
</script>

<template>
  <el-dialog
    v-model="showModel"
    :close-on-press-escape="false"
    center
    append-to-body
    destroy-on-close
    :width="'650px'"
    :title="titleType"
    v-loading="loading"
  >
    <el-form
      ref="formRef"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      style="margin-top: -10px"
      class="demo-ruleForm"
      status-icon
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="公司卡类型" prop="group_id">
            <ComCard
              v-model="ruleForm.group_id"
              :showDisabled="true"
              placeholder="公司卡类型"
            /> </el-form-item
        ></el-col>
        <el-col :span="24">
          <el-form-item label="视频" prop="video_ids">
            <div v-show="ruleForm.video_ids && ruleForm.video_ids.length > 0">
              <el-tag
                v-for="(tag, index) in ruleForm.video_ids"
                :key="tag.id + index"
                class="mx-1"
                disable-transitions
                closable
                size="large"
                @close="ruleForm.video_ids.splice(index, 1)"
              >
                {{ tag.video_name }}
              </el-tag>
            </div>
          </el-form-item></el-col
        >
        <el-col :span="16">
          <el-form-item label="" prop="card_id">
            <Video
              v-model="Video_id"
              placeholder="请选择直播视频"
              :showDisabled="true"
              @change="videoChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8" 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>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>
