<template>
  <div>
    <Card :bordered="false" dis-hover class="ivu-mt">
      <Button type="primary" @click="groupAdd()" class="mr20">添加功能</Button>
      <!-- <Button type="success" @click="buildCode()" class="mr20">重新发布</Button> -->
      <Table
        :columns="columns1"
        :data="tabList"
        ref="table"
        class="mt25"
        :loading="loading"
        highlight-row
        no-userFrom-text="暂无数据"
        no-filtered-userFrom-text="暂无筛选结果"
      >
        <template slot-scope="{ row, index }" slot="statuss">
          <i-switch
            v-model="row.status"
            :value="row.status"
            :true-value="1"
            :false-value="0"
            @on-change="onchangeIsShow(row)"
            size="large"
          >
            <span slot="open">显示</span>
            <span slot="close">隐藏</span>
          </i-switch>
        </template>
        <template slot-scope="{ row, index }" slot="action">
          <a @click="edit(row, '编辑')">查看</a>
          <Divider type="vertical" />
          <a @click="editItem(row)">编辑</a>
          <Divider type="vertical" />
          <a @click="downLoad(row)">下载</a>
          <Divider type="vertical" />
          <a @click="del(row, '删除', index)">删除</a>
        </template>
      </Table>
      <div class="acea-row row-right page">
        <Page
          :total="total"
          :current="formValidate.page"
          show-elevator
          show-total
          @on-change="pageChange"
          :page-size="formValidate.limit"
        />
      </div>
    </Card>
    <Drawer
      :class-name="className"
      title="Create"
      v-model="modals"
      width="80%"
      :mask-closable="false"
      :styles="styles"
      :before-close="editModalChange"
    >
      <!-- <Modal
      v-model="modals"
      scrollable
      footer-hide
      closable
      :mask-closable="false"
      width="80%"
      :before-close="editModalChange"
    > -->

      <p slot="header" class="diy-header" ref="diyHeader">
        <span>{{ title }}</span>
      </p>
      <div class="file" style="height: 100%">
        <Button class="save" type="primary" @click="pwdModal = true">保存</Button>

        <div class="file-box">
          <div class="file-fix"></div>
          <div class="file-content">
            <!-- <Tabs
              type="card"
              v-model="indexEditor"
              style="height: 100%"
              @on-click="toggleEditor"
              :animated="false"
              closable
              @on-tab-remove="handleTabRemove"
            >
              <TabPane
                v-for="value in editorIndex"
                :key="value.index"
                :name="value.index.toString()"
                :label="value.title"
                :icon="value.icon"
              >
                <div
                  ref="container"
                  :id="'container_' + value.index"
                  style="height: 100%; min-height: calc(100vh - 110px)"
                ></div>
              </TabPane>
            </Tabs> -->
            <el-tabs v-model="indexEditor" type="card" @tab-click="toggleEditor">
              <el-tab-pane v-for="value in editorIndex" :key="value.index">
                <span slot="label">
                  <el-tooltip class="item" effect="dark" :content="value.title" placement="top">
                    <span>{{ value.file_name }}</span>
                  </el-tooltip>
                </span>
                <div
                  ref="container"
                  :id="'container_' + value.index"
                  style="height: 100%; min-height: calc(100vh - 110px)"
                ></div>
              </el-tab-pane>
              <!-- <el-tab-pane label="用户管理" name="first">用户管理</el-tab-pane>
              <el-tab-pane label="配置管理" name="second">配置管理</el-tab-pane>
              <el-tab-pane label="角色管理" name="third">角色管理</el-tab-pane>
              <el-tab-pane label="定时任务补偿" name="fourth">定时任务补偿</el-tab-pane> -->
            </el-tabs>
          </div>
          <Spin size="large" fix v-if="spinShow"></Spin>
        </div>
      </div>

      <!-- </Modal> -->
      <!-- <div class="demo-drawer-footer">
        <Button style="margin-right: 8px" @click="modals = false">关闭</Button>
        <Button type="primary" @click="modals = false">保存</Button>
      </div> -->
    </Drawer>
    <Modal
      v-model="buildModals"
      scrollable
      title="终端"
      footer-hide
      closable
      :mask-closable="false"
      width="60%"
      :before-close="editModalChange"
    >
      <Alert type="warning">当前终端未运行于安装服务下，部分命令可能无法执行.</Alert>
      <div>
        <div v-for="(item, index) in codeBuildList" :key="index">{{ item }}</div>
      </div>
    </Modal>
    <Modal
      v-model="pwdModal"
      title="文件管理密码"
      :closable="false"
      @on-ok="crudSaveFile"
      @on-cancel="pwdModal = false"
    >
      <Input v-model="pwd" type="password" placeholder="请输入文件管理密码"></Input>
    </Modal>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { crudList, crudDet, crudDownload, crudSaveFile } from '@/api/systemCodeGeneration';
import * as monaco from 'monaco-editor';
import { getCookies, removeCookies } from '@/libs/util';
import Setting from '@/setting';
import { Input } from 'view-design';
export default {
  data() {
    return {
      grid: {
        xl: 7,
        lg: 7,
        md: 12,
        sm: 24,
        xs: 24,
      },
      formValidate: {
        page: 1,
        limit: 20,
        title: '',
      },
      styles: {
        height: 'calc(100% - 55px)',
        overflow: 'auto',
        paddingBottom: '53px',
        position: 'static',
      },
      loading: false,
      pwdModal: false,
      buildModals: false,
      pwd: '',
      tabList: [],
      codeBuildList: [],
      total: 0,
      columns1: [
        {
          title: 'ID',
          key: 'id',
          width: 80,
        },
        {
          title: '菜单名',
          key: 'name',
          minWidth: 130,
        },
        {
          title: '表名',
          key: 'table_name',
          minWidth: 130,
        },
        {
          title: '字符集',
          key: 'table_collation',
          minWidth: 130,
        },
        {
          title: '表备注',
          key: 'table_comment',
          minWidth: 130,
        },
        {
          title: '添加时间',
          key: 'add_time',
          minWidth: 130,
        },
        {
          title: '操作',
          slot: 'action',
          fixed: 'right',
          minWidth: 150,
        },
      ],
      FromData: null,
      titleFrom: '',
      groupId: 0,
      addId: '',
      editorList: [], //编辑器数组
      indexEditor: 0, //当前编辑器索引
      code: '', //当前文件打开时的内容
      contextData: null, //左侧导航右键点击是产生的数据对象

      fileType: '', // 文件操作类型 createFolder|创建文件夹 createFile|创建文件 delFolder|删除文件夹或者文件
      className: '', //全屏 class名
      spinShow: false,
      modals: false, //编辑器开关
      editor: '', //当前编辑器对象
      editorIndex: [],
      title: '',
      editId: 0,
    };
  },
  computed: {
    ...mapState('media', ['isMobile']),
    labelWidth() {
      return this.isMobile ? undefined : 75;
    },
    labelPosition() {
      return this.isMobile ? 'top' : 'right';
    },
  },
  mounted() {
    this.getList();
  },
  beforeDestroy() {
    if (this.source) {
      this.source.close(); //关闭EventSource
    }
  },
  methods: {
    crudSaveFile() {
      let data = {
        filepath: this.editorIndex[this.indexEditor].pathname,
        comment: this.editorList[this.indexEditor].editor.getValue(),
        pwd: this.pwd,
      };
      crudSaveFile(this.editId, data)
        .then((res) => {
          this.pwd = '';
          this.$Message.success(res.msg);
        })
        .catch((err) => {
          this.$Message.error(err.msg);
        });
    },
    downLoad(row) {
      crudDownload(row.id).then((res) => {
        window.open(res.data.download_url, '_blank');
      });
    },
    buildCode() {
      this.buildModals = true;
      if (typeof EventSource !== 'undefined') {
        //支持eventSource
        var postURL = Setting.apiBaseURL + '/system/crud/npm?token=' + getCookies('token');
        this.source = new EventSource(postURL);
        let self = this; //因EventSource中this的指向变了，所以要提前存储一下
        this.source.onopen = function (res) {};
        this.source.onmessage = function (data) {};
        this.source.onerror = function (err) {
          //链接失败后EventSource会每隔三秒左右重新发起链接
        };
      } else {
        console.log('暂不支持EventSource');
      }
    },
    // 跳转到组合数据列表页面
    goList(row) {
      this.$router.push({
        path: this.$routeProStr + '/system/config/system_group/list/' + row.id,
      });
    },
    // 列表
    getList() {
      this.loading = true;
      crudList(this.formValidate)
        .then(async (res) => {
          let data = res.data;
          this.tabList = data.list;
          this.total = data.count;
          this.loading = false;
        })
        .catch((res) => {
          this.loading = false;
          this.$Message.error(res.msg);
        });
    },
    pageChange(index) {
      this.formValidate.page = index;
      this.getList();
    },
    // 表格搜索
    userSearchs() {
      this.formValidate.page = 1;
      this.getList();
    },
    // 点击添加
    groupAdd() {
      this.$router.push({
        name: 'system_code_generation',
      });
    },
    // 删除
    del(row, tit, num) {
      let delfromData = {
        title: tit,
        num: num,
        url: `system/crud/${row.id}`,
        method: 'DELETE',
        ids: '',
      };
      this.$modalSure(delfromData)
        .then((res) => {
          this.$Message.success(res.msg);
          this.tabList.splice(num, 1);
          this.getList();
        })
        .catch((res) => {
          this.$Message.error(res.msg);
        });
    },
    // 编辑
    edit(row) {
      this.spinShow = true;
      // 创建代码容器
      this.title = row.name;
      this.$nextTick((e) => {
        this.openfile(row.id, false);
      });
    },
    editItem(row) {
      this.$router.push({
        name: 'system_code_generation',
        query: {
          id: row.id,
        },
      });
    },
    //打开文件
    openfile(id) {
      try {
        this.editId = id;
        let that = this;
        this.editorIndex = [];
        this.editorList = [];
        crudDet(id)
          .then(async (res) => {
            let data = res.data.file[0];
            res.data.file.map((i, index) => {
              let data = i;
              this.editorIndex.push({
                tab: true,
                index: index + '',
                title: data.name,
                file_name: data.file_name,
                pathname: data.path,
              });
              that.code = data.content;
              this.initEditor(index, data.content);
              this.$nextTick((e) => {
                // 保存相对信息
                that.editorList[index].path = data.path;
                that.editorList[index].oldCode = that.content;
                that.editorIndex[index].title = data.name;
                that.editorIndex[index].file_name = data.file_name;
              });
            });
            that.modals = true;
            that.spinShow = false;
          })
          .catch((res) => {
            that.catchFun(res);
          });
      } catch (error) {
        console.log(error);
      }
    },
    /**
     * 窗口最大化
     */
    winChanges() {
      if (this.className) {
        this.className = '';
      } else {
        this.className = 'diy-fullscreen';
      }
    },
    /**
     * 初始化编辑器
     */
    initEditor(index, conetnt) {
      try {
        let that = this;
        that.$nextTick(() => {
          // 初始化编辑器，确保dom已经渲染
          that.editor = monaco.editor.create(document.getElementById('container_' + index), {
            value: conetnt, //编辑器初始显示文字
            language: 'sql', //语言支持自行查阅demo
            automaticLayout: true, //自动布局
            theme: 'vs', //官方自带三种主题vs, hc-black, or vs-dark
            foldingStrategy: 'indentation', // 代码可分小段折叠
            overviewRulerBorder: false, // 不要滚动条的边框
            scrollbar: {
              // 滚动条设置
              verticalScrollbarSize: 4, // 竖滚动条
              horizontalScrollbarSize: 10, // 横滚动条
            },
            autoIndent: true, // 自动布局
            tabSize: 4, // tab缩进长度
            autoClosingOvertype: 'always',
            readOnly: false,
          });
          that.editorList.push({
            editor: that.editor,
            oldCode: that.code,
            path: '',
            index: index,
          });
        });
      } catch (error) {
        console.log(error);
      }
    },

    /**
     * 处理接口回调
     * @param {Object} res
     */
    catchFun(res) {
      if (res.status) {
        if (res.status == 400) this.$Message.error(res.msg);
        if (res.status == 110008) {
          // this.$Message.error(res.msg);
          this.isShowLogn = true;
          this.isShowList = false;
          this.loading = false;
        }
      } else {
        // this.$Message.error('文件编码不被兼容，无法正确读取文件!');
      }
      //关闭蒙版层
      if (this.spinShow) this.spinShow = false;
      // 关闭文件列表展示
      if (this.loading) this.loading = false;
    },
    //编辑器状态变化
    editModalChange() {
      let that = this;
      that.editorList.forEach(function (value, index) {
        // 销毁当前编辑器
        that.editorList[index].editor.dispose();
        that.editorList[index].editor = null;
      });
      // 初始话数据
      that.modals = false; //编辑器开关
      that.editor = ''; //当前编辑器对象
      that.editorIndex = [
        //选项卡数组
        {
          tab: true,
          index: '0',
          title: '',
          icon: '',
        },
      ];
      that.editorList = []; //编辑器数组
      that.indexEditor = '0'; //当前编辑器索引
      that.code = ''; //当前文件打开时的内容
      that.contextData = null; //左侧导航右键点击是产生的数据对象
    },
    /**
     * 切换选项卡
     * @param {Object} index
     */
    toggleEditor(index) {
      index = Number(index);
      this.code = this.editorList[index].oldCode; //设置文件打开时的代码
      this.editor = this.editorList[index].editor; //设置编辑器实例
    },
    handleTabRemove(index) {
      let that = this;
      // 关闭选项卡
      that.editorIndex[index].tab = false; // 关闭选项卡
    },
  },
};
</script>

<style lang="scss" scoped>
// 自定义方法缩小
>>> .diy-fullscreen {
  overflow: hidden;

  .ivu-modal {
    top: 0px;
    left: 0px;
    right: 0px;
    bottom: 0px;
    height: 100%;
    width: 100% !important;

    .ivu-modal-content {
      height: 100%;

      .ivu-modal-body {
        height: 100%;
      }
    }

    .ivu-tabs {
      .ivu-tabs-content-animated {
        height: 92%;
        background-color: #2f2f2f !important;
      }
    }

    .ivu-tabs-content {
      height: 100%;
    }

    .ivu-tabs {
      .ivu-tabs-tabpane {
        height: 92%;
      }
    }
  }
}
.diy-header {
  display: flex;
  align-items: center;
  justify-content: space-between;

  .diy-header-icon {
    margin-right: 30px;
    cursor: pointer;
  }

  .diy-header-icon:hover {
    opacity: 0.8;
  }
}
>>> .ivu-modal {
  top: 70px;
}

.ivu-modal-content {
  .ivu-modal-body {
    min-height: 632px;
    height: 80vh;
    overflow: hidden;
  }
}

.ivu-tabs {
  .ivu-tabs-content-animated {
    min-height: 560px;
    height: 73vh;
    margin-top: -1px;
  }

  .ivu-tabs-tabpane {
    min-height: 560px;
    margin-top: -1px;
  }
}

.ivu-tabs-nav .ivu-tabs-tab .ivu-icon {
  color: #f00;
}

>>> body .ivu-select-dropdown .ivu-dropdown-transfer {
  background: red !important;
}

// 导航栏右键样式 无效
.file-left /deep/ .ivu-select-dropdown.ivu-dropdown-transfer .ivu-dropdown-menu .ivu-dropdown-item:hover {
  background-color: #e5e5e5 !important;
}

// 选项卡头部
>>> .ivu-tabs.ivu-tabs-card > .ivu-tabs-bar .ivu-tabs-nav-container {
  background-color: #fff;
}
.demo-drawer-footer {
  width: 100%;
  position: absolute;
  bottom: 0;
  left: 0;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: right;
  background: #fff;
}
.file {
  position: relative;
  .save {
    position: absolute;
    left: 50%;
    bottom: -30px;
    z-index: 99;
  }
}
.file-box {
  height: 100%;
}
</style>
