<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 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="主干版本">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="170">
          <span slot-scope="scope">{{
            scope.row.createTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="updateTime" label="修改时间" width="170">
          <span slot-scope="scope">{{
            scope.row.updateTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column 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="300" class-name="action-column">
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toEditUser(scope.row)"
              >用户</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              @click="toEditModule(scope.row)"
              >模块</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              @click="toEditVersion(scope.row)"
              >版本</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              @click="toEditEnv(scope.row)"
              >环境</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              @click="toEditBaseline(scope.row)"
              >配置基线</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              @click="toEditConfig(scope.row)"
              >配置</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 ref="ModifyApp" :instance="curRow" @modified="getList">
    </modify-app>
    <!-- AppUserManage -->
    <app-user-manage ref="AppUserManage" :app="curRow"></app-user-manage>
    <!-- AppModuleManage -->
    <app-module-manage ref="AppModuleManage" :app="curRow"></app-module-manage>
    <!-- AppVersionManage -->
    <app-version-manage
      ref="AppVersionManage"
      :app="curRow"
      @master-change="getList"
    ></app-version-manage>
    <!-- AppEnvManage -->
    <app-env-manage ref="AppEnvManage" :app="curRow"></app-env-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";

export default {
  name: "app-manage",
  components: {
    ModifyApp,
    AppUserManage,
    AppModuleManage,
    AppVersionManage,
    AppEnvManage
  },
  data() {
    return {
      filter: {
        nameStartWith: "",
        code: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [
        {
          id: "1",
          name: "应用1",
          code: "app1",
          masterVersionName: "1.0.0",
          masterVersionId: "11",
          createTime: Date.now(),
          updateTime: Date.now()
        }
      ],
      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();
    },
    toEditBaseline(row) {
      console.log(row);
    },
    toEditConfig(row) {
      console.log(row);
    }
  }
};
</script>
