<script>
import axios from "axios";
import { inject } from "vue";
import { getCIDLogListService } from "@/api/cid.js";
import { runCIDService } from "@/api/cid.js";
import { addCIDService } from "@/api/cid.js";
import { deleteCIDService } from "@/api/cid.js";
import { updateCIDService } from "@/api/cid.js";
import router from "@/router/index.js";
import Menu from "@/views/Menu.vue";

export default {
  data() {
    return {
      ip: "",
      tableData: [],
      tokenData: {
        token: localStorage.getItem("token"),
        ip: localStorage.getItem("ip"),
        id:localStorage.getItem("cid_id"),
        userId: localStorage.getItem("userId"),
        username: localStorage.getItem("username"),
      },
      addDialogVisible: false,
      updateDialogVisible: false,
      updateForm: {
        create_time: "",
        script: "",
        end: "",
        error: "",
      },
    };
  },

  // methods 是一些用来更改状态与触发更新的函数
  // 它们可以在模板中作为事件处理器绑定
  methods: {
    async getCIDLogList() {
      let result = {};
      try {
        result = await getCIDLogListService(this.tokenData);
      } catch (e) {
        console.log(e);
      }
      let data = result.data;


      this.tableData = data;
    },
    onSubmit() {
      getDeviceList({ token: token });
    },
    handleSizeChange() {
      alert("每页记录数变化" + val);
    },
    handleCurrentChange() {
      alert("页码发生变化" + val);
    },

    //设置项目执行构建部署
    async runCID(index) {
      var id = this.tableData[index].ID;
      var run_data = {
        id: id,
        ip: this.ip,
        userId: this.tokenData.userId,
        token: this.tokenData.token,
      };
      try {
        var d_re = await runCIDService(run_data);
        if (d_re.code == 0) {
          alert("操作成功");
        } else {
          alert("操作失败");
        }
      } catch (e) {
        console.log(e);
      }
    },
    async deleteCID(index) {
      var id = this.tableData[index].ID;
      var delete_data = {
        id: id,
        userId: this.tokenData.userId,
        token: this.tokenData.token,
      };
      try {
        var d_re = await deleteCIDService(delete_data);
        if (d_re.code == 0) {
          alert("删除成功");
          //刷新页面
          this.getDeviceList();
        } else {
          alert("操作失败");
        }
      } catch (e) {
        console.log(e);
      }
    },
    async updateButtonLogCID(index) {
      this.updateDialogVisible = true;
      var id = this.tableData[index].ID;
      this.updateForm.create_time = this.tableData[index].CreatedAt;
      this.updateForm.script = this.tableData[index].Script;
      this.updateForm.end = this.tableData[index].Log;
      this.updateForm.error = this.tableData[index].Error;
    },
    async getIpClient() {
      try {
        const response = await axios.get("https://ipinfo.io/json");
        this.ip = response.data.ip;
        localStorage.setItem("ip", this.ip);
        //console.log(response);
      } catch (error) {
        console.error(error);
      }
    },
    handleMenuSelect(val) {
      router.push(val);
    },
    toVideoList() {
      router.push("/videoList");
    },
    // 修改条纹颜色
    tableRowClassName({ row, rowIndex }) {
      if (row.human === 1) {
        return {
          background: "#488aff",
        };
      } else {
        return "";
      }
    },
  },

  // 生命周期钩子会在组件生命周期的各个不同阶段被调用
  // 例如这个函数就会在组件挂载完成后被调用
  async mounted() {
    let now = new Date();
    if (localStorage.getItem("token") === null) {
      router.push("/login");
    }
    // console.log("mounted");
    this.getIpClient();
    this.getCIDLogList();
  },
};
</script>

<template>
  <div>
    <Menu></Menu>
    <el-container style="height: 700px; border: 1px solid #eee">
      <el-header style="font-size: 40px; background-color: rgb(238, 241, 246)"
        >集成部署项目日志{{ tokenData.id }}</el-header
      >
      <el-container>
        <el-main>
          <!-- 表单 -->
          <el-form :inline="true" :model="tokenData" class="demo-form-inline">
            <el-form-item>
              <el-button
                class="el-button--danger"
                type="primary"
                @click="getCIDLogList()"
                >刷新</el-button
              >
            </el-form-item>

            <el-form-item>
              <el-dialog
                v-model="updateDialogVisible"
                title="查看集成项目日志"
                width="50%"
                :before-close="handleClose"
              >
                <!-- 内容主体区域 -->
                <el-form
                  ref="addFormRef"
                  :model="updateForm"
                  :rules="updateFormRules"
                  label-width="70px"
                >
                  <el-form-item label="创建时间" prop="create_time">
                    <el-input
                      v-model="updateForm.create_time"
                      autocomplete="on"
                      style="width: 600px" 
                    ></el-input>
                  </el-form-item>
                  <el-form-item label="脚本内容" prop="script">
                    <el-input type="textarea" v-model="updateForm.script" style="width: 600px" :autosize="{ minRows: 4, maxRows: 8 }" />
                  </el-form-item>
                  <el-form-item label="执行结果" prop="end">
                    <el-input type="textarea" v-model="updateForm.end" style="width: 600px" :autosize="{ minRows: 4, maxRows: 8 }" />
                  </el-form-item>
                  <el-form-item label="错误内容" prop="error">
                    <el-input type="textarea" v-model="updateForm.error" style="width: 600px" :autosize="{ minRows: 4, maxRows: 8 }" />
                  </el-form-item>
                </el-form>
                <!-- 底部区域 -->
                <template #footer>
                  <span class="dialog-footer">
                    <el-button @click="updateDialogVisible = false"
                      >关闭</el-button
                    >
                  </span>
                </template>
              </el-dialog>
            </el-form-item>
          </el-form>

          <!-- 表格  :row-style="this.tableRowClassName"-->
          <el-table :data="tableData" width="100%" border>
            :row-style="this.tableRowClassName"
            <el-table-column prop="ID" label="id" width="80"></el-table-column>
            <el-table-column
              prop="CreatedAt"
              label="创建时间"
              width="100"
            ></el-table-column>
            <el-table-column
              prop="Script"
              label="执行脚本"
              width="180"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column
              prop="Log"
              label="运行日志"
              width="180"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column
              prop="Error"
              label="错误信息"
              width="250"
            ></el-table-column>
            <el-table-column
              prop="RunTime"
              label="运行时间(s)"
              width="50"
            ></el-table-column>
            <el-table-column label="操作" width="350">
              <template #default="scope">
                <el-button
                  type="primary"
                  size="mini"
                  @click.prevent="updateButtonLogCID(scope.$index)"
                  >详情</el-button
                >
                <!-- <el-button type="danger" size="mini">删除</el-button> -->
              </template>
            </el-table-column>
          </el-table>
          <br />

          <!-- 分页条 -->
          <!-- Pagination 分页 -->
          <!-- <el-pagination
            background
            layout="total,sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :total="1000"
          ></el-pagination> -->
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<style>
.blueRowbg {
  background: "#488aff";
}
</style>