<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-form ref="form" :model="form" label-width="250px" class="form-page">
        <el-form-item label="登录账号" v-show="isAddOrEdit">
          <el-input v-model="form.adminName" :disabled="isEdit"></el-input>
        </el-form-item>
        <el-form-item label="姓名" v-show="isAddOrEdit">
          <el-input v-model="form.realName"></el-input>
        </el-form-item>
        <el-form-item label="角色" v-show="isAddOrEdit">
          <el-select v-model="form.roleId" placeholder="请选择角色">
            <el-option v-for="item in roles" :label="item.ROLE_NAME" :value="item.ID" :key="item.ID"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input type="password" v-model="form.surePassword"></el-input>
        </el-form-item>
        <el-form-item label="绑定IP" v-show="isAddOrEdit">
          <el-input
                    type="textarea"
                    :rows="4"
                    placeholder="请输入内容"
                    v-model="form.bindIp">
          </el-input>
          <el-tag type="info">一行一个IP；可指定ip段，如192.168.0.1-255</el-tag>
        </el-form-item>
        <el-form-item label="备注" v-show="isAddOrEdit">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" v-show="isAddOrEdit">
          <el-switch v-model="form.isEnable"></el-switch>
        </el-form-item>
        <el-form-item label="是否修改密码" v-show="isAddOrEdit">
          <el-switch v-model="form.isModifyPassword"></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="submitButtonStat" @click="onSubmit">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import store from '@/utils/vuexStore'
import network from '@/utils/network'
import tool from '@/utils/tool'

export default {
  name: 'role-add',
  mounted () {
    if (this.$route.name === 'admin_edit') {
      network.getData('admin/edit/' + this.$route.params.id).then(response => {
        this.form = response.oneData
        this.roles = response.adminRoles
        this.loading = false
        this.isEdit = true
      })
    } else if (this.$route.name === 'admin_add'){
      network.getData('admin/add').then(response => {
        this.roles = response.adminRoles
        this.loading = false
      })
    }else {
      this.loading = false
    }
  },
  data () {
    return {
      form: {
        adminName: null,
        realName: null,
        remark: null,
        isEnable: true,
        isModifyPassword: false,
        bindIp: '',
        roleId: null,
        password: null,
        surePassword: null
      },
      loading: true,
      submitButtonStat: false,
      isEdit: false,
      roles: null,
    }
  },
  computed: {
    isAddOrEdit () {
      if (this.$route.name === 'admin_add' || this.$route.name === 'admin_edit') {
        return true
      }
      return false
    },
  },
  watch: {
    '$route' () {
      for (let i in this.form) {
        this.form[i] = null
      }
    }
  },
  methods: {
    onSubmit () {
      this.submitButtonStat = true
      let path = 'admin/add'
      if (this.$route.name === 'admin_edit') {
        path = 'admin/edit/' + this.$route.params.id
      }
      else if (this.$route.name === 'admin_change-password') {
        path = 'admin/change-password'
      }
      network.postData(path, this.form).then(response => {
        this.submitButtonStat = false
        this.$message({
          message: response,
          type: 'success'
        })
        this.$router.go(-1)
      }).catch(() => {
        this.submitButtonStat = false
      })
    }
  }
}
</script>

<style scoped>

</style>
