<template>
  <div class="app-container">
    <!--筛选条件-->
    <div class="search-div">
      <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
        <el-row>
          <el-form-item class="selectForm-container-item">
            <el-input v-model.trim="listQuery.projectName" placeholder="项目名称" clearable/>
          </el-form-item>
          <el-form-item class="selectForm-container-item">
            <el-select v-model="listQuery.projectStatus" filterable placeholder="项目状态" clearable value="" >
              <el-option
                v-for="item in statusList"
                :key="item.value"
                :label="item.name"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">查询</el-button>
        </el-row>
      </el-form>
    </div>
    <el-row class="table-title">
      <el-col :span="6"><div class="title-header"><i class="el-icon-menu"/>数据列表</div></el-col>
      <el-button class="edit_btn" size="small" @click="add">新增项目</el-button>
    </el-row>
    <!--查询结果Table显示-->
    <div>
      <el-table v-loading="listLoading" :data="list" class="table" border @selection-change="handleSelectionChange">
        <el-table-column :index="indexMethod" align="center" type="index"/>
        <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align" show-overflow-tooltip>
          <template slot-scope="scope">
            <span :class="column.class">{{ scope.row[column.value] }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="showOperate" label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button type="text" @click="detail(scope.row)">详情</el-button>
            <el-button type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" @click="connect(scope.row)">关联小程序用户</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页-->
    <div class="pagination-container">
      <el-pagination

        :current-page="listQuery.offset"
        :page-sizes="[10,20,30]"
        :page-size="listQuery.limit"
        :total="total"
        align="center"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>

    <edit-project v-show="dialogFormVisible" ref="editproject" @watchChild="fetchData"/>

  </div>
</template>

<script>
import { getProjectList } from '@/api/factoryManager/project'
import EditProject from './editProject'

export default {
  name: 'ListProject',
  components: { EditProject },
  data() {
    return {
      listQuery: {
        offset: 1,
        limit: 10
      }, // 筛选条件
      statusList: [
        {
          name: '未开始',
          value: '1'
        },
        {
          name: '进行中',
          value: '2'
        },
        {
          name: '已结束',
          value: '3'
        }
      ],
      columns: [
        {
          text: '项目名称',
          value: 'projectName',
          align: 'center'
        },
        {
          text: '项目简称',
          value: 'projectSimpleName',
          align: 'center'
        },
        {
          text: '项目经理',
          value: 'projectManager',
          align: 'center'
        },
        {
          text: '项目开始日期',
          value: 'projectStartTime',
          align: 'center'
        },
        {
          text: '项目结束日期',
          value: 'projectEndTime',
          align: 'center'
        },
        {
          text: '生产设备类型',
          value: 'devTpeNames',
          align: 'center'
        },
        {
          text: '项目状态',
          value: 'projectStatus',
          align: 'center',
          width: 120
        }
      ], // 显示列
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 加载动画
      fullscreenLoading: false, // 全屏加载动画
      dialogFormVisible: false // 是否显示编辑框
    }
  },
  computed: {
    'showOperate': function() { // 判断是否显示操作列，如果编辑和删除的权限都没有则不显示操作列
      return this.hasPerm('/dict/update') || this.hasPerm('/dict/delete')
    }
  },
  created() {
    this.fetchData()// 获取数据
    var lett = this
    document.onkeydown = function(e) {
      var key = window.event.keyCode
      if (key === 13) {
        lett.fetchData()
      }
    }
  },
  activated() {
    console.log('activated')
    this.fetchData()// 获取数据
  },
  methods: {
    // 新增
    add() {
      this.dialogFormVisible = true
      this.$refs.editproject.initDialog('create', this.dialogFormVisible)
    },
    // 编辑
    edit(row) {
      this.dialogFormVisible = true
      this.editShow = true
      this.$refs.editproject.initDialog('update', this.dialogFormVisible, row)
    },
    // 点击详情
    detail(row) {
      this.dialogFormVisible = true
      this.$refs.editproject.initDialog('detail', this.dialogFormVisible, row)
    },
    // 点击关联小程序用户
    connect(row) {

    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取字典数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getProjectList(this.listQuery).then(response => {
        this.list = response.data.rows
        console.log(this.list)
        this.total = parseInt(response.data.total)
        this.listLoading = false
      })
    },
    indexMethod(index) {
      return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      this.fetchData()
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .edit_btn{
    float:right;
    margin:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
  }
  $tableTitleHeight:46px;
  .table{
    margin-bottom: 20px;
  }
  .pagination-container{
    margin-bottom: 50px;
  }
  .table-title{
    background-color:rgba(243, 243, 243, 1);
    height: $tableTitleHeight;
    .title-header{
      line-height:$tableTitleHeight;
      color: #606266;
      font-size: 15px;
      i{
        margin-left: 5px;
        margin-right: 5px;
      }
    }
  }
  .edit_btns{
    .edit_btn{
      float:right;
      margin:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
</style>
