<template>
  <div class="container">
    <!-- 查询配置 -->
    <div>
      <div ref="lxHeader">
        <LxHeader icon="md-apps" text="角色权限" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
          <div slot="content"></div>
          <slot>
            <div>
              <Input style="width: 200px; margin-right: 10px" v-model.number="searchFields.Name" placeholder="关键字搜索" />
              <Button type="primary" @click="load" style="margin-left: 10px">查询</Button>
              <Button type="primary" @click="edit()" style="margin-left: 10px">新增角色</Button>
            </div>
          </slot>
        </LxHeader>
      </div>

      <div class="table-tree">
        <el-table :data="tableData" class="v-table" border style="width: 100%">
          <el-table-column type="index" align="center">
          </el-table-column>
          <el-table-column prop="name" label="角色名称" width="180">
          </el-table-column>
          <el-table-column prop="address" label="说明">
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="300">
            <template slot-scope="scope">
              <Button ghost size="small" @click="edit(scope.row)" type="primary">编辑</Button>
              <Button @click="del(scope.row)" type="error" ghost size="small" style="margin-left: 10px;">删除</Button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-dialog title="角色编辑" :visible.sync="dialogFormVisible" width="30%">
        <el-form :model="form" ref="form" :rules="rules" label-position="right" :label-width="formLabelWidth">

          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>

          <el-form-item label="排序">
            <el-input v-model="form.sortnumber" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="resetForm('form')">取 消</el-button>
          <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import LxHeader from "@/components/LxHeader/index.vue";
  import {
    save,
    list,
    del
  } from "../../api/system/role.js";
  export default {
    components: {
      LxHeader
    },
    created() {
      this.load();
    },
    mounted() {},
    data() {
      return {
        dialogFormVisible: false,
        formLabelWidth: "120px",
        form: {
          name: "",
          id: "",
          sortnumber: "0"
        },
        rules: {
          name: [{
            required: true,
            message: '请输入角色名称',
            trigger: 'blur'
          }],
        },
        tableHeight: 0,
        //查询条件字段
        searchFields: {
          KeyWord: ""
        },
        tableData: []
      }
    },
    methods: {
      load() {
        var that = this;
        list().then(response => {
          that.tableData = response;
        }).catch(error => {
          //reject(error)
        })
      },
      edit(obj) {
        if (obj) {
          this.form.id = obj.id;
          this.form.name = obj.name;
        }
        this.dialogFormVisible = true;
      },
      del(obj) {
        var that = this;
        if (obj) {
          this.$Modal.confirm({
            title: '确认要删除数据？',
            onOk: () => {
              del({
                id: obj.id
              }).then(response => {
                this.$Message.success('操作成功');
                that.load();
              }).catch(error => {
                console.log(error)
                reject(error)
              })
            },
            onCancel: () => {
              //this.$Message.info('Clicked cancel');
            }
          });
        }
      },
      submitForm(formName) {
        var that = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            save(that.form).then(response => {
              console.log(response)
              this.$Message.success('操作成功');
              that.load();
              that.dialogFormVisible = false;
            }).catch(error => {
              reject(error)
            })
          } else {
            this.$Message.error('数据校验失败');
            return false;
          }
        });
      },
      resetForm(formName) {
        var that = this;
        this.$refs[formName].resetFields();
        that.dialogFormVisible = false;
      }
    }
  };
</script>

<style>
</style>
