<template>
  <div class="app-container">
    <el-form :inline="true" ref="form" :model="form" size="mini">
      <el-form-item><el-button type="warning" @click="onAdd">添加应用</el-button></el-form-item>
      <el-form-item><el-button type="success" @click="onAddFramework">添加系统页面</el-button></el-form-item>
    </el-form>
    <el-table
      v-loading="isLoading"
      element-loading-text="Loading"
      :data="list"
      size="mini"
      border
      stripe
      fit
      highlight-current-row
    >
      <el-table-column
        prop="app_name"
        label="应用名称"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="app_version"
        label="应用版本号"
        width="150"
      ></el-table-column>
      <el-table-column
        prop="category"
        label="应用类别"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="app_url"
        label="应用路径"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="app_desc"
        label="应用描述"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="create_at"
        label="创建时间"
        width="150"
      ></el-table-column>
      <el-table-column
        prop="create_by.username"
        label="创建者"
        width="150"
      ></el-table-column>
      <el-table-column
        prop="update_at"
        label="更新时间"
        width="150"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="update_by.username"
        label="更新者"
        width="150"
      ></el-table-column>
      <el-table-column label="操作" align="center" min-width="180" fixed="right">
        <template slot-scope="scope">
            <el-button
            size="mini"
            type="primary"
            @click="handleBuild(scope.$index, scope.row)"
            >一键打包</el-button
          >
          <el-button
            size="mini"
            type="success"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="page-wrapper">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="form.pagenum"
        background
        small
        :page-size="form.pagesize"
        :pager-count="5"
        layout="pager, prev, next, total"
        :total="total"
      ></el-pagination>
    </div>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%">
      <el-form :model="post" status-icon ref="post" size="medium" label-width="100px">
        <el-form-item label="应用名称" prop="app_name">
          <el-input type="text" v-model="post.app_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="应用版本号" prop="app_version">
          <el-input type="text" v-model="post.app_version" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="应用路径" prop="app_url">
          <el-input type="text" v-model="post.app_url" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="应用类别" prop="category">
          <el-input type="text" v-model="post.category" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="应用描述" prop="app_desc">
          <el-input type="text" v-model="post.app_desc" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="应用Logo" prop="app_icon">
          <el-upload
            class="avatar-uploader"
            :action="`${window.location.protocol}//${window.location.host}/api/v1/evm_store/upload`"
            name="binfile"
            :on-remove="handleAvatarRemove"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="应用文件" prop="app_files">
          <el-upload
            drag
            :action="`${window.location.protocol}//${window.location.host}/api/v1/evm_store/upload`"
            :on-remove="handleRemove"
            :on-success="handleUploadSuccess"
            multiple
            name="binfile"
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将文件拖到此处，或<em>点击上传</em>
            </div>
            <div class="el-upload__tip" slot="tip">
              .evue和资源文件
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="medium" plain @click="submitForm('post')">提交</el-button>
        <el-button type="success" size="medium" plain @click="onReset('form')">重置</el-button>
        <el-button size="medium" @click="dialogVisible = false">关闭</el-button>
      </div>
    </el-dialog>
    <el-dialog title="添加系统页面" :visible.sync="frameworkDialog">
        <el-form :model="form" label-width="100px">
            <el-form-item label="页面名称">
                <el-input v-model="framework.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="页面路径">
                <el-input v-model="framework.url" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="活动描述">
                <el-input v-model="framework.desc" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="活动类型">
                <el-input v-model="framework.type" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="资源文件" prop="app_icon">
                <el-upload
                    drag
                    multiple
                    name="binfile"
                    :action="`${window.location.protocol}//${window.location.host}/api/v1/evm_store/upload`"
                    :on-remove="handleFrameworkRemove"
                    :on-success="handleFrameworkSuccess"
                    :file-list="fileList"
                >
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                    <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
                </el-upload>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="frameworkDialog = false">取 消</el-button>
            <el-button type="primary" @click="addFramework">确 定</el-button>
        </div>
    </el-dialog>
  </div>
</template>
<script>
import { getAppsList, deleteApp, addApp, updateApp, buildApp, addFramework } from "@/api/app-store";
import { mapTrim, checkURL } from "@/utils/index";

export default {
  name: "AppIndex",
  data() {
    return {
      imageUrl: "",
      total: 0,
      list: [],
      fileList: [],
      isLoading: false,
      form: {
        uuid: null,
        name: null,
        pagesize: 15,
        pagenum: 1,
      },
      framework: {
          name: null,
          url: null,
          desc: null,
          type: null,
          assets: {
              files: []
          }
      },
      currentIndex: 0,
      currentValue: null,
      frameworkDialog: false,
      dialogTitle: "",
      dialogVisible: false,
      post: {
        app_name: null,
        app_version: null,
        app_icon: null,
        app_url: null,
        category: null,
        app_desc: null,
        app_files: [],
      },
    };
  },
  computed: {
    window: () => window,
  },
  methods: {
    fetchData(params) {
      this.isLoading = true;
      getAppsList(params)
        .then((res) => {
          this.total = res.count;
          this.list = res.data;
        })
        .catch((err) => {
          // this.$message.error(err.message)
          console.log(err.message);
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
    handleSizeChange(e) {
      this.form.pagesize = e;
      this.fetchData(mapTrim(this.form));
    },
    handleCurrentChange(e) {
      this.form.pagenum = e;
      this.fetchData(mapTrim(this.form));
    },
    handleBuild(index, row) {
        console.log(index)
        buildApp(row.uuid).then(res => {
            this.$message.success(res.message)
        }).catch(err => {
            console.log(err)
            this.$message.error(err.message)
        })
    },
    handleEdit(index, row) {
      this.post = Object.assign(row);
      this.currentIndex = index;
      this.currentValue = row;
      this.dialogTitle = "编辑";
      this.dialogVisible = true;
    },
    handleDelete(index, row) {
      this.$alert(
        "您确定要删除么？删除操作将不可恢复。如需取消操作，请点击右上角关闭按钮。",
        "删除提醒",
        {
          confirmButtonText: "确定",
          callback: (action) => {
            if (action == "confirm")
              deleteApp(row.uuid)
                .then((res) => {
                  console.log(res);
                  this.total -= 1;
                  this.$delete(this.list, index);
                  this.$message({
                    type: "success",
                    message: `成功删除第${index}行`,
                  });
                })
                .catch((err) => {
                  this.$message.error(err.message);
                });
          },
        }
      );
    },
    handleUploadSuccess(res) {
        if (res.code == 200) {
            if (!checkURL(res.data.filepath))
                res.data.filepath = `${window.location.origin}/${res.data.filepath}`
            this.post.app_files.push(res.data)
        }
    },
    handleAvatarSuccess(res, file) {
        if (res.code == 200) {
            if (!checkURL(res.data.filepath))
                res.data.filepath = `${window.location.origin}/${res.data.filepath}`
            this.post.app_icon = res.data
        }
        this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload() {
        // const isJPG = file.type === "image/jpeg";
        // const isLt2M = file.size / 1024 / 1024 < 2;

        // if (!isJPG) {
        //     this.$message.error("上传头像图片只能是 JPG 格式!");
        // }
        // if (!isLt2M) {
        //     this.$message.error("上传头像图片大小不能超过 2MB!");
        // }
        // return isJPG && isLt2M;
        return true;
    },
    handleAvatarRemove(file) {
        console.log(file);
        this.imageUrl = null;
    },
    handleRemove(file) {
        for(let i = 0; i < this.post.app_files.length; i++) {
            if (this.post.app_files[i].uuid == file.response.data.uuid) {
                this.post.app_files.splice(i, 1);
                break;
            }
        }
        console.log(file);
    },
    handleFrameworkRemove() {

    },
    handleFrameworkSuccess(res) {
        this.framework.assets.files.push(res.data)
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        let result = true;
        if (valid) {
          if (this.dialogTitle === "添加")
            addApp(mapTrim(this.post))
              .then((res) => {
                console.log(res);
                this.$message({ type: "success", message: "添加成功" });
                this.fetchData(mapTrim(this.form));
              })
              .catch((err) => {
                this.$message.error(err.message);
              });
          else if (this.dialogTitle === "编辑")
            updateApp(this.currentValue.uuid, this.post)
              .then((res) => {
                console.log(res);
                // this.$set(this.list, this.currentIndex, Object.assign(this.currentValue, tmp))
                this.$message({ type: "success", message: "更新成功" });
                this.fetchData(mapTrim(this.form));
              })
              .catch((err) => {
                this.$message.error(err.message);
              });
        } else {
          result = false;
        }
        this.dialogVisible = false;
        return result;
      });
    },
    submitFramework() {

    },
    addFramework() {
        addFramework(this.framework).then(res => {
            this.$message.success(res.message)
            this.frameworkDialog = false
        }).catch(err => {
            this.$message.error(err.message)
        })
    },
    onAddFramework() {
        this.frameworkDialog = true;
    },
    onAdd() {
      this.dialogTitle = "添加";
      this.dialogVisible = true;
    },
    onSubmit() {
      this.form.pagenum = 1;
      this.form.pagesize = 15;
      this.fetchData(mapTrim(this.form));
    },
    onReset(formName) {
      this.$refs[formName].resetFields();
      this.form.pagesize = 15;
      this.form.pagenum = 1;
      this.fetchData(mapTrim(this.form));
    },
  },
  mounted() {},
  created() {
    this.fetchData(mapTrim(this.form));
  },
};
</script>
<style lang="less" scoped>
.app-container {
  & > div.page-wrapper {
    margin: 10px 0px;
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
