<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="启动页" name="startPage">
        <div class="block" style="float: left;margin-left: 3%;margin-top: 3%">
          <video
            ref="video"
            class="play-video"
            controls="controls"
            autoplay="autoplay"
            width="337px"
            height="600px"
          >
            <source :src="videoForm.filePath" type="video/mp4" />
          </video>
        </div>
        <div style="float: left;margin-left: 10%;margin-top: 10%;">
          <el-upload
            ref="upload"
            :limit="1"
            accept=".mp4"
            :headers="upload.headers"
            :action="upload.url"
            :disabled="upload.isUploading"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            drag
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">请上传视频，用于启动页显示，比例建议为16: 9</div>
          </el-upload>
          <div slot="footer" class="dialog-footer" style="margin-left: 70%;margin-top: 10%">
            <el-button type="primary" @click="handleDelete(null,1)">删 除</el-button>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="介绍页" name="middlePage">
        <div style="height: 600px;" v-for="(item,index) of imagesInfo">
          <div class="block" style="float: left;margin-left: 3%;margin-top: 3%" >
            <el-image :src="item.filePath" style="width: 330px;height: 461px;">
              <div slot="placeholder" class="image-slot"><span class="dot"></span>
              </div>
            </el-image>
          </div>
          <div style="float: left;margin-left: 5%;margin-top: 10%;">
            <el-upload
              ref="upload"
              :limit="1"
              accept=".jpg, .png, .jpeg"
              :data="{index:index}"
              :headers="upload.headers"
              :action="upload.url+'/else'"
              :disabled="upload.isUploading"
              :on-progress="handleFileUploadProgress"
              :on-success="handleFileSuccess"
              drag
            >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div class="el-upload__tip" slot="tip">请上传启动页介绍图片，建议尺寸为330px*460px</div>
            </el-upload>
          </div>
          <div  style="float: left;margin-left: 5%;margin-top: 10%;">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
              <el-row>
                <el-col :span="24">
                  <el-form-item label="标题" prop="title">
                    <el-input v-model="item.title" placeholder="请输入标题" maxlength="10" />
                  </el-form-item>
                  <el-form-item label="介绍文字" prop="content">
                    <el-input v-model="item.content" placeholder="请输入介绍文字" maxlength="30" />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer" style="margin-left: 10%;margin-top: 5%">
              <el-button type="primary" @click="submitForm(item)">提 交</el-button>
              <el-button @click="handleDelete(item,2)">删 除</el-button>
              <el-button type="primary" @click="handleAddInfo">新 增</el-button>
            </div>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="登录页" name="logoPage">
        <div class="block" style="float: left;margin-left: 3%;margin-top: 3%">
          <el-image :src="loginForm.filePath" style="width: 330px;height: 461px;">
            <div slot="placeholder" class="image-slot"><span class="dot"></span>
            </div>
          </el-image>
        </div>
        <div style="float: left;margin-left: 10%;margin-top: 10%;">
          <el-upload
            ref="upload"
            :limit="1"
            accept=".jpg, .png, .jpeg"
            :headers="upload.headers"
            :action="upload.url"
            :disabled="upload.isUploading"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            drag
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">请上传登录页背景图片，建议尺寸为375px*471px</div>
          </el-upload>
        </div>

      </el-tab-pane>

    </el-tabs>


  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/system/firing";
import {getToken} from "@/utils/auth";
export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      activeName: 'startPage',
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 除了video后image类型的数据
      imagesInfo: [],
      // 登录信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      videoSrc: 'http://127.0.0.1:9300/statics/2023/12/16/0c49191de1ed8f91b28d9c2b12556154_20231216154804A002.mp4',
      // 是否显示弹出层
      open: false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/file/upload"
      },
      videoForm:{

      },
      loginForm:{

      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        filePath: null,
        fileSuffix: null,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // title: [
        //   { required: true, message: '请输入标题', trigger: 'blur' },
        // ],
        // content: [
        //   { required: true, message: '请输入内容', trigger: 'blur' },
        // ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询登录信息列表 */
    getList() {
      this.loading = true;
      this.imagesInfo = [];
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
        // 处理逻辑
        this.infoList.forEach((item)=>{
          if (item.type == 1 && item.fileSuffix == 'video') {
            // 启动页
            this.videoForm = item;
            this.$refs["video"].load();
            this.$refs["video"].play();
          } else if(item.type == 1 && item.fileSuffix == 'images') {
           // 介绍页
            this.imagesInfo.push(item);
          }else {
            // 登录页
            this.loginForm = item;
          }
        })
        if (this.imagesInfo.length == 0) {
          this.imagesInfo.push({"filePath":""});
        }
      });
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        filePath: null,
        fileSuffix: null,
        delFlag: null,
        type: null,
        createTime: null,
        createBy: null
      };
      this.resetForm("form");
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      // this.upload.isUploading = true;
      console.log('上传处理中==>',JSON.stringify(event))
      console.log('上传处理中==>file',JSON.stringify(file))
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList, item) {
      if(response.code == '200') {
        if (this.activeName == 'startPage') {
          // 启动页处理
          this.videoForm.filePath = response.data.url;
          this.videoForm.fileUrl =  response.data.url;
          this.videoForm.fileSuffix = 'video';
          if (this.videoForm.id != null) {
            // 更新操作
            updateInfo(this.videoForm).then(response => {
              this.$modal.msgSuccess("更新成功");
              this.open = false;
              this.getList();
            });
          } else {
            // 新增操作
            this.videoForm.type =  1;
            addInfo(this.videoForm).then(response => {
              this.$modal.msgSuccess("保存成功");
              this.open = false;
              this.getList();
            });
          }
        } else if(this.activeName == 'middlePage'){
          this.imagesInfo[response.data.index].filePath =  response.data.url;
          console.log('结果',JSON.stringify(this.imagesInfo))
        } else {
          // 登录页处理
          this.loginForm.filePath = response.data.url;
          this.loginForm.fileUrl =  response.data.url;
          this.videoForm.fileSuffix = 'image';
          if (this.loginForm.id != null) {
            // 更新操作
            updateInfo(this.loginForm).then(response => {
              this.$modal.msgSuccess("更新成功");
              this.open = false;
              this.getList();
            });
          } else {
            // 新增操作
            this.loginForm.type =  2;
            addInfo(this.loginForm).then(response => {
              this.$modal.msgSuccess("保存成功");
              this.open = false;
              this.getList();
            });
          }
        }
      }

      console.log('上传后的结果==>',JSON.stringify(response))
    },
    handleAddInfo(){
      this.imagesInfo.push({"filePath":""});
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加登录信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改登录信息";
      });
    },
    /** 提交按钮 */
    submitForm(item) {
      this.form = item;
      this.form.fileSuffix = 'images';
      this.form.type = 1;
      if (!this.form.title || !this.form.content) {
        this.$modal.msgError("标题或者内容不能为空");
        return;
      }
      if (this.form.id != null) {
        updateInfo(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else {
        addInfo(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row,flag) {
      let content = "";
      if(!row && this.videoForm) {
        row = this.videoForm;
      }
      if (flag == 1) {
        content += '启动页视频';
      } else {
        content += '介绍页标题为'+row.title+'的数据项';
      }
      if (!row.id) {
        this.$modal.msgError('目前数据状态不允许删除');
        return;
      }
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除"' + content + '"？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
        if(this.videoForm && this.videoForm.id){
          this.videoForm = {};
          this.$refs["video"].load();
          this.$refs["video"].play();
        }
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/firing/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
