<script setup lang="ts">
import { ref, unref, computed } from "vue";
import { PageSearch, usePageSearch } from "/@/components/PageSearch";
import searchConfig from "./config/search.config";
import contentConfig from "./config/content.config";
import PageAuth from "/@/components/PageAuth";
import { PageModal, usePageModal } from "/@/components/PageModalShell";
import modalConfig from "./config/modal.config";
import { PageContent } from "/@/components/PageContent";
import type { PageContentInstance } from "/@/components/PageContent";
import { useResponseHandle } from "/@/hooks/useAsync";
import { httpDetail } from "/@/api/mobile/user";
import { projectFormConfig } from "./config/_details";
import { projectFormRules } from "./config/_rules";
import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
// import resetPassword from "/@/components/resetPassword.vue";

import {
  BasicForm,
  transform,
  createDefaultData
} from "/@/components/BasicForm";
import { Video } from "/@/components/RemoteSelect";
import { ElForm } from "element-plus";
const { formItems } = projectFormConfig;
const formData = ref<Record<string, any>>(createDefaultData(formItems));
const pageContentRef = ref<PageContentInstance | null>(null);

const {
  pageModalRef,
  handleUpdateData,
  handlePreviewData,
  handleConfrim,
  defaultInfo
} = usePageModal({
  pageContentRef
});
const { handleResetClick, handleSearchClick } = usePageSearch();
const responseHandle = useResponseHandle();
const Video_id = ref("");
async function handleDetailData(row, type) {
  const { id } = row;
  const { code, data, message } = await httpDetail({ id: id });

  responseHandle({
    code,
    message,
    handler: () => {
      if (type === "preview") {
        handlePreviewData(data);
      }
      if (type === "update") {
        formData.value = transform(formItems, data, {
          video_ids: "video_list"
        });
        columnsData = data;
        handleUpdateData(unref(formData));
      }
    }
  });
}
const basicFormRef = ref<InstanceType<typeof ElForm>>(null);
function handleCreate() {
  basicFormRef.value.validate(isValid => {
    if (!isValid) return;
    const { video_ids } = formData.value;
    const params = unref(formData);

    let arr = [];
    video_ids.forEach(e => {
      arr.push(e.id);
    });
    params.video_ids = arr;
    handleConfrim("update", params);
  });
}
function videoChange(e) {
  if (e) {
    const { id, video_name } = e;
    const index = formData.value.video_ids.findIndex(
      e => e.id + "" === id + ""
    );
    if (index == -1) {
      formData.value.video_ids.push({ id, video_name });
    }
  }
}
</script>

<template>
  <!-- <PageAuth :pageName="pageName"> -->
  <PageSearch
    :form-config="searchConfig"
    @search-btn-click="handleSearchClick"
    @reset-btn-click="handleResetClick"
  />
  <PageContent
    ref="pageContentRef"
    :content-config="contentConfig"
    @preview-btn-click="row => handleDetailData(row, 'preview')"
    @update-btn-click="row => handleDetailData(row, 'update')"
  />
  <!--需要优化 添加修改密码和重置密码功能 PageContent-->
  <PageModal
    ref="pageModalRef"
    :modal-config="modalConfig"
    :default-info="defaultInfo"
    @confirm-btn-click="handleConfrim"
  >
    <BasicForm
      ref="basicFormRef"
      v-bind="projectFormConfig"
      :form-data="formData"
      :rules="projectFormRules"
      :disabled="false"
      label-width="120px"
    >
      <template #username>
        <div>{{ formData.username }}</div>
      </template>
      <template #addtime>
        <div>{{ formData.addtime }}</div>
      </template>
      <template #company_title>
        <div>{{ formData.company_title }}</div>
      </template>
      <template #card_title>
        <div>{{ formData.card_title }}</div>
      </template>

      <template #video_ids>
        <div
          v-show="formData.video_ids && formData.video_ids.length > 0"
          style="width: 100%; padding: 0 0 15px 0"
        >
          <el-tag
            v-for="(tag, index) in formData.video_ids"
            :key="tag.id + index"
            class="mx-1"
            disable-transitions
            closable
            size="large"
            @close="formData.video_ids.splice(index, 1)"
          >
            {{ tag.video_name }}
          </el-tag>
        </div>
        <div style="width: 50%">
          <Video
            v-model="Video_id"
            placeholder="请选择直播视频"
            @change="videoChange"
          />
        </div>
      </template>

      <template #footer>
        <div class="w-full flex justify-end">
          <ElButton type="primary" @click="handleCreate">保存</ElButton>
        </div>
      </template>
    </BasicForm>
  </PageModal>
  <!-- <reset-password
    :itemId="passwordModelId"
    :show-model="passwordModel"
    @refresh="passwordModel = false"
    @cancel="passwordModel = false"
  /> -->
  <!-- </PageAuth> -->
</template>
