<template>
  <el-tabs class="my-tabs" v-model="activeName" type="card" @tab-click="handleClick">
    <el-tab-pane label="资源列表" name="roleList">

      <div class="container">
        <div class="tab-header">
          <el-form ref="form" :inline="true" :model="page" label-width="80px">
            <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item label="资源名称">
                  <el-input v-model="page.params.sourceName" clearable></el-input>
                </el-form-item>
                <el-button @click="getList(1)">查询</el-button>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <!-- table -->
        <el-table :data="roleTable" border style="width: 100%;">
          <el-table-column label="序号" width="80px" type="index" align="center">
          </el-table-column>
          <el-table-column label="操作" width="220px" align="center">
            <template slot-scope="scope">
              <el-button @click="editRow(scope.row)" type="primary" size="mini"> 修改</el-button>
              <el-button @click="delRow(scope.row)" type="danger" size="mini"> 删除</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="sourceName" label="资源名称" align="center"></el-table-column>
          <el-table-column prop="sourceId" label="资源编码" align="center"></el-table-column>
          <el-table-column prop="remarks" label="说明" align="center"></el-table-column>
        </el-table>
        <pagination :total="page.total" :page.sync="page.current" :limit.sync="page.size" @pagination="pagination" />

        <!-- 编辑资源信息 -->
        <el-dialog :title="dialogTitle + '资源信息'" :visible.sync="editDialog" width="40%">
          <table class="e-table" cellspacing="0">
            <tr>
              <td>资源名称</td>
              <td>
                <el-input v-model="roleForm.sourceName" style="width:300px"></el-input>
              </td>
            </tr>
            <tr>
              <td>资源编码</td>
              <td>
                <el-input v-model="roleForm.sourceId" style="width:300px"></el-input>
              </td>
            </tr>
            <tr>
              <td>资源说明</td>
              <td>
                <el-input v-model="roleForm.remarks" style="width:300px"></el-input>
              </td>
            </tr>
          </table>
          <div style="margin-top: 20px; text-align: center;">
            <el-button type="primary" @click="save()">保 存</el-button>
            <el-button @click="editDialog = false">关 闭</el-button>
          </div>
        </el-dialog>
      </div>
    </el-tab-pane>
    <!-- 新增资源信息 -->
    <el-tab-pane label="新增资源" name="addrole">
      <el-card class="box-card">
        <table class="e-table" cellspacing="0">
          <tr>
            <td>资源名称</td>
            <td>
              <el-input v-model="roleForm.sourceName" style="width:300px"></el-input>
            </td>
          </tr>
          <tr>
            <td>资源编码</td>
            <td>
              <el-input v-model="roleForm.sourceId" style="width:300px"></el-input>
            </td>
          </tr>
          <tr>
            <td>资源说明</td>
            <td>
              <el-input v-model="roleForm.remarks" style="width:300px"></el-input>
            </td>
          </tr>
        </table>
        <div style="margin-top: 20px; text-align: center;">
          <el-button type="primary" @click="save()">保 存</el-button>
        </div>
      </el-card>
    </el-tab-pane>
  </el-tabs>
</template>

<script>
  import req from '@/api/system/resourcesManage/resourcesManage.js'

  export default {
    data() {
      return {
        dialogTitle: '',
        activeName: 'roleList',
        roleForm: {
          sid: "",
          sourceName: "",
          sourceId: '',
          remarks: '',
        },
        formBackup: {},
        page: {
          total: 0, // 默认数据总数
          current: 1, // 默认开始页面
          size: 10, // 每页的数据条数
          params: {
            sourceName: '',
          },
        },
        roleTable: [],
        editDialog: false,
      };
    },
    mounted() {
      this.formBackup = Object.assign({}, this.roleForm),
        this.getList()
    },
    methods: {
      pagination(val) { // 分页函数
        this.page.current = val.pageNum
        this.page.size = val.pageSize
        this.getList()
      },
      // 分页列表
      getList(flag) {
        if (flag == '1') {
          this.page.current = 1
        }
        req.listPage(this.page).then(res => {
          this.page.total = res.data.total
          this.roleTable = res.data.records
        })
      },
      handleClick(tab, event) {
        if (tab.name == 'addrole') {
          this.dialogTitle = '新增'
          this.roleForm = Object.assign({}, this.formBackup)
        } else {
          this.getList()
        }
      },
      editRow(row) {
        this.dialogTitle = '编辑'
        this.editDialog = true
        console.log(row)
        this.roleForm = Object.assign({}, row)
        this.checkedId1 = [this.roleForm.orgSid]
      },
      delRow(row) {
        this.$confirm('确定要删除该资源吗, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          req.deleteBySid(row.sid).then(res => {
            this.getList()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          })
        })
      },
      // 保存资源
      save() {

        if (this.roleForm.sourceName == '') {
          this.$message({
            message: '资源名称不能为空',
            type: 'warning'
          })
          return
        }

        if (this.roleForm.sourceId == '') {
          this.$message({
            message: '资源编码不能为空',
            type: 'warning'
          })
          return
        }


        req.savereSourcesManage(this.roleForm).then(res => {
          if (res.code == '200') {
            this.getList()
            this.reset()
            this.editDialog = false
            this.$message({
              message: res.msg,
              type: 'success'
            })
          }
        })

      },
      reset() {
        this.roleForm = {
          sid: "",
          sourceName: "",
          sourceId: '',
          remarks: '',
        };
        this.page = {
          total: 0, // 默认数据总数
          current: 1, // 默认开始页面
          size: 10, // 每页的数据条数
          params: {
            sourceName: '',
          },
        }
      },

    }
  };
</script>

<style scoped>
  .el-select>.el-input {
    display: block;
    width: 300px;
  }

  /deep/ .tab-header .el-select {
    width: 180px;
  }

  .my-tabs {
    margin-top: 10px;
  }
</style>
