<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-form-item>
          <el-input
            v-model="dataForm.applicationName"
            placeholder="应用名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-button @click="getDataListByName()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          label="应用类型"
          v-model="dataForm.deployDeviceId"
          @change="getDataListByType(dataForm.applicationType)"
        >
          <el-option
            v-for="data in deployDevices"
            :key="data.edgenodesId"
            :label="data.edgenodesName"
            :value="data.edgenodesId"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="applicationName"
        header-align="center"
        align="center"
        label="名称"
      >
      </el-table-column>
      <el-table-column
        prop="applicationId"
        header-align="center"
        align="center"
        label="应用ID"
      >
      </el-table-column>
      <el-table-column
        prop="applicationVersion"
        header-align="center"
        align="center"
        label="应用版本"
      >
      </el-table-column>
      <el-table-column
        prop="applicationType"
        header-align="center"
        align="center"
        label="应用类型"
      >
      </el-table-column>

      <el-table-column
        prop="applicationStatus"
        header-align="center"
        align="center"
        label="应用状态"
      >
      </el-table-column>

      <el-table-column
        prop="deployDeviceName"
        header-align="center"
        align="center"
        label="部署设备"
      >
      </el-table-column>
      <!-- <el-table-column
        prop="parentProjectName"
        header-align="center"
        align="center"
        label="所属项目名称"
      >
      </el-table-column> -->
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间"
      >
      </el-table-column>
      <el-table-column
        prop="creatorName"
        header-align="center"
        align="center"
        label="创建者"
      >
      </el-table-column>

      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-row>
            <el-button
              type="text"
              size="small"
              @click="viewApplicationDetail(scope.row.applicationId)"
              >查看应用日志</el-button
            ></el-row
          >
          <!-- <el-row>
            <el-button
              type="text"
              size="small"
              @click="viewApplicationResult(scope.row.applicationId)"
              >查看应用结果</el-button
            ></el-row
          > -->
          <el-row>
            <el-button
              type="text"
              size="small"
              @click="applicationMonitorView(scope.row.applicationId)"
              >查看应用监控</el-button
            ></el-row
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="this.$store.state.settings.pageSizeList"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改  详情 监控 -->
    <!-- 这里将添加去掉，留下详情和监控两个控件，用于显示具体信息 -->
    <application-log v-if="viewDetailVisible" ref="viewDetail">
    </application-log>
    <application-view v-if="applicationViewVisible" ref="applicationView">
    </application-view>
    <apprunning-log v-if="appRunningLogVisible" ref="apprunningLog">
    </apprunning-log>
  </div>
</template>

<script>
import applicationLog from "./applicationMonitor/applicationLog.vue";
import applicationView from "./applicationMonitor/appMonitor.vue";
import apprunningLog from "./applicationMonitor/appRunningLog.vue";
export default {
  data() {
    return {
      dataForm: {
        applicationType: "",
        applicationName: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: this.$store.state.settings.pageSize,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      viewDetailVisible: false,
      appRunningLogVisible:false,
      applicationViewVisible: false,
      filterVal: "",
      applicationTypeOptions: [

      ],
      deployDevices:[

      ]
    };
  },
  components: {
    applicationLog,
    applicationView,
    apprunningLog,
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      /* this.$http({
        url: this.$http.adornUrl("/category/select"),
        method: "get",
      }).then(({ data }) => {
        this.applicationTypeOptions = this.unique(data.list);
      }); */
      // 获取所有边缘结点
      this.$http({
        url: this.$http.adornUrl('/generator/edgenodes/list'),
        method: 'get',
        params: this.$http.adornParams({

        })
      }).then(({data}) => {
        this.deployDevices=data.page.list;
        var center={edgenodesId:0,edgenodesName:"中心云"};
        this.deployDevices.push(center);
      })
      this.$http({
        url: this.$http.adornUrl("/generator/applications/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          deployDeviceId: this.dataForm.deployDeviceId,
          applicationName: this.dataForm.applicationName,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list.map((element) => {
            element["state"] = 0;
            element["loadingText"] = "启动";
            return element;
          });
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    //去重
    unique(arr) {
      var ss = [];
      for (var i = 0; i < arr.length; i++) {
        ss[i] = arr[i].categoryName;
      }
      return Array.from(new Set(ss));
    },

    // 查看详情
    viewApplicationDetail(id) {
      var item = this.dataList.filter((item) => item.applicationId === id);
      // 获取应用的name和版本号,传入到详情
      console.log("查看item中数据" + item);
      this.appRunningLogVisible = true;
      this.$nextTick(() => {
        this.$refs.apprunningLog.init(item[0].applicationName);
      });
    },
    viewApplicationResult(id){
      var item = this.dataList.filter((item) => item.applicationId === id);
      // 获取应用的name和版本号,传入到详情
      console.log("查看item中数据" + item);
      this.viewDetailVisible = true;
      this.$nextTick(() => {
        this.$refs.viewDetail.init(item[0].applicationName);
      });
    },
    //查看监控部分
    applicationMonitorView(id) {
      var item = this.dataList.filter((item) => item.applicationId === id);
      // 获取应用的name和版本号,传入到详情
      //  console.log('查看item中数据'+item)
      this.applicationViewVisible = true;
      this.$nextTick(() => {
        this.$refs.applicationView.init(item[0].applicationName);
      });
      // this.$nextTick(() => {
      //   this.$refs.viewDetail.init(
      //     item[0].applicationName,
      //     item[0].applicationVersion,
      //     item[0].createTime,
      //     item[0].applicationType,
      //     item[0].applicationDescription,
      //     item[0].parentProjectName,
      //   );
      // });
    },

    // 通过name获取id
    getNameById(id) {
      var item = this.dataList.filter((item) => item.applicationId === id);
      return item[0].applicationName;
    },

    getDataListByName(value) {
      console.log(
        "getDataListByName  dataForm  " +
          this.dataForm.applicationId +
          "|" +
          this.dataForm.applicationName +
          "|" +
          this.dataForm.applicationType
      );
      this.getDataList();
    },
    //根据下拉框进行查询，这里是指通过镜像类型进行查询
    getDataListByType(value) {
      console.log(
        "getDataListByType  dataForm  " +
          this.dataForm.applicationId +
          "|" +
          this.dataForm.applicationName +
          "|" +
          this.dataForm.applicationType
      );
      this.getDataList();
    },
  },
};
</script>


