<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main animate__animated animate__backInRight" v-loading="loading">
        <div class="w_1200">
          <el-tabs class="tab" type="border-card">
            <el-tab-pane label="科技成果在线">
              <el-collapse v-model="activeNames" accordion>
                <el-collapse-item title="正在直播" name="1">
                  <el-row>
                    <el-col :span="24" class="course">
                      <el-col class="list" :span="8" :key="index" v-for="(item, index) in science">
                        <el-col :span="24" class="image"><el-image :src="src"></el-image></el-col>
                        <el-col :span="24" class="info">
                          <el-col :span="24" class="title textOver" :title="item.title">
                            <span>[{{ item.room_id }}]</span>{{ item.title }}
                          </el-col>
                          <el-col :span="24" class="other">
                            <el-col :span="10" class="textOver">
                              <el-icon><LocationInformation /></el-icon>{{ item.province }}-{{ item.city }}
                            </el-col>
                            <el-col :span="14" class="textOver" :title="item.time">
                              <el-icon><Clock /></el-icon>{{ item.time }}
                            </el-col>
                          </el-col>
                          <el-col :span="24" class="btn">
                            <el-button type="primary" size="small" @click="toLogin(item)">管理进入</el-button>
                            <el-button type="primary" size="small" @click="toJump(item)">进入对接会</el-button>
                          </el-col>
                        </el-col>
                      </el-col>
                    </el-col>
                  </el-row>
                </el-collapse-item>
                <el-collapse-item title="下期预告" name="2">
                  <el-row>
                    <el-col :span="24" class="course">
                      <el-col class="list" :span="8" :key="index" v-for="(item, index) in nextList">
                        <el-col :span="24" class="image"><el-image :src="src"></el-image></el-col>
                        <el-col :span="24" class="info">
                          <el-col :span="24" class="title textOver" :title="item.title">
                            <span>[{{ item.room_id }}]</span>{{ item.title }}
                          </el-col>
                          <el-col :span="24" class="other">
                            <el-col :span="10" class="textOver">
                              <el-icon><LocationInformation /></el-icon>{{ item.province }}-{{ item.city }}
                            </el-col>
                            <el-col :span="14" class="textOver" :title="item.time">
                              <el-icon><Clock /></el-icon>{{ item.time }}
                            </el-col>
                          </el-col>
                          <el-col :span="24" class="btn">
                            <el-button type="primary" size="small" @click="toLogin(item)">管理进入</el-button>
                          </el-col>
                        </el-col>
                      </el-col>
                    </el-col>
                  </el-row>
                </el-collapse-item>
                <el-collapse-item title="已往直播" name="3">
                  <el-row>
                    <el-col :span="24" class="course">
                      <el-col class="list" :span="8" :key="index" v-for="(item, index) in endscience">
                        <el-col :span="24" class="image"><el-image :src="src"></el-image></el-col>
                        <el-col :span="24" class="info">
                          <el-col :span="24" class="title textOver" :title="item.title">
                            <span>[{{ item.room_id }}]</span>{{ item.title }}
                          </el-col>
                          <el-col :span="24" class="other">
                            <el-col :span="10" class="textOver">
                              <el-icon><LocationInformation /></el-icon>
                              {{ item.province }}-{{ item.city }}
                            </el-col>
                            <el-col :span="14" class="textOver" :title="item.time">
                              <el-icon><Clock /></el-icon>{{ item.time }}
                            </el-col>
                          </el-col>
                          <el-col :span="24" class="btn">
                            <el-button type="primary" size="small" @click="toLogin(item)">管理进入</el-button>
                          </el-col>
                        </el-col>
                      </el-col>
                    </el-col>
                  </el-row>
                </el-collapse-item>
              </el-collapse>
            </el-tab-pane>
          </el-tabs>
        </div>
      </el-col>
    </el-row>
    <cDialog :dialog="dialog" @handleClose="toClose">
      <template v-slot:info>
        <el-col :span="24" class="dialog_one" v-if="dialog.type == '1'">
          <cForm :span="24" :fields="fields" :form="form" :rules="rules" @save="toSave" submitText="提交登录"> </cForm>
        </el-col>
      </template>
    </cDialog>
  </div>
</template>

<script setup lang="ts">
// 基础
import type { Ref } from 'vue';
// reactive,
import { onMounted, ref, reactive } from 'vue';
import type { FormRules } from 'element-plus';
import { ElMessage } from 'element-plus';
// 接口
import { DockStore } from '@common/src/stores/allAdmin/dock';
import { TokenStore } from '@common/src/stores/user/token'; // 解析
import type { IQueryResult } from '@/util/types.util';
const dock = DockStore();
const tokenAxiso = TokenStore();
// 加载中
const loading: Ref<any> = ref(false);
const activeNames: Ref<any> = ref('1');
const science: Ref<any> = ref([]); // 开始
const endscience: Ref<any> = ref([]); // 结束
const nextList: Ref<any> = ref([]); // 准备
const src: Ref<any> = ref('/src/assets/live-1.jpg');

// 弹框
const dialog: Ref<any> = ref({ type: '1', show: false, title: '信息管理' });
let form: Ref<any> = ref({});
// 表单
let fields: Ref<any[]> = ref([
  { label: '房间号', model: 'room_id', options: { disabled: true } },
  { label: '密码', model: 'password', type: 'password' }
]);
const rules = reactive<FormRules>({
  password: [{ required: true, message: '请输入账号密码', trigger: 'blur' }]
});

// 请求
onMounted(async () => {
  loading.value = true;
  search();
  loading.value = false;
});
const search = async () => {
  let res: IQueryResult;
  // 开始
  res = await dock.query({ status: '1' });
  if (res.errcode == 0) {
    let data: any = res.data;
    for (const p1 of data) {
      if (!p1.start_time) p1.start_time = '暂无';
      if (!p1.end_time) p1.end_time = '暂无';
      p1.time = p1.start_time + '-' + p1.end_time;
    }
    science.value = data;
  }

  // 准备
  res = await dock.query({ status: '0' });
  if (res.errcode == 0) {
    let data: any = res.data;
    for (const p1 of data) {
      if (!p1.start_time) p1.start_time = '暂无';
      if (!p1.end_time) p1.end_time = '暂无';
      p1.time = p1.start_time + '-' + p1.end_time;
    }
    nextList.value = data;
  }
  // 结束
  res = await dock.query({ status: '2' });
  if (res.errcode == 0) {
    let data: any = res.data;
    for (const p1 of data) {
      if (!p1.start_time) p1.start_time = '暂无';
      if (!p1.end_time) p1.end_time = '暂无';
      p1.time = p1.start_time + '-' + p1.end_time;
    }
    endscience.value = data;
  }
};
// 管理登录
const toLogin = (e) => {
  let object: any = { room_id: e.room_id };
  form.value = object;
  dialog.value = { title: '展会登录', show: true, type: '1' };
};
// 提交登录
const toSave = async (data) => {
  let res: IQueryResult = await dock.login(data);
  if (res.errcode == '0') {
    ElMessage({ message: '登录成功', type: 'success' });
    localStorage.setItem('token', `${res.data}`);
    // 解析token跳转页面
    tokenView();
  } else {
    ElMessage({ message: `${res.errmsg}`, type: 'error' });
  }
};
const tokenView = async () => {
  let res: IQueryResult = await tokenAxiso.tokenInfo();
  if (res.errcode == '0') {
    // let user = res.data as any;
    let env = import.meta.env.VITE_APP_ENV;
    let path = '';
    if (env == 'development') path = 'http://localhost:8004/zkzxlive';
    else path = '/zkzxlive';
    window.open(path);
  }
};
// 弹框关闭
const toClose = () => {
  form.value = {};
  dialog.value = { title: '展会登录', show: false, type: '1' };
};
const toJump = (e) => {
  console.log(e);
};
</script>
<style scoped lang="scss">
.main {
  min-height: 557px;
  padding: 10px 0;
  .tab {
    min-height: 530px;
  }
}
.w_1200 {
  width: 1200px;
  margin: 0 auto;
}
.course {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}
.list {
  max-width: 32.466%;
  border: 1px solid #ccc;
  margin: 0 15px 10px 0;
  border-radius: 10px;
  .image {
    height: 260px;
  }
  .el-image {
    height: 260px;
    border-radius: 10px 10px 0px 0px;
  }
  .info {
    padding: 5px 10px 10px 10px;
    .title {
      font-size: 16px;
      font-weight: 700;
      padding: 0 0 5px 0;
    }
    .other {
      display: flex;
      font-size: 14px;
      padding: 0 0 5px 0;
    }
    .btn {
      text-align: center;
    }
  }
}
:deep(.el-tabs__item) {
  font-size: 18px;
  font-weight: 700;
  height: 50px;
  line-height: 50px;
}
.list:nth-child(3n) {
  margin: 0 0 10px 0;
}
.list .info .title span:first-child {
  color: red;
}
.textOver {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
