<template>
  <div class="root_container">
    <el-dialog
      :title="currentData.dialogTitle"
      :visible.sync="dialogShow"
    >
      <el-form
        :model="formData"
        style="padding-top:18px"
      >
        <el-form-item
          label="接口名称"
          :label-width="formLabelWidth"
        >
          <el-input
            placeholder="例如: 安卓首页接口"
            style="width:300px"
            v-model="currentData.title"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="接口路径"
          :label-width="formLabelWidth"
        >
          <el-input
            :disabled="currentData.type == 2"
            style="width:300px"
            placeholder="例如: /home/index"
            v-model="currentData.path"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          v-if="currentData.request_url"
          label="请求路径"
          :label-width="formLabelWidth"
        >
          <a
            style="color:#409EFF"
            target="blank"
            :href="currentData.request_url"
          >{{currentData.request_url}}</a>
        </el-form-item>
        <el-form-item
          label="响应数据"
          style="padding-right:30px"
          :label-width="formLabelWidth"
        >
          <div class="codeEditBox">
            <editor
              v-model="currentData.response"
              @init="editorInit"
              @input='codeChange'
              lang="javascript"
              :options="editorOptions"
              theme="chrome"
            ></editor>
          </div>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogShow = false">取 消</el-button>
        <el-button
          type="primary"
          @click="onDialogOkClick"
        >{{currentData.okBtn}}</el-button>
      </div>
    </el-dialog>
    <div class="header">
      <div class="form_container">
        <el-form
          :inline="true"
          :model="formData"
          class="demo-form-inline"
        >
          <el-form-item label="查询条件">
            <el-input
              v-model="formData.search"
              placeholder="请搜索内容"
              @change="onDoSearch"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              style="margin-left:10px"
              type="primary"
              @click="onDoSearch"
            >查询</el-button>
            <el-button
              type="success"
              plain
              @click="addUserBtn"
            >新增接口</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="pager">
        <span style="color:#999999;display:inline-block;padding-top:9px">共 {{totalCount}} 条数据，每页 {{pageSize}} 条</span>
        <div
          class="pager_item"
          style="display:inline-block"
        >
          <el-pagination
            background
            :page-size="pageSize"
            @current-change="pageChange"
            layout="prev, pager, next"
            :total="totalCount"
            :current-page="currentPage"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <div class="table_container">
      <el-table
        :data="tableData"
        :height="tableHeight"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="index"
          label=""
          width="40"
        >
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="path"
          label="接口路径"
        >
        </el-table-column>
        <el-table-column
          prop="add_time"
          label="添加时间"
          width="180"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              plain
              @click="onDetails(scope.row)"
            >查看详情</el-button>
            <el-button
              type="danger"
              plain
              @click="onDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import Editor from "vue2-ace-editor";
import {
  createMock,
  mockList,
  mockDetails,
  mockUpdate,
  mockDelete,
} from "../../api/log/mock";
import { Message } from "element-ui";
export default {
  components: {
    Editor,
  },
  data() {
    return {
      editorOptions: {
        // 设置代码编辑器的样式
        enableBasicAutocompletion: true, //启用基本自动完成
        enableSnippets: true, // 启用代码段
        enableLiveAutocompletion: true, //启用实时自动完成
        tabSize: 2, //标签大小
        fontSize: 14, //设置字号
        showPrintMargin: false, //去除编辑器里的竖线
      },
      //记录高度
      tableHeight: -1,

      formData: {
        search: "",
      },

      dialogShow: false,
      formLabelWidth: "100px",

      //分页数据
      totalCount: 0,
      currentPage: 1,
      pageSize: 20,

      tableData: [],

      dataPlaceHolder: '{"name":"穆仙念"}',

      //当前的数据
      currentData: {
        dialogTitle: "",
        title: "",
        path: "",
        request_url: "",
        response: "",
        type: -1, // 1 新增 2 修改
        id: -1,
      },
    };
  },
  watch: {
    "$store.state.screenHeight": function (val) {
      this.tableHeight = val - 98 - 50;
    },
  },
  mounted: function () {
    this.tableHeight = this.$store.state.screenHeight - 98 - 50;

    this.onObtainData();
  },
  methods: {
    codeChange(val) {
      console.log(val);
    },
    editorInit() {
      require("brace/theme/chrome");
      require("brace/ext/language_tools"); //language extension prerequsite...
      require("brace/mode/yaml");
      require("brace/mode/json");
      require("brace/mode/less");
      require("brace/snippets/json");
      require("brace/mode/lua");
      require("brace/snippets/lua");
      require("brace/mode/javascript");
      require("brace/snippets/javascript");
    },
    //页面发生变化
    pageChange(page) {
      this.currentPage = page;

      this.onObtainData();
    },
    //删除
    onDelete(item){
        this.$confirm('删除之后该接口mock地址将无法被访问, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          mockDelete(item.id).then((result) => {
              Message.success("删除成功");
              this.onObtainData()
          }).catch((err) => {
              
          });
        }).catch(() => {
                 
        });
    },
    //获取数据
    onObtainData() {
      mockList(this.currentPage, this.pageSize, this.formData.search)
        .then((result) => {
          console.log(result);
          this.tableData = result.data.data.list;

          this.totalCount = result.data.data.total;
          this.currentPage = result.data.data.currentPage;
          this.pageSize = result.data.data.pageSize;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //对话框里面的ok点击
    onDialogOkClick() {
      if (this.currentData.type == 1) {
        //新增
        //检查内容
        if (!this.currentData.title) {
          Message.warning("请输入标题");
          return;
        }
        if (!this.currentData.path) {
          Message.warning("请输入接口路径");
          return;
        }
        if (!this.currentData.response) {
          Message.warning("请输入响应数据");
          return;
        }
        createMock(this.currentData.title,this.currentData.path,this.currentData.response).then((result) => {
            Message.success("添加成功");
            this.dialogShow = false;

            this.onObtainData();
        }).catch((err) => {
            
        });
      }
      if (this.currentData.type == 2) {
        //修改
        mockUpdate(this.currentData.id, this.currentData.title,this.currentData.response)
          .then((result) => {
            Message.success("修改成功");
            this.dialogShow = false;

            this.onObtainData();
          })
          .catch((err) => {});
      }
    },
    //点击搜索
    onDoSearch() {
      this.onObtainData();
    },
    //点击添加用户
    addUserBtn() {
      this.currentData.dialogTitle = "新增接口";
      this.currentData.title = "";
      this.currentData.path = "";
      this.currentData.request_url = "";
      this.currentData.response = "";
      this.currentData.okBtn = "添 加";
      this.currentData.id = -1;
      this.currentData.type = 1;
      this.dialogShow = true;
    },
    //查看详情
    onDetails(item) {
      mockDetails(item.id)
        .then((result) => {
          var data = result.data.data;
          this.currentData.dialogTitle = "接口详情";
          this.dialogShow = true;
          this.currentData.title = data.title;
          this.currentData.path = data.path;
          this.currentData.id = data.id;
          this.currentData.request_url = data.request_url;
          this.currentData.response = data.response;
          this.currentData.okBtn = "确 定";
          this.currentData.type = 2;
        })
        .catch((err) => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.root_container {
  position: relative;
}
.table_container {
  padding-left: 18px;
  padding-right: 18px;
}
.pager {
  position: absolute;
  top: 26px;
  right: 18px;
  display: flex;
}
.header {
  padding-top: 18px;
  padding-left: 18px;
}
.codeEditBox {
  width: 100%;
  height: 300px;
  border: 1px solid #dcdee2;
}
</style>