<template>
  <div style="align-items: flex-start;min-height:100%" class="bgwhite flex">
    <div class="left-div" style="border-right:0">
      <bc-authority-tree tree-type="common" :height="673" :ztreeNameShow="true" url="api/v1/institutionCompany/tree/"
                         :params="treeParams" :reload.sync="reload"
                         :show-icon="true" :show-line="true" @onClick="treeClick"
                         :selected-id="selectedId"></bc-authority-tree>
    </div>
    <div class="right-div" style="border-left: 1px solid #e8e8e8;">
      <Card style="height: 95%;">
        <Icon size="24" type="md-menu" style="color: #348EED"/>
        <font style="color: #348EED">功能开关</font>
        <Row style="margin: 20px 20px;" type="flex">
          <Col span="4">
            <span style="width: 120px;">记录常用地</span>
          </Col>
          <Col span="6">
            <RadioGroup v-model="nowCommonLoginLandConfig.recordOpen" >
              <Radio :label="'1'">开启</Radio>
              <Radio :label="'0'">关闭</Radio>
            </RadioGroup>
          </Col>
          <Col span="4"></Col>
          <Col span="6">
            <span style="width: 120px;">开启登录限制</span>
          </Col>
          <Col span="4">
            <RadioGroup v-model="nowCommonLoginLandConfig.controlOpen" >
              <Radio :label="'1'">开启</Radio>
              <Radio :label="'0'">关闭</Radio>
            </RadioGroup>
          </Col>
          <Col span="4"></Col>
        </Row>
        <Divider/>
        <Icon size="24" type="md-menu" style="color: #348EED"/>
        <font style="color: #348EED">地址类型</font>
        <Row style="margin: 20px 20px;" type="flex">
          <Col span="4">
            <span style="width: 120px;">常用地类型</span>
          </Col>
          <Col span="6">
            <RadioGroup v-model="nowCommonLoginLandConfig.landType" >
              <Radio :label="'1'">区域地址</Radio>
              <Radio :label="'2'">IP地址</Radio>
            </RadioGroup>
          </Col>
          <Col span="4"></Col>
        </Row>
        <Divider/>
        <Icon size="24" type="md-menu" style="color: #348EED"/>
        <font style="color: #348EED">多常用登录地</font>
        <Row style="margin: 20px 20px;" type="flex">
          <Col span="4">
            <span style="width: 120px;">同时存在/起效</span>
          </Col>
          <Col span="6">
            <RadioGroup v-model="nowCommonLoginLandConfig.landMultipleOpen" >
              <Radio :label="'1'">允许</Radio>
              <Radio :label="'0'">不允许</Radio>
            </RadioGroup>
          </Col>
          <Col span="4"></Col>
          <Col span="4">
            <span style="width: 120px;">限制触发方式</span>
          </Col>
          <Col span="6">
            <RadioGroup v-model="nowCommonLoginLandConfig.landMultipleControlWay" >
              <Radio :label="'1'">所有常用登录地都不符合</Radio>
              <Radio :label="'2'" disabled>任一常用登录地不符合</Radio>
            </RadioGroup>
          </Col>
        </Row>
        <Divider/>
        <Icon size="24" type="md-menu" style="color: #348EED"/>
        <font style="color: #348EED">方式与途径</font>
        <Row style="margin: 20px 20px;" type="flex">
          <Col span="4">
            <span style="width: 120px;">记录方式</span>
          </Col>
          <Col span="7">
            <RadioGroup v-model="nowCommonLoginLandConfig.recordWay" >
              <Radio :label="'1'" disabled>登陆者记录</Radio>
              <Radio :label="'2'" disabled>管理员记录</Radio>
              <Radio :label="'3'">同地登录3次自动记录</Radio>
            </RadioGroup>
          </Col>
          <Col span="3"></Col>
          <Col span="4">
            <span style="width: 120px;">解除限制方式</span>
          </Col>
          <Col span="6">
            <RadioGroup v-model="nowCommonLoginLandConfig.controlUnlockWay" >
              <Radio :label="'1'">二级密码</Radio>
              <Radio :label="'2'" disabled>邮箱验证码</Radio>
              <Radio :label="'3'" disabled>短信验证码</Radio>
            </RadioGroup>
          </Col>
        </Row>
        <Row style="margin: 50px 0;" type="flex">
          <i-col span="6" >
            <Button size="large" @click="saveCommonLoginLandConfig" type="primary" :loading="saveLoading">保存</Button>
          </i-col>
        </Row>
      </Card>
    </div>
  </div>
</template>

<script>
import {
  getCommonLoginLandConfigPage,
  getCommonLoginLandConfigList,
  addCommonLoginLandConfig,
  editCommonLoginLandConfig
} from './commonLoginLandConfig-api'

let $this;
export default {
  name: 'commonLoginLandConfigPage',
  components: {},
  data() {
    return {
      treeParams: {areaId: "0", isDepart: '0'},
      reload: false,
      selectedId: 'first',
      nowCompanyId: null,
      modalParams: {},
      nowCommonLoginLandConfig: {},
      loading: false,
      saveLoading: false,
    }
  },
  methods: {
    treeClick(event, treeId, treeNode) {
      if (treeNode.type === "company") {
        this.nowCompanyId = treeNode.id;
      }
      this.reloadData()
    },
    saveCommonLoginLandConfig() {
      this.saveLoading = true
      if (!this.nowCommonLoginLandConfig.companyId){
        this.$Message.warning("请先选择单位!");
        this.saveLoading = false;
        return
      }
      if (this.nowCommonLoginLandConfig.id) {
        editCommonLoginLandConfig(this.nowCommonLoginLandConfig).then(data => {
          if (!data.status) {
            this.$Message.error(data.error.message);
            this.saveLoading = false;
            console.log(data);
            return false;
          }
          this.$Message.success("操作成功！");
          this.saveLoading = false;
        }).catch(err => {
          this.$Message.error("操作失败！");
          this.saveLoading = false;
          console.log(err);
        })
      } else {
        addCommonLoginLandConfig(this.nowCommonLoginLandConfig).then(data => {
          if (!data.status) {
            this.$Message.error(data.error.message);
            this.saveLoading = false;
            console.log(data);
            return false;
          }
          this.$Message.success("操作成功！");
          this.saveLoading = false;
        }).catch(err => {
          this.$Message.error("操作失败！");
          this.saveLoading = false;
          console.log(err);
        })
      }

    },
    reloadData() {
      getCommonLoginLandConfigPage({companyId: this.nowCompanyId}).then(res => {
        if (res.data.result.datas && res.data.result.datas.length > 0) {
          this.nowCommonLoginLandConfig = res.data.result.datas[0];
        } else {
          this.nowCommonLoginLandConfig = {companyId: this.nowCompanyId};
        }
        this.loading = false;
      }).catch(err => {
        this.tableData = {};
        this.$Message.error('查询失败！')
        console.log(err)
      })
    },
  },
  mounted() {
    $this = this;
  }
}
</script>

<style>

</style>
