<template>
  <div>
    <div class="menu-title">
      代码仓库
    </div>
    <div class="menu-content">
      <div>
        <el-form :inline="true" :model="qo" class="demo-form-inline">
          <el-form-item>
            <el-input v-model="qo.LIKES_name" placeholder="请输入仓库名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="qo.LIKES_name" placeholder="请输入仓库地址"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="qo.pageNo=1;queryData()">搜 索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div style="margin: 0px 0 20px 0">
        <el-button type="primary" @click="createRow()">新增</el-button>
      </div>
      <div>
        <el-table :data="result.records" style="width: 100%" ref="table" >
          <el-table-column type="index" label="行号" width="100"></el-table-column>
          <el-table-column prop="name" label="算法仓库名称"></el-table-column>
          <el-table-column prop="gitUrl" label="仓库地址"></el-table-column>
          <el-table-column prop="modifyTimeMillis" label="修改时间">
            <template slot-scope="scope">
              {{ scope.row.modifyTimeMillis | timeFilter }}
            </template>
          </el-table-column>
          <el-table-column prop="createTimeMillis" label="创建时间">
            <template slot-scope="scope">
              {{ scope.row.createTimeMillis | timeFilter }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120">
            <template slot-scope="scope">
              <el-button @click.native.prevent="modifyRow(scope.row)" type="text" size="small">
                编辑
              </el-button>
              <el-button @click.native.prevent="removeRow(scope.row)" type="text" size="small">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page-box">
        <el-pagination background
                       @current-change="handleCurrentChange"
                       :current-page="qo.pageNo"
                       :page-size="qo.pageSize"
                       layout="total, prev, pager, next"
                       :total="result.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog
        :title="dialogName"
        :visible.sync="cmdDialogVisible"
        width="40%">
      <el-form ref="cmd" label-width="100px" :rules="rules" :model="cmd">
        <el-form-item label="算法名称" prop="name">
          <el-input v-model="cmd.name" placeholder="请输入算法名称"></el-input>
        </el-form-item>
        <el-form-item label="仓库地址" prop="gitUrl">
          <el-input v-model="cmd.gitUrl" placeholder="请输入仓库地址"></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="userName">
          <el-input v-model="cmd.userName" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="cmd.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="cmd.remarks" placeholder="请输入描述" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cmdDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitModify">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request2';

var _this;
export default {
  name: "codeWarehouse",
  data() {
    return {
      qo: {
        pageNo: 1,
        pageSize: 10,
        LIKES_name: '',
      },
      result: {
        records: [],
        total: 0
      },
      dialogName: '编辑',
      cmdDialogVisible: false,
      cmd: {},
      rules: {
        name: [
          {required: true, message: '请输入算法名称'}
        ],
        gitUrl: [
          {required: true, message: '请输入仓库地址'}
        ],
        userName: [
          {required: true, message: '请输入账号'}
        ],
        password: [
          {required: true, message: '请输入密码'}
        ],
      },
    }
  },
  mounted() {
    _this = this;
    _this.queryData();
  },
  filters: {
    timeFilter(val) {
      if (val > 0) {
        let dt = new Date(val);
        let year = dt.getFullYear();
        let month = dt.getMonth() + 1;
        let date = dt.getDate();
        let hour = dt.getHours();
        let minute = dt.getMinutes();
        let second = dt.getSeconds();
        return `${year}-${month}-${date} ${hour}:${minute}:${second}`;
      } else {
        return "-";
      }
    }
  },
  methods: {
    queryData() {
      request({
        url: '/repository/list',
        method: 'post',
        data: _this.qo
      }).then(res => {
        _this.result.records = res.list;
        _this.result.total = res.total;
      });
    },
    handleCurrentChange(val) {
      _this.qo.pageNo = val;
      _this.queryData();
    },
    removeRow(item) {
      _this.$confirm('此操作将永久删除记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let id = item.id;
        request({
          url: `/repository/delete/${id}`,
          method: 'get',
        }).then(res => {
          _this.$message.success("删除成功");
          _this.queryData();
        });
      }).catch(() => {
      });
    },
    createRow() {
      _this.cmdDialogVisible = true;
      _this.$nextTick(() => {
        _this.dialogName = "新增";
        _this.cmd = {
          name: '',
          gitUrl: '',
          userName: '',
          password: '',
          remarks: ''
        };
        _this.$refs.cmd.resetFields();
      });
    },
    modifyRow(item) {
      _this.cmdDialogVisible = true;
      _this.$nextTick(() => {
        _this.dialogName = "编辑";
        _this.cmd = JSON.parse(JSON.stringify(item));
        _this.cmd.modifyTimeMillis = null;
        _this.$refs.cmd.resetFields();
      });
    },
    submitModify() {
      this.$refs.cmd.validate((valid) => {
        if (valid) {
          if (_this.cmd.id) {
            request({
              url: '/repository/update',
              method: 'post',
              data: _this.cmd
            }).then(res => {
              _this.$message.success("编辑成功");
              _this.queryData();
              _this.cmdDialogVisible = false;
            });
          } else { // 新建
            request({
              url: '/repository/save',
              method: 'post',
              data: _this.cmd
            }).then(res => {
              _this.$message.success("新增成功");
              _this.queryData();
              _this.cmdDialogVisible = false;
            });
          }
        }
      });
    },
  }
}
</script>

<style lang="scss">
</style>
