<template>
  <div class="app-container">
    <el-form  :inline="true"  >
      <el-form-item>
        <el-button
          type="primary"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="adminList" border style="margin-top:20px;">
      <el-table-column label="编号" prop="id"  />
      <el-table-column label="密码长度不少于" prop="pwdLength" >
        <template slot-scope="scope">
            <span v-if="scope.row.pwdLength">{{scope.row.pwdLength}} &nbsp;位</span>
    　　</template>
      </el-table-column>
      <el-table-column label="账号锁定次数" prop="lockNumber" />
      <el-table-column label="锁定时间" prop="loseDate" >
        <template slot-scope="scope">
          <span>{{scope.row.loseDate == 1 ? "1分钟" : scope.row.loseDate == 5 ? "5分钟"
            : scope.row.loseDate == 15 ? "15分钟" : scope.row.loseDate == 30 ? "30分钟" : scope.row.loseDate == 120 ? "2小时"
            : scope.row.loseDate == 1440 ? "1天" : ""}}</span>
        </template>
      </el-table-column>
      <el-table-column label="默认账户" align="center" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isEnable"
             @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <!-- <el-table-column label="大小写敏感" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>y
      </el-table-column> -->
      <el-table-column label="创建人" align="center" prop="createBy"  show-overflow-tooltip/>
      <el-table-column label="创建时间" align="center" prop="createTime"  show-overflow-tooltip/>
      <!-- <el-table-column label="修改人" align="center" prop="updateBy"  show-overflow-tooltip/>
      <el-table-column label="修改时间" align="center" prop="updateTime"  show-overflow-tooltip/> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            class="lans"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            class="hongs"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @current-change="handleCurrentChange"
      :page-sizes="[5, 10, 15, 20]"
      @size-change="handleSizeChange"
      :current-page="pageNo"
      :page-size="pageSize"
      layout="sizes,prev, pager, next"
      :total="totalCount"
    >
    </el-pagination>

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body :before-close="cancel" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="密码长度不少于" prop="pwdLength">
              <el-input-number v-model="form.pwdLength" controls-position="right" :min="0" :max="30" placeholder="请输入密码长度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号锁定次数" prop="lockNumber" >
              <el-input-number v-model="form.lockNumber" controls-position="right" :min="0" :max="10" placeholder="请输入账号锁定次数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="锁定时间" prop="loseDate" >
              <el-select
                v-model="form.loseDate"
                placeholder="请选择锁定时间"
                size="small"
              >
                <el-option
                  v-for="item in loseDateList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="默认账户" prop="isEnable">
              <el-switch
                v-model="form.isEnable"
                >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!loading">确 定</el-button>
        <el-button :loading="loading" type="primary"  v-else>提交中...</el-button>
        <el-button @click="cancel" plain>取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAccountLock, updateAccountLock, delAccountLock, addAccountLock, } from "@/api/system/security";

export default {
  name: "AccountLock",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //存储开始页
      pageNo: 1,
      //存储每页几条
      pageSize:10,
      totalCount:undefined,
      // 表格数据
      adminList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 日期范围
      dateRange: [],

      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        name:undefined
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },

      // 表单校验
      rules: {
        pwdLength: [
          { required: true, message: "密码长度不能为空", trigger: ["blur",'change'] }
        ],
        lockNumber: [
          { required: true, message: "锁定次数不能为空", trigger: ["blur",'change']}
        ],
        loseDate:[
          { required: true, message: "锁定时间不能为空", trigger: ["blur",'change']}
        ]
      },
      loseDateList:[//锁定时间
        {value:1,label:"1分钟"},
        {value:5,label:"5分钟"},
        {value:15,label:"15分钟"},
        {value:30,label:"30分钟"},
        {value:120,label:"2小时"},
        {value:1440,label:"1天"},
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询安全机制列表 */
    getList() {
      this.loading = true;
      listAccountLock(this.queryParams).then(
        response => {
          this.adminList = response.data.pageList;
          this.totalCount = response.data.totalCount
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    // 表单重置
    reset() {

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加安全机制";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row))
      this.title = "修改角色";
      this.open = true
    },
    handleStatusChange(row){
      let text = row.isEnable === true ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.id + '"机制吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateAccountLock(row)
        }).then(() => {
          this.getList()
          this.msgSuccess(text + "成功");
        }).catch(function() {
          row.isEnable = row.isEnable === "0" ? "1" : "0";
        });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true
          if (this.form.id != undefined) {
            updateAccountLock(this.form).then(response => {
              this.loading = false
              this.$message({
                type: "success",
                message: "修改成功!",
              });
              this.open = false;
              this.getList();
            }).catch(()=>{
              this.loading = false
            });
          } else {
            if(!this.form.isEnable){
              this.form.isEnable = false
            }
            addAccountLock(this.form).then(response => {
              this.loading = false
              this.$message({
                type: "success",
                message: "新增成功!",
              });
              this.open = false;
              this.getList();
            }).catch(()=>{
              this.loading = false
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$confirm('是否确认删除角色编号为"' + id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAccountLock(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    //分页查询
    handleCurrentChange(val) {
      this.pageNo = val;
      this.getList()
    },
    //改变每条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList()
    },
  }
};
</script>
