<template>
  <xy-page title="角色列表">
    <xy-search
      class="mgt20"
      @enter="search(1)"
    >
      <el-form
        inline
        :model="searchFilter"
        label-width="70px"
      >
        <el-form-item
          label="集团"
          prop=""
        >
          <el-select
            v-model="searchFilter.companyId"
            placeholder="请选择集团"
          >
            <el-option
              label="全部"
              :value="-2"
            />
            <el-option
              label="运营支撑系统"
              :value="-1"
            />
            <el-option
              v-for="item in companyList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="right">
        <el-button
          type="primary"
          @click="search(1)"
        >查询</el-button>
        <el-button
          plain
          @click="resetForm"
        >重置条件</el-button>
      </div>
    </xy-search>
    <div class="system-index-btns mgb20">
      <el-button
        v-auth="'AUTH_ROLE-SINGLE_SAVE'"
        type="primary"
        icon="icon-scrappy-add"
        @click="editRow()"
      >
        新建角色
      </el-button>
    </div>
    <!-- 列表区域-STR -->
    <xy-table
      v-show="!listData.firtIn"
      :loading="listData.showLoading"
      :table-data="listData.tableData"
      @change="search"
    >
      <el-table-column
        min-width="40px"
        prop="id"
        label="流水号"
      />
      <el-table-column
        min-width="70px"
        prop="name"
        label="角色名字"
      />
      <el-table-column
        min-width="70px"
        prop="code"
        label="角色编码"
      />
      <el-table-column
        min-width="70px"
        prop="appName"
        label="系统名字"
      />
      <el-table-column
        min-width="90px"
        label="创建时间"
      >
        <template slot-scope="scope">
          {{ scope.row.createTime | date }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="70px"
        prop="createName"
        label="创建人"
      />
      <el-table-column
        min-width="80px"
        label="更多"
      >
        <template
          v-if="scope.row.isEdit !== 0"
          slot-scope="scope"
        >
          <el-button
            v-auth="'AUTH_ROLE-SINGLE_SAVE'"
            type="text"
            @click="editRow(scope.row)"
          >编辑</el-button>
          <el-button
            v-auth="'AUTH_ROLE-SAVE'"
            type="text"
            @click="$toPage('/auth-role/edit',{id:scope.row.id})"
          >角色权限</el-button>
          <!--<el-button  @click="deleteRow(scope.row.id)" type="text" v-auth="'AUTH_ROLE-SAVE'">删除</el-button>-->
        </template>
      </el-table-column>
    </xy-table>
    <!-- 列表区域-END -->

    <!-- 保存记录-STR -->
    <el-dialog
      :title="editDialog.data.id?'修改':'添加'+editDialog.title"
      :visible.sync="editDialog.show"
      class="modal-resetpwd"
    >
      <div class="form-row form-group ">
        <label class="text-right">
          <span class="require">*</span>角色名字:</label>
        <el-input
          v-model="editDialog.data.name"
          type="test"
          placeholder="角色名字"
        />
        <span class="errmsg">{{ editDialog.errmsg.name }}</span>
      </div>
      <div class="form-row form-group ">
        <label class="text-right">
          <span class="require">*</span>角色编码:</label>
        <el-input
          v-model="editDialog.data.code"
          type="test"
          placeholder="角色编码"
          :disabled="!!editDialog.data.id"
        />
        <span class="errmsg">{{ editDialog.errmsg.code }}</span>
      </div>

      <div class="form-row form-group ">
        <label class="text-right">
          <span class="require">*</span>关联系统:</label>
        <el-select
          v-model="editDialog.data.appId"
          placeholder="请选择系统"
          :disabled="!!editDialog.data.id"
        >
          <el-option
            v-for="item in appList"
            :key="item.id"
            :label="item.cnName"
            :value="item.id"
          />
        </el-select>
        <span class="errmsg">{{ editDialog.errmsg.descript }}</span>
      </div>

      <div
        slot="footer"
        class="dialog-footer"
      >
        <xy-save
          v-model="editDialog.save"
          type="primary"
          @click="saveDialog"
        >确 定</xy-save>
        <el-button @click="editDialog.show = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 保存记录-END -->
  </xy-page>
</template>

<script>
export default {
  name: 'AuthRoleList',
  data() {
    return {
      companyList: [],
      searchFilter: {
        companyId: -1
      },
      appList: [],
      loading: false,
      listData: {
        firtIn: true,
        tableData: {
          total: 0,
          page: 1,
          size: 15,
          list: []
        },
        showLoading: false
      },
      editDialog: {
        title: '角色',
        url: '/admin/auth/instance/authRole/singleSave.do',
        data: {
          id: '',
          appId: '',
          code: '',
          name: ''
        },
        errmsg: {
          id: '',
          appId: '',
          code: '',
          name: ''
        },
        save: false,
        show: false
      }
    }
  },
  mounted() {
    this.search(1)
    this.$postStream('/admin/auth/instance/authCompany/listAll.do', {
      data: {},
      success: res => {
        if (res.success) {
          this.companyList = res.object || []
        } else {
          this.$message.error(res.message)
        }
      },
      error: () => { }
    })
  },
  methods: {
    search(page) {
      page = page || this.listData.tableData.page
      this.listData.firtIn = false
      const param = {
        page: page || 1,
        size: this.listData.tableData.size
      }
      for (const key in this.searchFilter) {
        param[key] = this.searchFilter[key]
      }
      this.listData.tableData.list = []
      this.listData.showLoading = true
      this.$postStream('/admin/auth/user/authRole/listForPage.do', {
        data: param,
        success: res => {
          this.listData.showLoading = false
          if (res.success) {
            this.listData.tableData.list = res.object.list || []
            this.listData.tableData.total = res.object.count
            this.listData.tableData.page = page || 1
          } else {
            this.$message.error(res.message)
          }
        },
        error: () => {
          this.listData.showLoading = false
        }
      })
    },
    resetForm() {
      this.searchFilter = this.$options.data().searchFilter
      this.search(1)
    },
    editRow(row) {
      row = row || {}
      this.editDialog.data = JSON.parse(JSON.stringify(row))
      for (const key in this.editDialog.errmsg) {
        this.editDialog.errmsg[key] = ''
      }
      this.remoteMethod()
      this.editDialog.show = true
    },
    saveDialog() {
      this.editDialog.save = true
      this.$postStream(this.editDialog.url, {
        data: this.editDialog.data,
        success: res => {
          if (res.success) {
            this.$message.warning(res.message)
            this.editDialog.show = false
            this.search()
          } else {
            this.$message.error(res.message)
          }
          this.editDialog.save = false
        },
        error: () => {
          this.editDialog.save = false
        }
      })
    },
    remoteMethod(query) {
      this.loading = true
      this.$postStream('/admin/auth/instance/authApp/list.do', {
        data: {
          quickSearch: query
        },
        success: (res) => {
          this.appList = res.object.list
          this.loading = false
        }
      })
    },
    deleteRow(ids) {
      this.$xyConfirm('是否删除，删除后将无法恢复？', () => {
        this.$post('/admin/auth/instance/authAppUserInstance/delete.do', {
          data: {
            ids: ids
          },
          success: (res) => {
            if (!res.success) {
              return this.$message.error(res.message)
            }
            this.$message.success(res.message)
            this.search()
          }
        })
      })
    }
  }
}
</script>

<style lang="scss">
.errmsg,
.require {
  color: $lk-danger;
}
.system-index-btns {
  text-align: right;
  & i {
    font-size: 12px;
    margin-right: 8px;
    vertical-align: top;
  }
  & span {
    display: inline-block;
    vertical-align: top;
  }
}
.device-info {
  .device-info-body {
    margin: 15px 15px 70px;
  }
  .el-dialog__header {
    border-bottom: 1px solid $lk-border-base;
  }
  .info-line {
    margin-bottom: 10px;
    span {
      display: inline-block;
      padding: 5px 0;
      color: $lk-text-primary;
      font-size: 14px;
      line-height: 24px;
    }
    span.name {
      min-width: 110px;
      line-height: 24px;
    }
  }
}
</style>
