<template>
  <div>
    <el-card>
      <div class="filter">
        <el-button type="primary" icon="el-icon-circle-plus" @click="DialogVisible = true">新增</el-button>
      </div>
      <el-table :data="modelList" 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="140px"></el-table-column>
        <el-table-column prop="projectName" label="所属项目" width="140px"></el-table-column>
        <el-table-column prop="domain" label="域名" width="260px"></el-table-column>
        <el-table-column prop="description" label="描述" width="140px"></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="warning" icon="el-icon-edit" @click="openEditDialog(scope.row)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" @click="delModelFrom(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" @close="closeAddDialog" width="30%">
      <el-form ref="modelFormRef" :rules="rules" :model="modelForm" label-width="90px">
        <el-form-item label="模块名称:" prop="name" style="width:400px">
          <el-input v-model="modelForm.name"></el-input>
        </el-form-item>
        <el-form-item label="所属项目:" prop="projectId">
          <el-select v-model="modelForm.projectId" placeholder="请选择项目" clearable style="width:310px">
            <el-option v-for="item in projectList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模块描述:" prop="description" style="width:400px">
          <el-input v-model="modelForm.description"></el-input>
        </el-form-item>
        <el-form-item label="域名:" prop="domain" style="width:400px">
          <el-input v-model="modelForm.domain"></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="addModelFrom">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getModulList,
  getProjectList,
  addModul,
  editModel,
  delModel
} from '@/api/getAotoInterface'
export default {
  data() {
    return {
      queryModuleInfo: {
        pageNum: 1,
        pageSize: 10
      },
      totalNum: 0,
      modelList: [],
      projectList: [],
      modelForm: {
        name: '',
        projectId: '',
        description: '',
        domain: ''
      },
      DialogVisible: false,
      delModelParam: {
        moduleId: ''
      },
      // 验证
      rules: {
        name: [
          {
            required: true,
            message: '模块名称不能为空！',
            trigger: 'blur'
          }
        ],
        projectId: [
          {
            required: true,
            message: '模块名称不能为空！',
            trigger: 'blur'
          }
        ],
        description: [
          {
            required: true,
            message: '模块描述不能为空！',
            trigger: 'blur'
          }
        ],
        domain: [
          {
            required: true,
            message: '域名不能为空！',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getModelList()
    this.getProjectList()
  },
  methods: {
    // 获取模块列表
    getModelList() {
      getModulList(this.queryModuleInfo).then((resp) => {
        this.modelList = resp.data.data.list
        this.totalNum = resp.data.data.total
      })
    },
    // 获取项目列表
    getProjectList() {
      getProjectList(this.queryModuleInfo).then((resp) => {
        this.projectList = resp.data.data.list
      })
    },
    // 分页展示
    handleSizeChange(newSize) {
      this.queryModuleInfo.pageSize = newSize
      this.getModelList()
    },
    // 分页展示
    handleCurrentChange(newPage) {
      this.queryModuleInfo.pageNum = newPage
      this.getModelList()
    },

    // 添加模块按钮
    addModelFrom() {
      this.$refs.modelFormRef.validate((valid) => {
        if (!valid) {
          return false
        } else {
          if (this.modelForm.id) {
            editModel(this.modelForm).then((resp) => {
              if (resp.data.data === true) {
                this.DialogVisible = false
                this.$message.success('修改成功！')
                this.getModelList()
              } else {
                this.$message.error(resp.data.msg)
              }
            })
          } else {
            // 添加模块
            addModul(this.modelForm)
              .then((resp) => {
                console.log('保存信息', this.modelForm)
                if (resp.data.data === true) {
                  this.DialogVisible = false
                  this.$message.success('新增成功！')
                  this.modelList.name = ''
                  this.modelList.projectName = ''
                  this.modelList.description = ''
                  this.modelList.domain = ''
                  this.getModelList()
                } else {
                  this.$message.error(resp.data.msg)
                }
              })
              .catch((error) => {
                this.$message({ type: 'error', message: error })
              })
          }
        }
      })
    },
    // 编辑模块
    openEditDialog(row) {
      this.DialogVisible = true
      this.modelForm = row
    },
    // 对话框方法
    openMessage(message, confirmText, doit) {
      this.$messageBox
        .confirm(message, '确定', {
          cancelButtonText: '取消',
          confirmButtonText: confirmText,
          type: 'warning'
        })
        .then(() => {
          doit()
        })
        .catch(() => {})
    },
    closeAddDialog() {
      this.$refs.modelFormRef.resetFields()
    },
    // 删除模块
    delModelFrom(row) {
      this.delModelParam.moduleId = row.id
      this.openMessage('您确定要删除吗？', '删除', () => {
        delModel(this.delModelParam)
          .then((resp) => {
            if (resp.data.data === true) {
              this.$message.success('删除成功！')
              this.getModelList()
            } else if (resp.data.data === false) {
              this.$message.error(resp.data.msg)
            }
          })
          .catch((error) => {
            this.$message({ type: 'error', message: error })
          })
      })
    }
  }
}
</script>
