<template>
  <div>
    <div class="filter">
      <el-input v-model="listQuery.name" placeholder="名称" clearable style="width: 200px;" class="filter-item" />
      <el-select v-model="listQuery.module" placeholder="类型" clearable style="width: 200px" class="filter-item">
        <el-option v-for="item in moduleList" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchIndex">搜索</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit"
        @click="handleCreate">添加</el-button>
    </div>

    <el-table :data="indexList" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="#" prop="id" align="center" width="65">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center">
        <template slot-scope="scope">
          <span class="link-type" @click="openWindow(scope.row)">{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="url" label="地址" align="center" />
      <el-table-column prop="moduleName" label="类型" align="center" />

      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增更新对话框 -->
    <el-dialog :title="dialogName" :visible.sync="dialogFormVisible" @closed="handleClose">
      <el-form ref="dataFormRef" :rules="rules" :model="dataForm" label-position="left" label-width="105px"
        style="width: 90%; margin-left:40px;">
        <el-form-item label="类型" prop="module">
          <el-select v-model="dataForm.module" class="filter-item" placeholder="请选择模块">
            <el-option v-for="item in moduleList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name" style="width:100%">
          <el-input v-model="dataForm.name" />
        </el-form-item>
        <el-form-item label="地址" prop="url" style="width:100%">
          <el-input :autosize="{ minRows: 2, maxRows: 4}" v-model="dataForm.url" type="textarea" placeholder="请输入地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="createOrUpdateNavigate()">确认</el-button>
      </div>
    </el-dialog>

    <!-- 删除对话框 -->
    <el-dialog :visible.sync="dialogDeleteVisible" :data="dataForm" title="删除导航">
      <span>
        确定要删除 {{ dataForm.name }} 吗?
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogDeleteVisible = false">取消</el-button>
        <el-button type="primary" @click="deleteData()">确认</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import {
  getNavigateList,
  getModuleList,
  addNavigate,
  updateNavigate,
  deleteNavigate
} from '@/api/navigate'
export default {
  data() {
    return {
      listQuery: {
        name: '',
        module: null
      },
      indexList: [],
      moduleList: [],
      dataForm: {},
      dialogName: '',
      dialogFormVisible: false,
      dialogDeleteVisible: false,
      rules: {
        module: [{ required: true, message: '模块不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        url: [{ required: true, message: 'url不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getNavigateList()
    this.getModuleList()
  },
  methods: {
    getNavigateList() {
      getNavigateList(this.listQuery).then((resp) => {
        this.indexList = resp.data.data
      })
    },
    getModuleList() {
      getModuleList().then((resp) => {
        this.moduleList = resp.data.data
      })
    },
    searchIndex() {
      this.getNavigateList()
    },
    // 打开新增对话框
    handleCreate() {
      this.dialogName = '新增导航'
      this.dataForm = {}
      this.dialogFormVisible = true
    },
    // 新增/更新导航
    createOrUpdateNavigate() {
      // 表单验证
      this.$refs.dataFormRef.validate((valid) => {
        if (!valid) {
          return false
        } else {
          if (this.dialogName === '新增导航') {
            addNavigate(this.dataForm).then((resp) => {
              if (resp.data.data === true) {
                this.dialogFormVisible = false
                this.getNavigateList()
                this.$message.success('新增导航成功')
              } else {
                return this.$message.error('导航新增失败')
              }
            })
          }
          if (this.dialogName === '更新导航') {
            updateNavigate(this.dataForm).then((resp) => {
              if (resp.data.data === true) {
                this.getNavigateList()
                this.dialogFormVisible = false
                return this.$message.success('更新导航成功')
              } else {
                return this.$message.error('导航更新失败')
              }
            })
          }
        }
      })
    },
    // 打开新窗口
    openWindow(row) {},
    // 打开更新对话框
    handleUpdate(row) {
      console.log(row)
      this.dialogName = '更新导航'
      this.dataForm = Object.assign({}, row)
      this.dialogFormVisible = true
    },
    // 删除
    handleDelete(row) {
      this.dataForm = {}
      this.dataForm = row
      this.dialogDeleteVisible = true
    },
    // 删除导航
    deleteData() {
      var formdata = new FormData()
      formdata.set('id', this.dataForm.id)
      deleteNavigate(formdata).then((resp) => {
        if (resp.data.data === true) {
          this.dialogDeleteVisible = false
          this.getNavigateList()
          return this.$message.success('导航删除成功')
        } else {
          return this.$message.error('导航删除失败')
        }
      })
    },
    // 关闭新增/编辑对话框触发方法
    handleClose() {
      this.$refs.dataFormRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.filter {
  margin-bottom: 20px;
}
</style>
