<template>
  <div>
    <div class="selfClass" :style="{height}">
      <el-card style="width:990px;margin:40px auto auto auto">
        <div>
          <el-row>
            <el-col :span="24">
              <div class="self-avatar">
                <h2>我的首页</h2>
              </div>
            </el-col>
            <el-col :span="24" style="margin-bottom: 10px">
              <el-radio-group v-model="currentHome" size="small" @change="_radioChange">
                <el-radio-button
                  v-for="(item,index) in radioList"
                  v-show="item.name !== '平台首页'"
                  :key="index"
                  :label="index"
                >
                  {{ item.name }}
                </el-radio-button>
              </el-radio-group>
            </el-col>
            <el-col :span="24">
              <el-table
                stripe
                size="small"
                highlight-current-row
                :data="getPage"
                style="width: 100%"
                :height="300"
              >
                <el-table-column
                  type="index"
                  width="50"
                />
                <el-table-column
                  v-for="item in pageColumns"
                  :key="item.id"
                  show-overflow-tooltip
                  :prop="item.key"
                  :label="item.title"
                  :min-width="item.width"
                >
                  <template slot-scope="scope">
                    <div v-if="item.status">
                      {{ scope.row.status.display }}
                    </div>
                    <div v-else-if="item.key==='sort'" @click.stop="_handleRow(scope)">
                      <el-input-number
                        v-model.number="scope.row.sort"
                        placeholder="请输入排序"
                        :min="0"
                        size="small"
                        controls-position="right"
                        step-strictly
                        @blur="_save"
                      />
                    </div>
                    <div v-else>
                      {{ scope.row[item.key] }}
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="120">
                  <template slot-scope="scope">
                    <el-button
                      v-if="scope.row.status.display === '正常'"
                      size="mini"
                      type="danger"
                      @click="_operate(scope.row.status.display, scope.row.id)"
                    >
                      禁用
                    </el-button>
                    <el-button
                      v-if="scope.row.status.display === '禁用'"
                      size="mini"
                      @click="_operate(scope.row.status.display, scope.row.id)"
                    >
                      启用
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
/**
 * 我的首页
 */
import { sysResource, sysModule, sysSystem } from '../../../api'

export default {
  data() {
    return {
      getPage: [],
      currentHome: 0,
      radioList: [],
      radioData: 0,
      pageColumns: [
        { title: '模块名称', key: 'name', width: 150 },
        { title: '状态', key: 'status', width: 120, status: true },
        { title: '排序', key: 'sort', width: 150 }
      ],
      currentRow: {}
    }
  },
  computed: {
    height: function() {
      return this.$tableHeight('', 120) + 'px'
    }
  },
  mounted() {
    this.currentHome = 1
    this._getNavMenu()
  },
  methods: {
    _handleRow(data) {
      this.currentRow = data.row
    },
    _save: async function() {
      if (this.currentRow.sort) {
        await sysModule.editSort({
          id: this.currentRow.id,
          sort: this.currentRow.sort
        })
      }
    },
    _getNavMenu: async function() {
      // 根据平台信息与导航栏tab 取其交集，获取有权限的首页（后期可以换成一个接口）
      const result = await sysResource.getNavMenu()
      this._getRadio(result)
    },
    _getRadio: async function(menuList) {
      const result = await sysSystem.listIdName()
      const tempData = []
      for (let i = 0; i < result.length; i++) {
        menuList.forEach(value => {
          if (value.name === result[i].name) {
            tempData.push(result[i])
          }
        })
      }
      this.radioList = tempData
      this._radioChange(0)
    },
    _radioChange: async function(data) {
      this.radioData = data
      const result = await sysModule.listBySystemId(this.radioList[data].id)
      if (result.length === 0) {
        // 没有该模块首页权限
        // this.$message.warning('抱歉，您暂无本模块首页权限！');
        this.getPage = []
        this.loading = false
      } else {
        this.getPage = result
        this.loading = false
      }
    },
    _operate(status, id) {
      if (status === '禁用') {
        this._batchModuleNormal(id)
      } else {
        this._batchModuleForbidden(id)
      }
    },
    _resultChange(result) {
      if (result) {
        this._radioChange(this.radioData)
      }
    },
    _batchModuleNormal: async function(id) {
      const result = await sysModule.addModule(id)
      this._resultChange(result)
    },
    _batchModuleForbidden: async function(id) {
      const result = await sysModule.deleteModule(id)
      this._resultChange(result)
    }
  }
}
</script>

<style scoped>
.selfClass {
  background: #eee;
  padding: 20px;
}

.self-avatar {
  margin-bottom: 20px;
  border-bottom: 1px solid #eee;
  padding-bottom: 10px;
  text-align: center;
}
</style>
