<template>
  <div class="app-container">
    <!-- 项目表单 -->
    <el-form class="form" :model="projectForm" label-width="100px">
      <el-form-item label="项目名称：">
        <el-input v-model="projectForm.name" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="图标：">
        <Upload :url="projectForm.pic" @success="getUpload($event, 'icon')"
          @delete="getUpload($event, 'icon')" />
      </el-form-item>
      <el-form-item v-if="projectForm.id" label="版本管理：" style="width: 1500px">
        <!-- app列表 -->
        <el-button type="primary" icon="el-icon-plus" @click="getDialog()">新增</el-button>
        <el-table :data="versionData" v-loading="loading" height="342px">
          <el-table-column type="index" label="序号" align="center" />
          <el-table-column prop="name" label="应用名称" align="center" show-overflow-tooltip />
          <el-table-column prop="version" label="最新版本" align="center" width="100px" show-overflow-tooltip />
          <el-table-column label="更新内容" align="center" show-overflow-tooltip>
            <template slot-scope="scope">
              <span class="updateContent" v-html="scope.row.updateContent" />
            </template>
          </el-table-column>
          <el-table-column prop="os" label="系统类型" align="center" width="100px" />
          <el-table-column prop="updateTimeText" label="更新时间" align="center" />
          <el-table-column prop="status" label="当前状态" align="center" :formatter="statusFormatter"
            width="100px" />
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button v-if="scope.row.status === 0" type="text" @click="getDialog(scope.row, '上架')">上架
              </el-button>
              <el-button v-else type="text" @click="getDialog(scope.row, '下架')">下架</el-button>
              <el-button type="text" @click="getDialog(scope.row, '更新')">更新</el-button>
              <el-button type="text" @click="getDialog(scope.row, '编辑')">编辑</el-button>
              <el-button type="text" v-clipboard:copy="scope.row.downUrl">复制下载地址</el-button>
              <!-- <el-dropdown @command="getCommand" style="margin-left: 10px">
                <el-button type="text">更多</el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="copy" v-clipboard:copy="scope.row.downUrl">复制下载地址</el-dropdown-item>
                  <el-dropdown-item command="history">查看版本记录</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>-->
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="versionForm.pageNum"
          :limit.sync="versionForm.pageSize" @pagination="getList" />
      </el-form-item>
      <el-form-item>
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="getSubmit">确定</el-button>
      </el-form-item>
    </el-form>

    <!-- 新增弹窗 -->
    <el-dialog :visible.sync="dialogVisible" :before-close="getCancel" :title="title" width="500px">
      <el-form :model="addForm" ref="addForm" label-width="100px">
        <el-form-item v-show="title !== '更新'" label="应用名称：">
          <el-input v-model="addForm.name" placeholder="请输入应用名称" />
        </el-form-item>
        <el-form-item v-show="title === '新增'" label="系统类型：">
          <el-select v-model="addForm.os" placeholder="请选择系统类型">
            <el-option v-for="item in osOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item v-show="title !== '更新'" label="应用Logo：">
          <Upload :url="addForm.pic" @success="getUpload($event, 'logo')"
            @delete="getUpload($event, 'logo')" />
        </el-form-item>
        <el-form-item v-show="title !== '编辑'" label="版本号：">
          <el-input v-model="addForm.version" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item v-show="title !== '编辑'" label="更新内容：">
          <Editor v-model="addForm.updateContent" :height="192" />
        </el-form-item>
        <el-form-item v-show="title !== '编辑'" label="安装包：">
          <Upload type="file" @success="getUpload($event, 'file')" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="getCancel">取消</el-button>
        <el-button type="primary" @click="getChange">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, edit, update, updateAdd, updatePage } from "@/api/project/list";
import Upload from "@/components/Upload";
import Editor from '@/components/Editor'
import { statusMixin, osMixin } from "../mixin/index";
export default {
  mixins: [statusMixin, osMixin],
  components: {
    Upload, Editor
  },
  data() {
    return {
      // 遮罩层
      loading: false,

      // 项目表单
      projectForm: {},
      // 新增表单
      addForm: {},
      // app列表
      versionData: [],
      versionForm: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,

      // 新增弹窗
      dialogVisible: false,
      title: "",
    };
  },
  mounted() {
    let row = JSON.parse(this.$route.query.row)
    if (row) {
      this.projectForm = row;
      this.addForm.projectId = this.versionForm.projectId = row.id;
      this.getList();
    }
  },
  methods: {
    // 获取版本管理分页
    getList() {
      this.loading = true;
      updatePage(this.versionForm).then((res) => {
        if (res.code === 0) {
          this.versionData = res.data.records;
          this.total = res.data.total;
          this.loading = false;
        }
      });
    },

    // 打开弹窗
    getDialog(e, key) {
      this.title = key ? key : "新增";
      if (e) {
        this.addForm = e;
      }

      if (key === "上架") {
        this.loading = true;
        this.addForm.status = 1;
        this.getChange();
      } else if (key === "下架") {
        this.loading = true;
        this.addForm.status = 0;
        this.getChange();
      } else {
        this.dialogVisible = true;
      }
    },

    // 上传
    getUpload(e, key) {
      if (key === "icon") {
        this.projectForm.pic = e.file;
      } else if (key === "logo") {
        this.addForm.pic = e.file;
      } else if (key === "file") {
        this.addForm.downUrl = e.file;
        this.addForm.size = e.size;
      }
    },

    // 新增 / 更新版本
    getChange() {
      if (this.title !== '编辑') {
        updateAdd(this.addForm).then((res) => {
          if (res.code === 0) {
            this.$message.success("新增成功！");
            this.dialogVisible = false;
            this.getList();
          }
        });
      } else {
        update(this.addForm).then((res) => {
          if (res.code === 0) {
            this.$message.success(`${this.title}成功！`);
            this.dialogVisible = false;
            this.getList();
          }
        });
      }
      // if (this.title === "新增") {
      //   updateAdd(this.addForm).then((res) => {
      //     if (res.code === 0) {
      //       this.$message.success("新增成功！");
      //       this.dialogVisible = false;
      //       this.getList();
      //     }
      //   });
      // } else {
      //   update(this.addForm).then((res) => {
      //     if (res.code === 0) {
      //       this.$message.success(`${this.title}成功！`);
      //       this.dialogVisible = false;
      //       this.getList();
      //     }
      //   });
      // }
    },

    // 取消
    getCancel() {
      this.addForm = {
        projectId: this.$route.query.row.id,
      };
      this.dialogVisible = false;
    },

    // 新建项目
    getSubmit() {
      if (this.$route.query.row) {
        edit(this.projectForm).then((res) => {
          if (res.code === 0) {
            this.$message.success("修改成功！");
            this.cancel();
          }
        });
      } else {
        add(this.projectForm).then((res) => {
          if (res.code === 0) {
            this.$message.success("新增成功！");
            this.cancel();
          }
        });
      }
    },

    // 取消新建项目
    cancel() {
      this.$tab.closeOpenPage("/project/projectList");
    },

    // 字典翻译
    statusFormatter(row) {
      return this.selectDictLabel(this.statusOptions, row.status);
    }
  },
};
</script>

<style lang="scss" scoped>
.form {
  width: 500px;
}

::v-deep .updateContent {
  display: block;
  height: 23px;
  margin: 0;

  p {
    margin-top: 0;
  }
}
</style>