<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" icon="el-icon-circle-plus" @click="DialogAddVisible=true">新增</el-button> -->

        <el-button type="primary" icon="el-icon-circle-plus" @click="DialogAddVisible=true">新增</el-button>
      </div>
      <el-table :data="projectList" border style="width: 100%;margin-top:20px">
        <el-table-column type='index' width="50px">
          <template slot-scope="scope">
            <span>{{  (queryModuleInfo.pageNum -1 ) * queryModuleInfo.pageSize + scope.$index + 1}}</span>
          </template>
        </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 label="操作">
          <slot slot-scope="scope">
            <el-button type="warning" icon="el-icon-edit" @click="openEditDialog(scope.row)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" @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="DialogAddVisible" @close="closeAddDialog" width="30%">
      <el-form ref="addProjectFormRef" :rules="rules" :model="addProjectForm" label-width="90px">
        <el-form-item label="项目名称:" prop="name" style="width:400px">
          <el-input v-model="addProjectForm.name"></el-input>
        </el-form-item>
        <el-form-item label="项目描述:" prop="description" style="width:400px">
          <el-input v-model="addProjectForm.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="DialogAddVisible=false">取 消</el-button>
        <el-button type="primary" @click="addPojectFrom">确 定</el-button>
      </span>
    </el-dialog>
    <!--编辑页面-->
    <el-dialog title="编辑项目" :visible.sync="DialogEditVisible" width="30%">
      <el-form ref="editProjectFormRef" :rules="rules" :model="editProjectForm" label-width="90px">
        <el-form-item label="项目名称:" prop="name" style="width:400px">
          <el-input v-model="editProjectForm.name"></el-input>
        </el-form-item>
        <el-form-item label="项目描述:" prop="description" style="width:400px">
          <el-input v-model="editProjectForm.description"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="DialogEditVisible=false">取 消</el-button>
        <el-button type="primary" @click="editPojectFrom">确 定</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,
      DialogAddVisible: false,
      DialogEditVisible: false,
      addProjectForm: {
        name: '',
        description: ''
      },
      editProjectForm: {
        id: '',
        name: '',
        description: ''
      },
      delProjectParam: {
        projectId: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '项目名称不能为空！',
            trigger: 'blur'
          }
        ],
        description: [
          {
            required: true,
            message: '项目描述不能为空！',
            trigger: 'blur'
          }
        ]
      },
      clearMsg: false
    }
  },
  created() {
    this.getProjectList()
  },
  methods: {
    // 获取项目列表
    getProjectList() {
      getProjectList(this.queryModuleInfo).then((resp) => {
        this.projectList = resp.data.data.list
        this.totalNum = resp.data.data.total
      })
    },
    // 查询按钮
    searchMockList() {},
    // 新增确定按钮
    addPojectFrom() {
      this.$refs.addProjectFormRef.validate((valid) => {
        if (!valid) {
          return false
        } else {
          // 添加接口
          addProject(this.addProjectForm)
            .then((resp) => {
              if (resp.data.data === true) {
                this.DialogAddVisible = false
                this.getProjectList()
                this.$message.success('新增成功！')
              } else {
                this.$message.error(resp.data.msg)
              }
            })
            .catch((error) => {
              this.$message({ type: 'error', message: error })
            })
        }
      })
    },
    // 编辑确定按钮
    editPojectFrom() {
      // 编辑接口
      this.$refs.editProjectFormRef.validate((valid) => {
        if (!valid) {
          return false
        } else {
          editProject(this.editProjectForm)
            .then((resp) => {
              if (resp.data.data === true) {
                this.DialogEditVisible = 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 })
            })
        }
      })
    },
    // 对话框关闭后清空文本框数据
    closeAddDialog() {
      this.$refs.addProjectFormRef.resetFields()
    },
    // 编辑信息回显
    openEditDialog(row) {
      this.DialogEditVisible = true
      this.editProjectForm.id = row.id
      this.editProjectForm.name = row.name
      this.editProjectForm.description = row.description
    },
    // 对话框方法
    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>
