<template>
  <div>
    <el-button size="small" type="primary" style="width:120px" @click="querySite_Parameter">设 置</el-button>
    <el-dialog title="设 置" :visible.sync="dialogFormVisible" width="860px">
      <el-form :model="form" label-position="left" style="padding:0 30px">
        <el-form-item label="默认密码">
          <div class="flex-align-center">
            <el-input size="small" style="width:300px" :disabled="disabled" type="password" v-model="password_default"  placeholder="请输入默认密码"></el-input>
            <el-button type="text" size="small" style="margin-left:10px" @click="setPassword">重新设置</el-button>
          </div>
        </el-form-item>
        <el-form-item label="密码位数">
          <div class="flex-align-center">
            <el-input size="small" style="width:300px" v-model="form.password_length" placeholder="请输入密码位数"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="包含字符类型">
          <div class="flex-align-center">
            <div>
              <el-checkbox-group :disabled="radio2 === 0" v-model="checkList" @change="checkboxChange">
                <el-checkbox label="password_needcapital">大写英文字母</el-checkbox>
                <el-checkbox label="password_needlowercase">小写英文字母</el-checkbox>
                <el-checkbox label="password_neednum">数字0-9</el-checkbox>
                <el-checkbox label="password_needspecialchar">特殊符号~,!,@,#,$,%,^;</el-checkbox>
              </el-checkbox-group>
            </div>
          </div>
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="warning" @click="updateSite_Parameter" class="normal-btn-width btn-warning">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import md5 from 'js-md5'

export default {
  data () {
    return {
      dialogFormVisible:false,
      disabled:true,
      radio:1,
      radio2:1,
      form:{
        "loginmode_phonenumber": 1,
        "loginmode_wechat": 0,
        "loginmode_account":1
      },
      checkList:[],
      password_default:''
    }
  },
  mounted () {
    // this.querySite_Parameter()
  },
  methods:{
    async querySite_Parameter () {
      this.dialogFormVisible = true
      this.disabled = true
      
      const res = await this.$api.requested({
        "classname": "webmanage.site.site",
        "method": "querySite_Parameter",
        "content": {
        }
      })
      let obj = {
        password_needcapital:res.data.password_needcapital,
        password_needlowercase:res.data.password_needlowercase,
        password_neednum:res.data.password_neednum,
        password_needspecialchar:res.data.password_needspecialchar,
        password_length:res.data.password_length
      }
      this.password_default = res.data.password_default
      this.form = Object.assign({},this.form,obj)

      Object.keys(res.data).map((key,item)=>{
        res.data[key] === 1?this.checkList.push(key):''
      })
    },
    checkboxChange () {
      Object.keys(this.form).map((key,item)=>{
        if (key !== 'password_length')
         return this.form[key] = this.checkList.some(item=>item === key)?1:0
      })
    },
    async updateSite_Parameter () {
      if (this.form.password_length <= 0) return this.$message({message:'密码长度必须大于0',type:'error'})
      const res = await this.$api.requested({
        "classname": "webmanage.site.site",
        "method": "updateSite_Parameter",
        "content": this.form
      })
      if (res.code === 1) {
        this.disabled?this.$message({
          message:'修改成功',
          type:'success'
        }):this.setDefaultPassword()
        this.disabled?this.dialogFormVisible = false:''
      } else {
        this.$message({
          message:res.data,
          type:'error'
        })
      }
    },
    async setDefaultPassword () {
      if (this.password_default.length < this.form.password_length) return this.$message({message:'密码长度有误请重新设置',type:'error'})
      const res = await this.$api.requested({
        "classname": "webmanage.site.site",
        "method": "updateSite_defaultpassword",
        "content": {
          "password_default":md5(this.password_default)
        }
      })
      res.code === 1?this.dialogFormVisible = false:this.$message({
          message:res.data,
          type:'error'
        })
    },
    setPassword () {
      this.disabled = false
      this.password_default = ''
    }
  }
}

</script>
<style>
</style>
<style scoped>
.dialog-footer{
  text-align: center;
}
</style>