<template>
  <div class="app-container">
    <el-table :data="branchDaata">
      <el-table-column label="分支名称" prop="branchName" width="150"/>
      <el-table-column label="当前版本" prop="sha1" width="340" show-overflow-tooltip/>
      <el-table-column label="备注" prop="note" width="200" show-overflow-tooltip/>
      <el-table-column label="是否打包中" prop="time" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.time" style="color: #409EFF">打包中 <i class="el-icon-loading"></i></span>
          <span v-else>无打包任务</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-upload2" :disabled="scope.row.time" @click="handlePack(scope.row)">打包并发布最新</el-button>
          <el-button size="mini" type="text" icon="el-icon-back" :disabled="scope.row.time" @click="handleBack(scope.row)">Git版本回退</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="打包并发布最新" :visible.sync="packOpen" width="400px" append-to-body>
      <el-form label-width="100px">
        <el-form-item label="选择打包命令">
            <el-select v-model="commandValue" multiple collapse-tags placeholder="请选择" style="width: 100%" @change="selsectHandle">
              <el-option
                v-for="item in commandData"
                :key="item.value"
                :label="item.name + '/' + item.value"
                :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handlePublish">确 定</el-button>
        <el-button @click="packOpen = false; commandValue = null">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="Git版本回退" :visible.sync="backOpen" width="400px" append-to-body>
      <el-form label-width="100px">
        <el-form-item label="选择打包命令">
            <el-select v-model="branchValue" style="width: 100%" placeholder="请选择" v-if="backValue && backValue.id === 1">
              <el-option
                v-for="item in devData"
                :key="item.sha1"
                :label="item.author + '-' + item.sha1 + '-' + item.note + '-' + item.date"
                :value="item.sha1">
              </el-option>
            </el-select>
            <el-select v-model="branchValue" style="width: 100%" placeholder="请选择" v-else>
              <el-option
                v-for="item in masterData"
                :key="item.sha1"
                :label="item.author + '-' + item.sha1 + '-' + item.note + '-' + item.date"
                :value="item.sha1">
              </el-option>
            </el-select>
        </el-form-item>
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleVersionBack">确 定</el-button>
        <el-button @click="backOpen = false; backValue = null">取 消</el-button>
      </div>
  </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      packOpen: false, // 打包弹窗控制
      branchDaata: [
        {
          id: 1,
          branchName: 'develop',
          sha1: '',
          note: '',
          time: false,
          packvisible: true
        },
        {
          id: 2,
          branchName: 'master',
          sha1: '',
          note: '',
          time: false,
          packvisible: false
        }
      ],
      devData: [],
      masterData: [],
      show: false,
      branchValue: null,
      commandValue: null,
      commandData: [
        {
          name: '全部',
          value: '-a'
          // value: 'arg&-a'
        },
        {
          name: '登录',
          value: 'login'
        },
        {
          name: '游戏管理',
          value: 'game'
        },
        {
          name: '支付管理',
          value: 'pay'
        },
        {
          name: '玩家',
          value: 'gamePlayer'
        },
        {
          name: '订单',
          value: 'order'
        },
        {
          name: '404',
          value: '404'
        }
      ],
      backOpen: false,
      backValue: null
    };
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取dev分支列表
    async getList() {
      let devRes = await fetch('http://47.99.91.240:8088/git/log/gameManage?type=develop').then(res => res.json())
      this.devData = devRes.data
      if (devRes && devRes.data && devRes.data.length > 0) {
        this.branchDaata[0].sha1 = devRes.data[0].sha1
        this.branchDaata[0].note = devRes.data[0].note
      }
      let masRes = await fetch('http://47.99.91.240:8088/git/log/gameManage?type=master').then(res => res.json())
      this.masterData = masRes.data
      if (masRes && masRes.data && masRes.data.length > 0) {
        this.branchDaata[1].sha1 = masRes.data[0].sha1
        this.branchDaata[1].note = masRes.data[0].note
      }
      
    },
    // 打包并发布最新
    handlePack(data) {
      this.packData = data
      this.packOpen = true
    },
    handleBack(data) {
      this.backOpen = true
      this.backValue = data
    },
    // 打包并发布
    handlePublish() {
      let data = this.packData
      this.packOpen = false
      console.log('arg&' + this.commandValue.join(' '))
      let params = {ref: data.branchName, commits: [{message: 'arg&' + this.commandValue.join(' ')}] }
      if (data.id === 1) {
        this.branchDaata[0].time = true
      } else {
        this.branchDaata[1].time = true
      }
      fetch('http://47.99.91.240:8088/git/push/gameManage', {
        method: 'POST', 
        headers: {
          'Content-Type': 'application/json',
        },
        timeout: 1000 * 60 * 2,
        body: JSON.stringify(params)
      }).then(res => res.json()).then(res => {
        if (res.code === 200) {
          this.msgSuccess('提交成功')
          this.getList()
          if (data.id === 1) {
            this.branchDaata[0].time = false
          } else {
            this.branchDaata[1].time = false
          }
        }
      })
    },
    // 版本回退
    handleVersionBack() {
      let data = this.backValue
      this.backOpen = false
      let params = {ref: data.branchName, head: this.branchValue, commits: [{message: 'arg&-a'}] }
      if (data.id === 1) {
        this.branchDaata[0].time = true
      } else {
        this.branchDaata[1].time = true
      }
      fetch('http://47.99.91.240:8088/git/push/gameManage', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        timeout: 1000 * 60 * 2,
        body: JSON.stringify(params)
      }).then(res => res.json()).then(res => {
        this.getList()
        if (data.id === 1) {
          this.branchDaata[0].time = false
        } else {
          this.branchDaata[1].time = false
        }
      })
    },
    
    selsectHandle(e) {
      if (e.includes('-a')) {
        this.commandValue = ['-a']
      } else {
        this.commandValue = e
      }
    }
  }
};
</script>