<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>

    <!-- 列表区域-STR -->
    <xy-table
      v-show="!listData.firtIn"
      :loading="listData.showLoading"
      :table-data="listData.tableData"
      @change="search"
    >
      <el-table-column
        min-width="30px"
        prop="id"
        label="流水号"
      />
      <el-table-column
        min-width="50px"
        prop="account"
        label="登录账号"
      />
      <el-table-column
        min-width="50px"
        prop="userName"
        label="登录人"
      />
      <el-table-column
        min-width="70px"
        prop="ip"
        label="登录IP地址"
      />
      <el-table-column
        min-width="50px"
        prop="address"
        label="登录地址"
      />
      <el-table-column
        min-width="70px"
        prop="targetIp"
        label="目标IP地址"
      />
      <el-table-column
        min-width="30px"
        prop="status"
        label="状态"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.status !== 0"
            class="deep-green"
          >成功</span>
          <span
            v-if="scope.row.status === 0"
            class="red"
          >失败</span>
        </template>
      </el-table-column>
      <el-table-column
        min-width="110px"
        prop="reason"
        label="失败原因"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.status !== 0"
            class="deep-green"
          >--</span>
          <span
            v-if="scope.row.status === 0"
            class="red"
          >{{ scope.row.reason }}</span>
        </template>
      </el-table-column>
      <el-table-column
        min-width="90px"
        label="创建时间"
      >
        <template slot-scope="scope">
          {{ scope.row.createTime | date }}
        </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.shortName"
          type="test"
          placeholder="请输入集团简称"
        />
        <span class="errmsg">{{ editDialog.errmsg.shortName }}</span>
      </div>

      <div class="form-row form-group ">
        <label class="text-right">
          <span class="require">*</span>集团账号:</label>
        <el-input
          v-model="editDialog.data.account"
          type="test"
          placeholder="请输入集团账号"
        />
        <span class="errmsg">{{ editDialog.errmsg.descript }}</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="请选择系统"
        >
          <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: 'AuthAppList',
  data() {
    return {
      companyList: [],
      appList: [],
      searchFilter: {
        companyId: -1
      },
      listData: {
        firtIn: true,
        tableData: {
          total: 0,
          page: 1,
          size: 15,
          list: []
        },
        showLoading: false
      },
      editDialog: {
        title: '集团',
        url: '/admin/auth/instance/auth/save.do',
        data: {
          name: '',
          shortName: '',
          account: ''
        },
        errmsg: {
          name: '',
          shortName: '',
          account: ''
        },
        save: false,
        show: false
      }
    }
  },
  mounted() {
    this.search(1)
    this.$postStream('/admin/auth/instance/authApp/list.do', {
      data: {},
      success: res => {
        if (res.success) {
          this.appList = res.object.list || []
        } else {
          this.$message.error(res.message)
        }
      },
      error: () => { }
    })
    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/instance/authLoginLog/list.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.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
        }
      })
    }
  }
}
</script>

<style lang="scss">
.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>
