<style lang="scss">
.add {
  .el-input,
  button {
    width: 300px;
  }
}
</style>
<template>
  <section class="add">
    <p>系统管理 > 新增账户</p>
    <div class="content">
      <el-form label-width="80px">
        <el-form-item label="账号">
          <el-input placeholder="账号" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" placeholder="密码" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input type="password" placeholder="确认密码" v-model="form.repassword"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input placeholder="姓名" v-model="form.realname"></el-input>
        </el-form-item>
        <el-form-item label="手机">
          <el-input placeholder="手机" v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="权限">
          <el-select v-model="permission" multiple placeholder="请选择">
            <el-option
              v-for="(item,index) in permissions"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态">
          <el-radio v-model="form.status" :label="1">激活</el-radio>
          <el-radio v-model="form.status" :label="2">禁用</el-radio>
        </el-form-item>
        <el-form-item label=" ">
          <el-button type="primary" @click="save">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
  </section>
</template>
<script>
export default {
  data() {
    return {
      test: "",
      form: { name: "", realname: "", password:"",repassword:"",permissions:[],status: 1 },
      permissions:[],
      permission:[],
    }
  },
  methods: {
    getData() {
      this.$api.getAccountById({ id: this.form.id }).then(res => {
        this.form = res.data.data;
        if(res.data.data.permissions){
          this.permission=res.data.data.permissions.split(',')
        }
      });

    },
    save() {
      var parm = this.form;
      parm.permissions=this.permission.join(',')
      if (parm.id) {
        // debugger;
        this.$api.editAccount(parm).then(res => {
          if (res.data.code == 0) {
            this.$message({ message: "修改成功！", type: "success" });
            this.$router.push({ path: "/system" });
          } else {
            this.$message.error(res.data.message);
          }
        });
      } else {
        this.$api.addAccount(parm).then(res => {
          if (res.data.code == 0) {
            this.$message({ message: "添加成功！", type: "success" });
            this.$router.push({ path: "/system" });
          } else {
            this.$message.error(res.data.message);
          }
        });
      }
    }
  },
  created() {
    this.$api.getPerList().then(res=>{
      this.permissions=res.data.data;
    })
    if (this.$route.query.id) {
      this.form.id = this.$route.query.id;
      this.id = this.$route.query.id;
      this.getData();
    }
  }
};
</script>





