<template>

 <div class="layout-no-flex">
    <el-toolbar>
      <el-button @click="$router.back()" type="primary" size="medium" round class="shadow-primary">
        <i class="fa fa-chevron-left"></i> 返回上页
      </el-button>
      <!-- <span class="button-separator"></span>
      <el-button :disabled="selectedRows.length<1" @click="deleteTemplatePackage">
        <i class="fa fa-remove" aria-hidden="true"></i>删除
      </el-button> -->
    </el-toolbar>

    <div>
        <el-table  :data-read-url="dataReadFunc" :page-size="10"  pagination-layout="sizes, ->, prev, pager, next, jumper" ref="templateTable" @selection-change="tableSelectionChange" >
          <el-table-column type="index" label="序号" width="100" align="center"></el-table-column>
          <el-table-column property="version" label="版本号" width="150"></el-table-column>
          <el-table-column property="fileName" label="文件名">
            <template slot-scope="scope">
              <a href="javascript:void(0)" @click="downloadTemplateZip(scope.row.fileName)">{{ scope.row.fileName }}</a>
            </template>
          </el-table-column>
          <el-table-column property="addUser" label="操作人员" width="100"></el-table-column>
          <el-table-column property="addTime" label="保存时间" width="180"></el-table-column>
          <el-table-column label="操作" align="right" width="80">
            <template slot-scope="scope">
              <el-button @click.stop="recoveryTemplatePackage(scope.row)" type="text" title="恢复">
                <i class="fa fa-refresh f16"></i>
              </el-button>
              <el-button @click.stop="deleteTemplatePackage(scope.row)" type="text" title="删除">
                <i class="fa fa-remove f16"></i>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
    </div>

    <!--模板包版本Dialog-->
    <el-dialog v-if="false" title="模板包版本" :visible.sync="dialogVisible">
      <el-toolbar>
        <el-button :disabled="selectedRows.length<1" @click="deleteTemplatePackage">
          <i class="fa fa-remove" aria-hidden="true"></i>删除
        </el-button>
      </el-toolbar>
      <el-table  :data-read-url="dataReadFunc" :page-size="10"  pagination-layout="sizes, ->, prev, pager, next, jumper" ref="templateTable" @selection-change="tableSelectionChange" >
        <el-table-column type="index" label="序号" align="center"></el-table-column>
        <el-table-column property="version" label="版本号" width="120"></el-table-column>
        <el-table-column property="fileName" label="文件名">
          <template slot-scope="scope">
            <a href="javascript:void(0)" @click="downloadTemplateZip(scope.row.fileName)">{{ scope.row.fileName }}</a>
          </template>
        </el-table-column>
        <el-table-column property="addUser" label="操作人员" width="80"></el-table-column>
        <el-table-column property="addTime" label="保存时间" width="180"></el-table-column>
        <el-table-column label="操作" align="right" width="80">
          <template slot-scope="scope">
            <el-button @click.stop="recoveryTemplatePackage(scope.row)" type="text" title="恢复">
              <i class="fa fa-refresh f16"></i>
            </el-button>
            <el-button @click.stop="deleteTemplatePackage(scope.row)" type="text" title="删除">
              <i class="fa fa-remove f16"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import util from '@src/common/util.js'

export default {
  data() {
    return {
      params: {
        siteID: 0,
        platformID: 'pc'
      },
      selectedRows: [], // 选中的行
      dialogVisible: false, // dialog是否可见
      currentRowData: {}, // 选中当前行数据
      interval: null,
      dataReadURL: '/ui/templateziphistory'
    }
  },
  created() {
    this.initForm()
  },
  methods: {
    // 初始化Dialog
    initForm() {
      this.params.siteID = this.$route.query.siteID
      this.params.platformID = this.$route.query.platformID
      // console.log(this.params)
      this.dialogVisible = true
      this.loadTableData()
    },

    dataReadFunc() {
      let params = {
        siteID: this.params.siteID,
        platformID: this.params.platformID
      }
      return [this.dataReadURL, { params }]
    },

    // 加载模板包版本数据
    async loadTableData() {
      !!this.$refs.templateTable && this.$refs.templateTable.getData()
    },

    // 模板包版本表格数据选中事件
    tableSelectionChange(selection) {
      this.selectedRows = selection
      this.currentRowData = this.selectedRows.length ? Object.assign({}, this.selectedRows[0]) : {}
    // console.log(this.currentRowData)
    },
    // 恢复模板包
    async recoveryTemplatePackage(row) {
      !!row && row.ID && Object.assign(this.currentRowData, row)
      let param = {
        ID: row.ID,
        siteID: row.siteID,
        platformID: row.platformID
      }
      try {
        await this.$confirm('确认使用选中的版本进行恢复吗 ？', '系统提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        let res = await axios.put('/ui/templateziphistory/restore', param)
        if (res.data.status === 1) {
          await util.showProgress(res.data.taskID, `执行中...`)
          this.selectedRows = []
          this.currentRowData = {}
          // this.loadTableData()
          this.$emit('success')
          this.dialogVisible = false
        } else {
          util.showMessage(res)
        }
      } catch (e) {

      }
    },
    // 删除模板包
    async deleteTemplatePackage(row) {
      let ids = !!row && row.ID ? Array.of(row['ID']) : util.getSelectedIDs(this.selectedRows)
      let param = {
        ID: ids.join(),
        siteID: row.siteID,
        platformID: row.platformID
      }

      try {
        await this.$confirm('确认删除？', '系统提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        let res = await axios.delete(`/ui/templateziphistory/${param.ID}`, { params: param })
        util.showResponseMessage(res.data)
        if (res.data.status === 1) {
          this.selectedRows = []
          this.currentRowData = {}
          this.loadTableData()
        }
      } catch (e) {

      }
    },

    // 模板包下载
    downloadTemplateZip(fileName) {
      let platformID = this.params.platformID
      let siteID = this.params.siteID
      window.location.href = `./ui/site/export?path=${fileName}&siteID=${siteID}&platformID=${platformID}`
    }
  },
  props: {
    // params: {
    //    type: Object
    //   default: function() {
    //     return {
    //        siteID: 0
    //       platformID: 0
    //     }
    //   }
    // }
  },
  watch: {}
}
</script>
