<template>
  <a-card :bordered="false">
    <div>
      <a-tabs defaultActiveKey="1" tabPosition="left">
        <a-tabPane tab="密码策略" key="1">
          <div class="content">
            <div class="content-item">
              <div class="content-title">是否开启密码策略 ：</div>
              <a-switch :defaultChecked="pwdPolicy.status" v-model="pwdPolicy.status" @change="onChangePwd"></a-switch>
            </div>
            <div class="content-item">
              <div class="content-title">密码类型：</div>
              <a-radio-group :defaultValue="pwdConfig.pwdType" v-model="pwdConfig.pwdType" @change="onChangePwdType">
                <a-radio value="number">{{ textList.one }}</a-radio>
                <a-radio value="character">{{ textList.two }}</a-radio>
                <a-radio value="numAndChar">{{ textList.three }}</a-radio>
                <a-radio value="numAndCharAndSpecial">{{ textList.four }}</a-radio>
              </a-radio-group>
            </div>
            <div class="content-item">
              <div class="content-title">密码长度位数 ：</div>
              <a-inputNumber :defaultValue="pwdConfig.pwdLengthMin" v-model="pwdConfig.pwdLengthMin" @change="onChangePwdLengthMin" :min="1" :max="10"></a-inputNumber>
              &nbsp; - &nbsp;
              <a-inputNumber :defaultValue="pwdConfig.pwdLengthMax" v-model="pwdConfig.pwdLengthMax" @change="onChangePwdLengthMax" :min="1" :max="20"></a-inputNumber>
            </div>
            <!--<div class="content-item">-->
              <!--<div class="content-title">新旧密码差异位数 ：</div>-->
              <!--<a-inputNumber :defaultValue="pwdConfig.difference" v-model="pwdConfig.difference" :max="10"></a-inputNumber>-->
            <!--</div>-->
            <div class="content-item">
              <div class="content-title">密码有效期月数 ：</div>
              <a-inputNumber :defaultValue="pwdConfig.effectiveTime" v-model="pwdConfig.effectiveTime" :min="1" @change="onChangePwdEffectiveTime" ></a-inputNumber>
              <div class="content-title">过期提醒天数 ：</div>
              <a-inputNumber :defaultValue="pwdConfig.remindDays" v-model="pwdConfig.remindDays" :min="1" @change="onChangePwdRemindDays"></a-inputNumber>
            </div>
          </div>
        </a-tabPane>
        <a-tabPane tab="登录策略" key="2">
          <div class="content">
            <div class="content-item">
              <div class="content-title">是否开启登录策略 ：</div>
              <a-switch :defaultChecked="loginPolicy.status" v-model="loginPolicy.status"  @change="onChangeLogin"></a-switch>
            </div>
            <div class="content-item">
              <div class="content-title">登录错误次数锁定账户 ：</div>
              <a-inputNumber :defaultValue="loginConfig.errorCount" v-model="loginConfig.errorCount" :min="1" :max="10" @change="onChangeErrorCount"></a-inputNumber>
              <div class="content-title">锁定时间：</div>
              <a-inputNumber :defaultValue="loginConfig.lockNumber" v-model="loginConfig.lockNumber" :min="1" :max="10" @change="onChangeLockNumber"></a-inputNumber>
              <a-select :defaultValue="loginConfig.lockTime" v-model="loginConfig.lockTime" @change="onChangeLockTime">
                <a-select-option value="day">天</a-select-option>
                <a-select-option value="hour">时</a-select-option>
                <a-select-option value="minute">分</a-select-option>
              </a-select>
            </div>
            <!--<div class="content-item">-->
              <!--<div class="content-title">登录失效时间 ：</div>-->
              <!--<a-inputNumber :defaultValue="loginConfig.expiresNumber" :min="1" :max="10" @change="onChangeExpiresNumber"></a-inputNumber>-->
              <!--<a-select :defaultValue="loginConfig.expiresTime" slot="addonAfter" @change="onChangeExpiresTime">-->
                <!--<a-select-option value="day">天</a-select-option>-->
                <!--<a-select-option value="hour">时</a-select-option>-->
              <!--</a-select>-->
            <!--</div>-->
            <div class="content-item">
              <div class="content-title">首次登录需要更改密码 ：</div>
              <a-switch :defaultChecked="loginConfig.changePwd"  v-model="loginConfig.changePwd" @change="onChangeFirstChangePwd"></a-switch>
            </div>
            <!--<div class="content-item">-->
              <!--<div class="content-title">同一IP允许重复登录 ：</div>-->
              <!--<a-switch :defaultChecked="loginConfig.allowOneIPRelogin"@change="onChangeAllowIPRelogin"></a-switch>-->
            <!--</div>-->
          </div>
        </a-tabPane>
        <a-tabPane tab="IP限制策略" key="3">
          <div class="content">
            <div class="content-item">
              <div class="content-title">是否开启IP限制策略 ：</div>
              <a-switch :defaultChecked="ipValue" v-model="ipValue" @change="onChangeIP" ></a-switch>
            </div>
            <!--<div>-->
              <!--<div class="content-item">-->
                <!--<div class="content-title">IP白名单 ：</div>-->
                <!--<div>-->
                  <!--<a-button type="primary" @click="$refs.createForm.handleAdd('1')">添加</a-button>-->
                  <!--<a-button type="danger" :disabled="multiple" @click="handleDeleteIP">删除</a-button>-->
                <!--</div>-->
              <!--</div>-->
              <!--<div class="content-table">-->
                <!--<a-table-->
                  <!--:loading="whiteLoading"-->
                  <!--:size="tableSize"-->
                  <!--rowKey="id"-->
                  <!--:columns="columns"-->
                  <!--:data-source="whiteList"-->
                  <!--:row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"-->
                  <!--:pagination="false"/>-->
              <!--</div>-->
            <!--</div>-->
            <div>
              <div class="content-item">
                <div class="content-title">IP黑名单 ：</div>
                <div class="content-button">
                  <a-button type="primary" @click="$refs.createForm.handleAdd('2')">添加</a-button>
                  <a-button type="danger" :disabled="multiple2" @click="handleDeleteIP2">删除</a-button>
                </div>
              </div>
              <div class="content-table">
                  <a-table
                    :loading="blackLoading"
                    :size="tableSize"
                    rowKey="id"
                    :columns="columns"
                    :data-source="blackList"
                    :row-selection="{ selectedRowKeys: selectedRowKeys2, onChange: onSelectChange2 }"
                    :pagination="false"/>
              </div>
            </div>
          </div>
        </a-tabPane>
      </a-tabs>
    </div>
     <!-- 增加修改 -->
      <create-form
        ref="createForm"
        @ok="getIPList"
      />
  </a-card>

</template>
<script>
import { savePolicy, getPolicyByType } from '@/api/security/policy'
import { listIp, delIp } from '@/api/security/ip'
import CreateForm from '@/views/security/ip/modules/CreateForm'

export default {
  name: 'SafePolicy',
  components: {
    CreateForm
  },
  data () {
    return {
        check: ['4'],
        selectedRowKeys: [],
        selectedRowKeys2: [],
        loginValue: false,
        ipValue: false,
         // 非单个禁用
        single: true,
        single2: true,
        // 非多个禁用
        multiple: true,
        multiple2: true,
        ids: [],
        ids2: [],
        ips: [],
        ips2: [],
        whiteTotal: 0,
        whiteLoading: false,
        blackTotal: 0,
        blackLoading: false,
        whiteList: [],
        blackList: [],
        // 查询参数
        queryParam: {
          ipType: null,
          ipAddr: null,
          pageNum: 1,
          pageSize: 10
        },
        pwdPolicy: {
          pType: '1',
          status: false,
          config: {}
        },
        pwdConfig: {
          pwdType: 'character',
          pwdLengthMin: 1,
          pwdLengthMax: 10,
          difference: 3,
          effectiveTime: 24,
          remindDays: 30
        },
        loginPolicy: {
          pType: '2',
          status: false,
          config: {}
        },
        loginConfig: {
          errorCount: 5,
          lockNumber: 1,
          lockTime: 'day',
          expiresNumber: 2,
          expiresTime: 'day',
          changePwd: false,
          allowOneIPRelogin: false
        },
        columns: [
            {
                title: '序号',
                dataIndex: 'index',
                key: 'index',
                align: 'center',
                width: '240px',
                customRender: (text, record, index) => `${index + 1}`
            },
            {
              title: 'IP地址',
              dataIndex: 'ipAddr',
              ellipsis: true,
              align: 'center'
            },
            {
              title: '备注',
              dataIndex: 'remark',
              ellipsis: true,
              align: 'center'
            }
        ],
        textList: {
            one: '纯数字',
            two: '纯字母',
            three: '纯数字+纯字母',
            four: '纯数字+纯字母+特殊符号'
        }
    }
  },
  mounted () {
    this.getIPList()
    this.getPolicyPwd()
    this.getPolicyLogin()
    this.getPolicyIP()
  },
  methods: {
    /** 密码策略  */
     getPolicyPwd () {
      getPolicyByType('1').then(response => {
        this.pwdPolicy.status = JSON.parse(response.data.status)
        if (response.data.config) {
          this.pwdConfig = JSON.parse(response.data.config)
        }
        this.pwdPolicy.config = this.pwdConfig
      })
    },
    onChangePwd (checked) {
      // 策略类别(1.密码2.登录3.IP)
      this.pwdPolicy.status = checked
      this.savePolicyPwd()
    },
    onChangePwdType (e) {
      this.pwdConfig.pwdType = e.target.value
      this.savePolicyPwd()
    },
    onChangePwdLengthMin (value) {
      this.pwdConfig.pwdLengthMin = value
      this.savePolicyPwd()
    },
    onChangePwdLengthMax (value) {
      this.pwdConfig.pwdLengthMax = value
      this.savePolicyPwd()
    },
    onChangePwdEffectiveTime (value) {
      this.pwdConfig.effectiveTime = value
      this.savePolicyPwd()
    },
    onChangePwdRemindDays (value) {
      this.pwdConfig.remindDays = value
      this.savePolicyPwd()
    },
    savePolicyPwd () {
      this.pwdPolicy.config = JSON.stringify(this.pwdConfig)
      savePolicy(this.pwdPolicy).then(response => {
        // this.$message.success(
        //   '修改成功',
        //   3
        // )
      })
    },
    /**   登录策略     */
     getPolicyLogin () {
      getPolicyByType('2').then(response => {
        this.loginPolicy.status = JSON.parse(response.data.status)
        if (response.data.config) {
          this.loginConfig = JSON.parse(response.data.config)
        }
        this.loginPolicy.config = this.loginConfig
      })
    },
    onChangeLogin (checked) {
      // 策略类别(1.密码2.登录3.IP)
      this.loginPolicy.status = checked
      this.savePolicyLogin()
    },
    onChangeErrorCount (value) {
        this.loginConfig.errorCount = value
        this.savePolicyLogin()
    },
    onChangeLockNumber (value) {
        this.loginConfig.lockNumber = value
        this.savePolicyLogin()
    },
    onChangeLockTime (value) {
        this.loginConfig.lockTime = value
        this.savePolicyLogin()
    },
    onChangeExpiresNumber (value) {
        this.loginConfig.expiresNumber = value
        this.savePolicyLogin()
    },
    onChangeExpiresTime (value) {
        this.loginConfig.expiresTime = value
        this.savePolicyLogin()
    },
    // 首次登录需要更改密码
    onChangeFirstChangePwd (checked) {
        // 策略类别(1.密码2.登录3.IP)
        this.loginConfig.changePwd = checked
        this.savePolicyLogin()
    },
     // 同一IP允许重复登录
    onChangeAllowIPRelogin (checked) {
        // 策略类别(1.密码2.登录3.IP)
      this.loginConfig.allowOneIPRelogin = checked
      this.savePolicyLogin()
    },
    savePolicyLogin () {
      this.loginPolicy.config = JSON.stringify(this.loginConfig)
      savePolicy(this.loginPolicy).then(response => {
        // this.$message.success(
        //   '修改成功',
        //   3
        // )
      })
    },
     /** IP限制策略  */
    getPolicyIP () {
      getPolicyByType('3').then(response => {
        this.ipValue = JSON.parse(response.data.status)
      })
    },
    onChangeIP (checked) {
      // 策略类别(1.密码2.登录3.IP)
      savePolicy({ pType: '3', status: checked }).then(response => {
        // this.$message.success(
        //   '修改成功',
        //   3
        // )
      })
    },
    getIPList () {
        this.getWhiteIPList()
        this.getBlackIPList()
    },
    /** 查询安全策略信息-IP白名单列表 */
    getWhiteIPList () {
      this.whiteLoading = true
      this.queryParam.ipType = '1'
      listIp(this.queryParam).then(response => {
        this.whiteList = response.rows
        this.whiteTotal = response.total
        this.whiteLoading = false
      })
    },
    /** 查询安全策略信息-IP黑名单列表 */
    getBlackIPList () {
      this.blackLoading = true
      this.queryParam.ipType = '2'
      listIp(this.queryParam).then(response => {
        this.blackList = response.rows
        this.blackTotal = response.total
        this.blackLoading = false
      })
    },
     onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.ips = this.selectedRows.map(item => item.ipAddr)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
     onSelectChange2 (selectedRowKeys, selectedRows) {
      this.selectedRowKeys2 = selectedRowKeys
      this.selectedRows2 = selectedRows
      this.ids2 = this.selectedRows2.map(item => item.id)
      this.ips2 = this.selectedRows2.map(item => item.ipAddr)

      this.single2 = selectedRowKeys.length !== 1
      this.multiple2 = !selectedRowKeys.length
    },
     /** 删除按钮操作 */
    handleDeleteIP (row) {
      var that = this
      const ids = row.id || this.ids
      const ips = row.ipAddr || this.ips
      this.$confirm({
        title: '确认删除所选中数据?',
        content: '当前选中IP地址为' + ips + '的数据',
        onOk () {
          return delIp(ids)
            .then(() => {
              that.onSelectChange([], [])
              that.getWhiteIPList()
              that.$message.success(
                '删除成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    handleDeleteIP2 (row) {
      var that = this
      const ids = row.id || this.ids2
      const ips = row.ipAddr || this.ips2
      this.$confirm({
        title: '确认删除所选中数据?',
        content: '当前选中IP地址为' + ips + '的数据',
        onOk () {
          return delIp(ids)
            .then(() => {
              that.onSelectChange([], [])
              that.getBlackIPList()
              that.$message.success(
                '删除成功',
                3
              )
          })
        },
        onCancel () {}
      })
    }
  }
}
</script>

<style lang="less" scoped>
    .ant-switch-checked {
        background:#13CE66
    }
    .content{
        margin-top: 40px;
    }
    .content-title{
        width: 160px;
        text-align: right;
    }
    .content-item{
        width: 100%;
        margin: 20px;
        display: flex
    }
    .ant-btn-primary:focus, .ant-btn-primary:hover,.ant-btn-primary{
        background:#46A6FF;
        border-color:#46A6FF
    }
    .content-table{
        margin-left:180px ;
    }
</style>
