<template>
  <div class="app-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="80px" inline>
        <el-form-item label="模糊查询">
          <el-input
            v-model="filter.nameStartWith"
            placeholder="名称前缀"
          ></el-input>
        </el-form-item>
        <el-form-item label="编码">
          <el-input v-model="filter.code" placeholder="编码"></el-input>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button type="primary" icon="ios-search" @click="toPage(1)"
            >查询</el-button
          >
          <el-button
            v-if="checkPrivilege('app_add')"
            type="success"
            icon="md-add"
            @click="toAdd"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column prop="code" label="编码"> </el-table-column>
        <el-table-column prop="masterVersionName" label="主干版本" width="120">
        </el-table-column>
        <el-table-column
          v-if="checkPrivilege('app_edit')"
          label="操作"
          width="80"
          class-name="action-column"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="管理" width="320" class-name="action-column">
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('app_user')"
              class="btn-primary"
              type="text"
              @click="toEditUser(scope.row)"
              >用户</el-button
            >
            <el-button
              v-if="checkPrivilege('app_module')"
              class="btn-primary"
              type="text"
              @click="toEditModule(scope.row)"
              >模块</el-button
            >
            <el-button
              v-if="checkPrivilege('app_version')"
              class="btn-primary"
              type="text"
              @click="toEditVersion(scope.row)"
              >版本</el-button
            >
            <el-button
              v-if="checkPrivilege('app_env')"
              class="btn-primary"
              type="text"
              @click="toEditEnv(scope.row)"
              >环境</el-button
            >
            <el-button
              v-if="checkPrivilege('app_config')"
              class="btn-primary"
              type="text"
              @click="toEditConfig(scope.row)"
              >程序配置</el-button
            >
            <el-button
              v-if="checkPrivilege('app_config_nginx')"
              class="btn-primary"
              type="text"
              @click="toEditConfigNginx(scope.row)"
              >nginx配置</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total,prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>

    <!-- modify-app -->
    <modify-app
      v-if="checkPrivilege('app_add') || checkPrivilege('app_edit')"
      ref="ModifyApp"
      :instance="curRow"
      @modified="getList"
    >
    </modify-app>
    <!-- AppUserManage -->
    <app-user-manage
      v-if="checkPrivilege('app_user')"
      ref="AppUserManage"
      :app="curRow"
    ></app-user-manage>
    <!-- AppModuleManage -->
    <app-module-manage
      v-if="checkPrivilege('app_module')"
      ref="AppModuleManage"
      :app="curRow"
    ></app-module-manage>
    <!-- AppVersionManage -->
    <app-version-manage
      v-if="checkPrivilege('app_version')"
      ref="AppVersionManage"
      :app="curRow"
      @master-change="getList"
    ></app-version-manage>
    <!-- AppEnvManage -->
    <app-env-manage
      v-if="checkPrivilege('app_env')"
      ref="AppEnvManage"
      :app="curRow"
    ></app-env-manage>
    <!-- AppConfigManage -->
    <app-config-manage
      v-if="checkPrivilege('app_config')"
      ref="AppConfigManage"
      :app="curRow"
    ></app-config-manage>
    <!-- AppNginxManage -->
    <app-nginx-manage
      v-if="checkPrivilege('app_config_nginx')"
      ref="AppNginxManage"
      :app="curRow"
    ></app-nginx-manage>
  </div>
</template>

<script>
import { appQuery } from "../api";
import ModifyApp from "../components/ModifyApp";
import AppUserManage from "../components/AppUserManage.vue";
import AppModuleManage from "../components/AppModuleManage.vue";
import AppVersionManage from "../components/AppVersionManage.vue";
import AppEnvManage from "../components/AppEnvManage.vue";
import AppConfigManage from "../components/AppConfigManage.vue";
import AppNginxManage from "../components/AppNginxManage.vue";

export default {
  name: "app-manage",
  components: {
    ModifyApp,
    AppUserManage,
    AppModuleManage,
    AppVersionManage,
    AppEnvManage,
    AppConfigManage,
    AppNginxManage
  },
  data() {
    return {
      filter: {
        nameStartWith: "",
        code: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      loading: false
    };
  },
  created() {
    this.initData();
  },
  methods: {
    initData() {
      this.toPage(1);
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await appQuery(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toAdd() {
      this.curRow = {};
      this.$refs.ModifyApp.open();
    },
    toEdit(row) {
      this.curRow = row;
      this.$refs.ModifyApp.open();
    },
    toEditUser(row) {
      this.curRow = row;
      this.$refs.AppUserManage.open();
    },
    toEditModule(row) {
      this.curRow = row;
      this.$refs.AppModuleManage.open();
    },
    toEditVersion(row) {
      this.curRow = row;
      this.$refs.AppVersionManage.open();
    },
    toEditEnv(row) {
      this.curRow = row;
      this.$refs.AppEnvManage.open();
    },
    toEditConfig(row) {
      this.curRow = row;
      this.$refs.AppConfigManage.open();
    },
    toEditConfigNginx(row) {
      this.curRow = row;
      this.$refs.AppNginxManage.open();
    }
  }
};
</script>
