<template>
  <div class="app-container">
    <el-card>
      <!--<el-input v-model="RoleFunctioning" size="small" clearable placeholder="请输入角色功能" style="width:200px"/>-->
      <!--<el-button type="primary" icon="el-icon-search" size="small" style="margin-left: 15px" plain @click="searchData()">查询</el-button>-->
      <el-button type="distribution" icon="el-icon-circle-plus-outline" size="small" @click="handleAdd()">增加</el-button>
      <el-table v-loading="loading" :data="tableData" height="640" highlight-current-row border style="width: 550px;">
        <el-table-column align="center" label="角色编号" width="100">
          <template slot-scope="scope">
            {{ scope.row.角色编号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="角色功能" width="300">
          <template slot-scope="scope">
            {{ scope.row.角色功能 }}
          </template>
        </el-table-column>
        <!--<el-table-column align="center" label="操作日期">-->
        <!--<template slot-scope="scope">-->
        <!--{{ scope.row.操作日期 }}-->
        <!--</template>-->
        <!--</el-table-column>-->
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" plain icon="el-icon-edit" @click="handleEdit(scope.row)"/>
            <el-button size="mini" type="text" plain icon="el-icon-delete" @click="handleDelete(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
      <!--      <div class="block" style="margin-top: 10px;">-->
      <!--        <el-pagination-->
      <!--          :current-page="pageCurrent"-->
      <!--          :page-sizes="[10, 20, 30, 40, 50]"-->
      <!--          :page-size="pageSize"-->
      <!--          :total="total"-->
      <!--          layout="total, sizes, prev, pager, next, jumper"-->
      <!--          @size-change="handleSizeChange"-->
      <!--          @current-change="handleCurrentChange"/>-->
      <!--      </div>-->
    </el-card>

    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible1" title="新增角色" center style="min-height: 200px" width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="110px">
        <el-form-item label="角色名称" prop="角色功能" style="display: inline-block">
          <el-input
            v-model="form.角色功能"
            clearable
            size="small"
            placeholder="角色功能"
            style="width: 200px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible1 = false">取消</el-button>
        <el-button :disabled="handleAdd_disable" type="primary" size="small" @click="submitAdd1('form')">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible2" title="编辑角色" center style="min-height: 200px" width="400px">
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left" label-width="110px">
        <el-form-item label="角色名称" prop="角色功能" style="display: inline-block">
          <el-input
            v-model="form2.角色功能"
            clearable
            size="small"
            placeholder="角色功能"
            style="width: 200px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible2 = false">取消</el-button>
        <el-button :disabled="handleAdd_disable" type="primary" size="small" @click="submitAdd2('form2')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { InitRolefunction, deleteData, searchTable, addData, addData2 } from '@/api/BasicData/SystemRrolemaintenance'
export default {
  data() {
    return {
      handleAdd_disable: false,
      RoleFunctioning: '',
      loading: false,
      tableData: [],
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      name: '',
      num: '', // 角色编号
      form: {
        角色功能: ''
      },
      form2: {
        角色功能: ''
      },
      rules: {
        角色功能: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
      },
      rules2: {
        角色功能: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      InitRolefunction().then(response => { // 初始化
        this.tableData = response.data
        this.total = response.data.total
      })
    },
    searchData() {
      this.loading = true
      searchTable(this.RoleFunctioning).then(response => {
        this.tableData = response.data
        this.loading = false
        console.log(response.data)
      })
    },
    // handleSizeChange(val) {
    //   this.pageCurrent = 1
    //   this.pageSize = val
    //   this.fetchData()
    // },
    // handleCurrentChange(val) {
    //   this.pageCurrent = val
    //   this.fetchData()
    // },
    handleEdit(row) {
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      this.handleAdd_disable = false
      this.dialogFormVisible2 = true
      this.form2.角色功能 = row.角色功能
      this.num = row.角色编号
    },
    submitAdd2(formName) { // 编辑角色功能
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.handleAdd_disable = true
          addData2(this.form2.角色功能, this.num).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('编辑成功')
              this.fetchData()
              this.dialogFormVisible2 = false
            } else { this.$message.error('编辑失败') }
          })
        }
      })
    },
    submitAdd1(formName) { // 增加角色功能
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.handleAdd_disable = true
          addData(this.form.角色功能).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('增加成功')
              this.form.角色功能 = ''
              this.fetchData()
              this.dialogFormVisible1 = false
            } else { this.$message.error('增加失败') }
          })
        }
      })
    },
    handleAdd() {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.handleAdd_disable = false
      this.dialogFormVisible1 = true
    },
    handleDelete(row) { // 删除
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteData(row.角色编号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
          } else {
            this.$message.error('删除失败')
          }
          this.fetchData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style scoped>
  .el-card{
    margin: 5px;
  }
</style>

