<template>
  <div class="main_root">
    <div class="header">
      <div class="comm">选择动态密码后，需要去设备列表中计算设备密码。</div>
      <div class="comm">
        校级管理员可设置设备管理密码为空，校级密码为空时，该校设备管理密码继承超管设置的设备管理密码。
      </div>
    </div>
    <div class="content">
      <Form ref="addForm" :model="adminInfo" :label-width="100" inline>
        <FormItem label="动态密码" style="width: 100%">
          <RadioGroup v-model="adminInfo.trend">
            <Radio :label="1">是</Radio>
            <Radio :label="0">否</Radio>
          </RadioGroup>
        </FormItem>
        <FormItem label="管理密码" style="width: 100%">
          <Input
            v-model="adminInfo.password"
            placeholder="请输入管理密码"
          ></Input>
        </FormItem>
      </Form>
    </div>
    <div class="footer">
      <Button style="margin-right: 20px" @click="Refresh()">取消</Button>
      <Button @click="saveInfo()" class="primary_btn" type="primary"
        >保存</Button
      >
    </div>
    <Spin fix v-if="showLoading" style="background-color: transparent">
      <Icon type="ios-loading" size="18" class="demo-spin-icon-load"></Icon>
      <div>加载中</div>
    </Spin>
  </div>
</template>

<script>
import {
  password_detail,
  password_edit
} from "@/api/setting";

export default {
  data() {
    return {
      adminInfo: {
        trend: 1,
        password: ""
      },
      userInfo: {},
      showLoading: false
    };
  },
  created() {
    this.userInfo = JSON.parse(
      localStorage.getItem("xh_control_userInfo")
    ).content;
    this.init();
  },
  methods: {
    init() {
      password_detail({}).then((res) => {
        this.showLoading = false;
        if (res.code === 0) {
          if (res.obj) {
            this.adminInfo = {
              trend: res.obj.trend,
              password: res.obj.password
            };
          }
        } else {
          this.$Message.error(res.msg);
        }
      });
    },
    //刷新页面
    Refresh() {
      location.reload();
    },
    saveInfo() {
      if (!this.adminInfo.password) {
        this.$Message.error("请输入管理密码！");
        return;
      }
      let form = {
        trend: this.adminInfo.trend,
        password: this.adminInfo.password
      };
      console.log(form);
      this.showLoading = true;
      password_edit(form).then((res) => {
        this.showLoading = false;
        if (res.code === 0) {
          console.log(res.obj);
          this.$Message.success(res.msg);
        } else {
          this.$Message.error(res.msg);
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.main_root {
  position: relative;
  padding: 40px 100px;
  .header {
    margin-top: 10px;
    width: 100%;
    height: 92px;
    border-radius: 8px;
    border: 1px dashed #ced9f2;
    background-color: #f1f6fc;
    padding: 6px 0;
    .comm {
      padding-left: 20px;
      height: 40px;
      line-height: 40px;
      &::before {
        content: "*" !important;
        display: inline-block;
        margin-right: 4px;
        line-height: 1;
        font-family: SimSun;
        font-size: 14px;
        color: #ed4014;
      }
    }
  }
  .content {
    width: 500px;
    margin: 100px auto 40px;
  }
  .footer {
    text-align: center;
  }
}
</style>
