<template>
  <div>
    <el-card class="no-bottom">
      <el-form
        ref="searchForm"
        :model="searchForm"
        label-width="80px"
        inline
      >
        <el-form-item
          prop="input"
        >
          <el-input v-model="searchForm.input" placeholder="系统名称/系统代码" />
        </el-form-item>
        <el-form-item
          label="状态:"
          prop="status"
        >
          <el-select v-model="searchForm.status" placeholder="请选择">
            <el-option label="全部" :value="null" />
            <el-option label="正常" :value="0" />
            <el-option label="锁定" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getSystemList(1)">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="margin-top-20">
      <div class="buttons-group" style="margin-bottom: 20px">
        <el-button v-permission="'MenuMgr_add'" type="primary" icon="el-icon-plus" @click="addRow">增加系统</el-button>
      </div>

      <el-table
        :data="tableData"
        border
      >
        <el-table-column
          type="index"
          label="序号"
          width="50px"
          align="center"
        />
        <el-table-column
          prop="systemCode"
          label="系统代码"
        />
        <el-table-column
          prop="systemName"
          label="系统名称"
        />
        <el-table-column
          prop="url"
          label="访问地址"
        />
        <el-table-column
          prop="devUrl"
          label="端口"
        />
        <el-table-column
          prop="status"
          label="状态"
          :formatter="formatterStatus"
        />
        <el-table-column
          label="操作"
          width="140px"
          align="center"
        >
          <template slot-scope="props">
            <el-button v-permission="'SysMgr_update'" type="text" @click="editItem(props.row)">编辑</el-button>
            <el-popconfirm
              title="确定要删除此条记录吗？"
              @onConfirm="deleteItem(props.row.id)"
            >
              <el-button slot="reference" type="text">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 20px;text-align: right"
        :current-page="pageForm.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageForm.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <el-dialog
      :title="add ? '增加系统' : '编辑系统'"
      :visible.sync="dialogVisible"
      width="700px"
      :before-close="beforeClose"
    >
      <el-form
        ref="addForm"
        :model="addForm"
        label-width="90px"
        :rules="rules"
      >
        <el-form-item
          label="系统代码:"
          prop="systemCode"
        >
          <el-input v-model="addForm.systemCode" />
        </el-form-item>
        <el-form-item
          label="系统标题:"
          prop="systemName"
        >
          <el-input v-model="addForm.systemName" />
        </el-form-item>
        <el-form-item
          label="访问地址:"
          prop="url"
        >
          <el-input v-model="addForm.url" />
        </el-form-item>
        <el-form-item
          label="端口:"
          prop="devUrl"
        >
          <el-input v-model="addForm.devUrl" />
        </el-form-item>
        <el-form-item
          label="状态:"
        >
          <el-radio-group v-model="addForm.status">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">锁定</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="saveSystem">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getSystemList, addSystem, updateSystem, deleteSystem } from '@/api/system-management/system-management'

export default {
  name: 'Index',
  data() {
    return {
      searchForm: {
        input: null,
        status: null
      },
      pageForm: {
        pageSize: 10,
        pageNum: 1,
        total: 0
      },
      tableData: [],
      dialogVisible: false,
      add: true,
      addForm: {
        systemCode: null,
        systemName: null,
        url: null,
        devUrl: null,
        status: 0
      },
      system: null,
      rules: {
        systemCode: [
          { required: true, message: '请输入系统代码', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        systemName: [
          { required: true, message: '请输入系统标题', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请输入访问地址', trigger: 'blur' }
        ],
        devUrl: [
          { required: true, message: '请输入端口', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getSystemList()
  },
  methods: {
    resetFields() {
      this.addForm = {
        icon: null,
        systemName: null,
        url: null,
        status: 0
      }
      this.$refs.addForm.clearValidate()
    },
    beforeClose(done) {
      this.resetFields()
      this.$nextTick()
        .then(_ => {
          done()
        })
    },
    closeDialog() {
      this.resetFields()
      this.$nextTick()
        .then(_ => {
          this.dialogVisible = false
        })
    },
    saveSystem() {
      this.$refs.addForm.validate()
        .then(_ => {
          const { systemCode, systemName, url, devUrl, status } = this.addForm
          const params = {
            systemCode,
            systemName,
            url,
            devUrl,
            status
          }
          if (this.add) {
            addSystem(params)
              .then(_ => this.$message.success('操作成功'))
              .then(_ => this.getSystemList())
              .then(_ => this.closeDialog())
              .catch(err => {
                this.$message.error('操作失败')
                console.error(err.message)
              })
          } else {
            params.id = this.system.id
            updateSystem(params)
              .then(res => this.$message.success('操作成功'))
              .then(_ => this.getSystemList())
              .then(_ => this.closeDialog())
              .catch(err => {
                this.$message.error('操作失败')
                console.error(err.message)
              })
          }
        })
        .catch(_ => {})
    },
    resetForm() {
      this.$refs.searchForm.resetFields()
    },
    formatterStatus(row, column, cellValue) {
      return (cellValue !== 1) ? '正常' : '锁定'
    },
    getSystemList(pageNum) {
      const params = {
        current: pageNum,
        size: this.pageForm.pageSize,
        systemName: this.searchForm.input,
        status: this.searchForm.status
      }
      getSystemList(params)
        .then(res => {
          const { data } = res
          this.tableData = data.records
          this.pageForm.total = data.total
        })
        .catch(err => {
          this.$message.error('操作失败')
          console.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.pageForm.pageSize = val
      this.getSystemList(this.pageForm.pageNum)
    },
    handleCurrentChange(val) {
      this.pageForm.pageNum = val
      this.getSystemList(val)
    },
    editItem(row) {
      this.system = row
      this.add = false
      const { systemCode, systemName, url, devUrl, status } = row
      this.addForm = {
        systemCode,
        systemName,
        url,
        devUrl,
        status
      }
      this.dialogVisible = true
    },
    deleteItem(id) {
      deleteSystem({ id: id })
        .then(res => this.$message.success('操作成功'))
        .then(_ => {
          this.getSystemList()
        })
        .catch(err => {
          this.$message.error('操作失败')
          console.error(err.message)
        })
    },
    addRow() {
      this.add = true
      this.dialogVisible = true
    }
  }
}
</script>

<style scoped>

</style>
