<template>
  <div class="form-wrap">
    <div class="searchBox">
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        label-width="100px">
        <el-form-item
          label="名称"
          prop="projectName">
          <el-input v-model="form.projectName"></el-input>
        </el-form-item>
      </el-form>
      <div class="btn-wrap">
        <el-button
          type="primary"
          @click="getProjectList">搜索</el-button>
        <el-button
          type="primary"
          v-if="canAddUsers"
          @click="showDialog('add')">新增项目</el-button>
      </div>
    </div>
    <div class="tableBox">
      <!-- <div class="block-title">
        项目列表
      </div> -->

      <el-table
        v-loading="loading"
        row-class-name="is-center"
        :data="tableData">
        <el-table-column
          prop="projectIndex"
          label="项目序号"
          width="100">
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="160">
        </el-table-column>
        <el-table-column
          prop="id"
          label="项目ID"
          width="200">
        </el-table-column>
        <el-table-column
          prop="projectName"
          label="项目名称">
        </el-table-column>
        <el-table-column
          prop="projectNickName"
          label="项目中文名称"
          width="160">
        </el-table-column>
        <el-table-column
          prop="projectDesc"
          label="项目描述"
          show-overflow-tooltip
          width="200">
        </el-table-column>
        <el-table-column
          prop="offlineUrl"
          label="线下地址"
          width="200">
        </el-table-column>
        <el-table-column
          prop="lastHeartbeatTime"
          label="最近一次在线时间"
          width="180">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="validEndTime"
          label="项目到期时间" />
        <el-table-column
          fixed="right"
          label="操作"
          width="120">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="showDialog('edit',scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 账号弹框 -->
    <ElDialog
      title="添加项目"
      width="660px"
      v-model="addVisible">
      <AddDialog @updateList="getProjectList"></AddDialog>
    </ElDialog>

    <ElDialog
      title="编辑项目"
      width="660px"
      v-model="editVisible">
      <EditDialog
        :project-item="currentItem"
        @updateList="getProjectList"></EditDialog>
    </ElDialog>

    <ElDialog
      title="项目详细"
      width="800px"
      v-model="detailVisible">
      <DetailDialog
        :project-item="currentItem"
        @updateList="getProjectList"></DetailDialog>
    </ElDialog>

    <el-pagination
      background
      :page-size="10"
      @current-change="handlePage"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getProjectList } from '@/api/platform'
import ElDialog from '@/components/Dialog'
import AddDialog from './project/AddDialog'
import EditDialog from './project/EditDialog'
import DetailDialog from './project/DetailDialog'

export default {
  components: {
    ElDialog,
    AddDialog,
    EditDialog,
    DetailDialog
  },
  computed: {
    canAddUsers () {
      return true
      // return this.$store.state.user.permissionList.includes('system_settings:add_users')
    }
  },

  created () {
    this.getProjectList()
  },
  data () {
    return {
      loading: false,
      addVisible: false,
      editVisible: false,
      detailVisible: false,
      tableData: [],
      total: 0,
      form: {
        projectName: '', // 账号名
        pageNum: 1,
        pageSize: 10
      },
      // 当前账号对象
      currentItem: {}
    }
  },
  methods: {
    showDialog (type, item) {
      this[`${type}Visible`] = true
      this.currentItem = item
    },
    getProjectList (goFirst) {
      this.loading = true
      if (goFirst) {
        this.form.pageNum = 1
      }
      getProjectList(this.form).then(res => {
        this.loading = false
        this.tableData = res.data.records.map(item => {
          item.permissionList = (item.roleList || []).reduce((acc, role) => {
            return acc.concat(role.resourceList || [])
          }, [])
          return item
        }) || []
        this.total = res.data.total
      })
    },
    handlePage (pageNum) {
      this.form.pageNum = pageNum
      this.getProjectList()
    }
  }
}
</script>

<style scoped>

</style>
