<template>
  <div>
    <el-card>
      <div class="filter">
        <!-- <el-input placeholder="请输入项目名称" style="width:15%"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="searchMockList">查询</el-button> -->
        <el-button type="primary" @click="DialogVisible = true">新增</el-button>
      </div>
      <el-table :data="projectList" border style="width: 100%;margin-top:20px">
        <!-- <el-table-column prop="id" label="序号" width="80px"></el-table-column> -->
        <el-table-column type='index' width="50px"></el-table-column>
        <el-table-column prop="name" label="名称" width="200px"></el-table-column>
        <el-table-column prop="description" label="描述" width="300px"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="200px"></el-table-column>
        <el-table-column prop="updateTime" label="修改时间" width="200px"></el-table-column>
        <el-table-column label="操作">
          <slot slot-scope="scope">
            <el-button type="success" @click="openEditDialog(scope.row)">编辑</el-button>
            <el-button type="danger" @click="delProjectFrom(scope.row)">删除</el-button>
          </slot>
        </el-table-column>
      </el-table>
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="queryModuleInfo.pageNum" :page-size="queryModuleInfo.pageSize" layout="total, prev, pager, next"
        :total="totalNum">
      </el-pagination>
    </el-card>
    <!-- 新增页面 /编辑页面-->
    <el-dialog title="新增项目" :visible.sync="DialogVisible" width="30%">
      <el-form ref="projectForm" :rules="rules" :model="projectForm" label-width="90px">
        <el-form-item label="项目名称:" prop="name" style="width:400px">
          <el-input v-model="projectForm.name"></el-input>
        </el-form-item>
        <el-form-item label="项目描述:" prop="description" style="width:400px">
          <el-input v-model="projectForm.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="DialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addPojectFrom">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getProjectList,
  addProject,
  editProject,
  delProject
} from '@/api/getAotoInterface'
export default {
  data() {
    return {
      queryModuleInfo: {
        pageNum: 1,
        pageSize: 10
      },
      projectList: [],
      totalNum: 0,
      DialogVisible: false,
      projectForm: {
        name: '',
        description: ''
      },
      delProjectParam: {
        projectId: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '项目名称不能为空！',
            trigger: 'blur'
          }
        ],
        description: [
          {
            required: true,
            message: '项目描述不能为空！',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getProjectList()
  },
  methods: {
    // 获取项目列表
    getProjectList() {
      getProjectList(this.queryModuleInfo).then((resp) => {
        this.projectList = resp.data.data.list
        this.totalNum = resp.data.data.total
      })
    },
    // 查询按钮
    searchMockList() {},
    // 新增确定按钮
    addPojectFrom() {
      if (this.projectForm.id) {
        // 编辑接口
        editProject(this.projectForm)
          .then((resp) => {
            if (resp.data.data === true) {
              this.DialogVisible = false
              this.$message.success('修改成功！')
              this.getProjectList()
            } else if (resp.data.data === false) {
              this.$message.error(resp.data.msg)
            }
          })
          .catch((error) => {
            this.$message({ type: 'error', message: error })
          })
      } else {
        // 添加接口
        addProject(this.projectForm)
          .then((resp) => {
            if (resp.data.data === true) {
              this.DialogVisible = false
              this.$message.success('新增成功！')
              this.projectForm.name = ''
              this.projectForm.description = ''
              this.getProjectList()
            } else {
              this.$message.error(resp.data.msg)
              this.projectForm.name = ''
              this.projectForm.description = ''
            }
          })
          .catch((error) => {
            this.$message({ type: 'error', message: error })
          })
      }
    },
    // 编辑信息回显
    openEditDialog(row) {
      this.DialogVisible = true
      this.projectForm = row
    },
    // 对话框方法
    openMessage(message, confirmText, doit) {
      this.$messageBox
        .confirm(message, '确定', {
          cancelButtonText: '取消',
          confirmButtonText: confirmText,
          type: 'warning'
        })
        .then(() => {
          doit()
        })
        .catch(() => {})
    },
    // 删除项目
    delProjectFrom(row) {
      this.delProjectParam.projectId = row.id
      this.openMessage('您确定要删除吗？', '删除', () => {
        delProject(this.delProjectParam)
          .then((resp) => {
            if (resp.data.data === true) {
              this.$message.success('删除成功！')
              this.getProjectList()
            } else if (resp.data.data === false) {
              this.$message.error(resp.data.msg)
            }
          })
          .catch((error) => {
            this.$message({ type: 'error', message: error })
          })
      })
    },
    // 分页展示
    handleSizeChange(newSize) {
      this.queryModuleInfo.pageSize = newSize
      this.getProjectList()
    },
    // 分页展示
    handleCurrentChange(newPage) {
      this.queryModuleInfo.pageNum = newPage
      this.getProjectList()
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 20px;
}
.el-select {
  margin-right: 20px;
}
</style>
