<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" listType="picture-card" @upload="getUpload($event, 'icon')" />
      </el-form-item>
      <el-form-item v-if="projectForm.id" label="版本管理：" style="width: 100%;">
        <!-- app列表 -->
        <el-button type="primary" icon="el-icon-plus" @click="getDialog()"
          v-hasPermi="['project:list:add']">新增</el-button>
        <el-table :data="versionData" v-loading="loading">
          <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" 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="getShelves(scope.row, 1)"
                v-hasPermi="['project:list:up']">上架
              </el-button>
              <el-button v-else type="text" @click="getShelves(scope.row, 0)"
                v-hasPermi="['project:list:down']">下架</el-button>
              <el-button type="text" @click="getDialog(scope.row, '更新')">更新</el-button>
              <el-button type="text" @click="getDialog(scope.row, '编辑')" v-hasPermi="['project:list:edit']">编辑</el-button>
              <el-dropdown @command="getCommand($event, scope.row)" 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>
    <div class="form-btn">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="getSubmit" v-hasPermi="['project:list:edit']">确定</el-button>
    </div>

    <!-- 新增弹窗 -->
    <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-if="title !== '更新'" label="应用名称：">
          <el-input v-model="addForm.name" placeholder="请输入应用名称" />
        </el-form-item>
        <el-form-item v-if="title === '新增'" label="系统类型：">
          <el-select v-model="addForm.os" placeholder="请选择系统类型">
            <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="title !== '更新'" label="应用Logo：">
          <Upload :url="addForm.pic" listType="picture-card" @upload="getUpload($event, 'logo')" />
        </el-form-item>
        <el-form-item v-if="title !== '编辑'" label="版本号：">
          <el-input v-model="addForm.version" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item v-if="title !== '编辑'" label="更新内容：">
          <Editor v-model="addForm.updateContent" :height="192" />
        </el-form-item>
        <el-form-item v-if="title !== '编辑'" label="安装包：">
          <Upload @upload="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>

    <!-- 历史记录 -->
    <el-dialog :visible.sync="dialogVisible_history" title="历史记录" width="1000px">
      <el-table :data="historyData">
        <el-table-column label="序号" type="index" align="center" />
        <el-table-column label="应用版本" prop="version" align="center" />
        <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 label="系统类型" prop="os" align="center" />
        <el-table-column label="更新时间" prop="updateTimeText" align="center" />
        <el-table-column label="当前状态" prop="status" align="center" :formatter="statusFormatter" />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status === 0" type="text" @click="getShelves(scope.row, 1)"
              v-hasPermi="['project:list:up']">上架
            </el-button>
            <el-button v-else type="text" @click="getShelves(scope.row, 0)"
              v-hasPermi="['project:list:down']">下架</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="historyTotal > 0" :total="historyTotal" :page.sync="historyForm.pageNum"
        :limit.sync="historyForm.pageSize" @pagination="getHistoryPage" />
    </el-dialog>
  </div>
</template>

<script>
import { add, edit, update, appEdit, updateAdd, updatePage, historyPage, shelves } from "@/api/project/list";
import Editor from "@/components/Editor";
export default {
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      projectId: 0,
      // 项目表单
      projectForm: {},
      // 新增表单
      addForm: {},
      // app列表
      versionData: [],
      versionForm: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      // 历史记录列表
      historyData: [],
      historyForm: {
        pageNum: 1,
        pageSize: 10
      },
      historyTotal: 0,

      // 新增弹窗
      dialogVisible: false,
      title: "",
      // 历史记录弹窗
      dialogVisible_history: false,
      // 手机系统
      typeOptions: [{
        value: 'IOS',
        label: 'IOS'
      }, {
        value: 'Android',
        label: 'Android'
      }],
      // 当前状态
      statusOptions: [{
        value: 0,
        lable: '未发布'
      }, {
        value: 1,
        label: '已发布'
      }]
    };
  },
  mounted() {
    let row = JSON.parse(this.$route.query.row);
    if (row) {
      this.projectForm = row;
      this.projectId = row.id
      this.getList();
    }
  },
  methods: {
    // 获取版本管理分页
    getList() {
      this.loading = true;
      this.versionForm.projectId = this.projectId
      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 (this.title === '编辑') {
        this.addForm = {
          id: e.id,
          name: e.name,
          pic: e.pic
        }
      } else if (this.title === '更新') {
        this.addForm = {
          downUrl: e.downUrl,
          id: e.id,
          size: e.size,
          updateContent: e.updateContent,
          version: e.version
        }
      } else {
        this.addForm = {
          projectId: this.projectId
        }
      }
      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 === '编辑') {
        appEdit(this.addForm).then((res) => {
          if (res.code === 0) {
            this.$message.success(`${this.title}成功！`)
            this.dialogVisible = false,
              this.getList()
          }
        })
      } else if (this.title === '更新') {
        update(this.addForm).then((res) => {
          if (res.code === 0) {
            this.$message.success(`${this.title}成功！`)
            this.dialogVisible = false
            this.getList()
          }
        })
      } else {
        updateAdd(this.addForm).then((res) => {
          if (res.code === 0) {
            this.$message.success("新增成功！");
            this.dialogVisible = false;
            this.getList();
          }
        });
      }
    },

    // 上下架
    getShelves(row, status) {
      shelves({
        id: row.id,
        status: status
      }).then(res => {
        if (res.code === 0) {
          this.getList()
          this.getHistoryPage()
        }
      })
    },

    // 更多
    getCommand(e, row) {
      if (e === 'history') {
        this.dialogVisible_history = true
        this.historyForm.projectUpdateId = row.id
        this.getHistoryPage()
      }
    },

    // 历史记录
    getHistoryPage() {
      this.historyForm.projectId = this.projectId
      historyPage(this.historyForm).then(res => {
        if (res.code === 0) {
          this.historyData = res.data.records
          this.historyTotal = res.data.total
        }
      })
    },

    // 取消
    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 {
  .el-form-item {
    width: 500px;
  }
}

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

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