<template>
  <el-container id="repositoryMainList">
    <el-main :style="{'height': mainHeight}"
             v-loading="dataLoading"
             element-loading-text="拼命加载中">
      <div style="width: 100%;height: 100%;overflow: auto;display: inline">
        <div class="recent"> 
          <el-tabs v-model="activeName" @tab-click="handleRecnetOrStore">
            <el-tab-pane label="最近编辑" name="recent"></el-tab-pane>
            <el-tab-pane label="收藏文件" name="store"></el-tab-pane>
          </el-tabs>
        </div>    
        <div :style="{'margin': '12px 10px 10px 10px;','display':recentDisplay}">
<!--          <div style="margin-bottom: 10px;margin-left: 10px;">-->
<!--            <label style="font-size: 18px;font-weight: 600;" class="text-general-color">{{defaultCategoryName}}</label>-->
<!--          </div>-->
          <!-- 最近 -->
          <div>
            <!--
            <div style="margin: 10px;">
              <label style="font-size: 13px;color: #979797;">最近编辑</label>
            </div>
            -->
            <div style="margin-left: 10px;">
              <el-table
                :data="recentData"
                style="width: 100%;"
                :row-style="{height:'46px'}"
                :cell-style="{padding:'0px'}"
                :show-header=false>
                <el-table-column
                  width="20px"
                />
                <el-table-column
                  prop="main"
                  label="名称"
                  class-name="row-repository-title"
                  min-width="300px">
                  <template slot-scope="scope">
                    <div v-if="!scope.row.folder" :style="{'background-color': scope.row.icon.color}" class="icon-div-repository">
                      <i class="awsui-iconfont icon-dynamic-repository" v-html="scope.row.icon.code"></i>
                    </div>
                    <div v-else class="icon-div-repository">
                      <i class="awsui-iconfont icon-dynamic-repository" :style="{color: scope.row.icon.color, 'font-size': '32px'}" v-html="scope.row.icon.code"></i>
                    </div>
                    <div class="div-repository-title">
                      <p class="text-general-color" style="cursor: pointer;" @click="positionRepositoy(scope.row.id, scope.row.versionId, scope.row.pathData)">
                        {{scope.row.name}}
                      </p>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="second"
                  label="修改日期"
                  width="350">
                  <template slot-scope="scope">
                    <div class="div-update-date">
                      <p>
                        {{scope.row.updateUser}} 于 {{scope.row.updateDate}}修改
                      </p>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="operate"
                  label="操作"
                  align="right"
                  width="100">
                  <template slot-scope="scope">
                    <div class="div-operate text-second-color">
                      <div v-if="scope.row.isFavorite" class="div-cancel-favorite">
                        <el-tooltip content="取消收藏" placement="bottom" :hide-after=2000>
                          <i class="iconfont" style="cursor: pointer;color: #FFB800;" @click="setFavorite('0', scope.row.versionId, scope.row.id)">&#xe618;</i>
                        </el-tooltip>
                      </div>
                      <div v-if="!scope.row.isFavorite" class="non-favorite-display div-favorite">
                        <el-tooltip content="收藏" placement="bottom" :hide-after=2000>
                          <i class="iconfont icon-operate" style="cursor: pointer;" @click="setFavorite('1', scope.row.versionId, scope.row.id)">&#xe630;</i>
                        </el-tooltip>
                      </div>
                    </div>
                    <div class="div-operate text-second-color" style="margin: 0 10px 0 15px;">
                      <div class="operate-icon-display">
                        <el-tooltip v-if="scope.row.folder && havingWritePerm" placement="bottom" :hide-after=2000>
                          <span slot="content">修改{{scope.row.methodName}}</span>
                          <i class="iconfont icon-operate" style="display: inline-block;cursor: pointer;" @click="openUpdateFolder(scope.row.id, scope.row.name, scope.row.desc, scope.row.methodId, scope.row.methodName)">&#xe8b5;</i>
                        </el-tooltip>
                        <el-tooltip v-if="!scope.row.folder" content="打开模型" placement="bottom" :hide-after=2000>
                          <i class="iconfont icon-operate" style="display: inline-block;cursor: pointer;" @click="openDesigner(scope.row.id)">&#xe8b5;</i>
                        </el-tooltip>
                      </div>
                    </div>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
        </div>
        <div :style="{'margin': '10px;','margin-top': '20px;','display':storeDisplay}">
          <!-- 收藏 -->
          <div>
            <!--
            <div style="margin: 10px;">
              <label style="font-size: 13px;color: #979797;">收藏文件</label>
            </div>
            -->
            <div style="margin-left: 10px;">
              <el-table
                :data="commonData"
                style="width: 100%;"
                :row-style="{height:'46px'}"
                :cell-style="{padding:'0px'}"
                empty-text="无收藏文件"
                :show-header=false>
                <el-table-column
                        width="20px"
                />
                <el-table-column
                  prop="main"
                  label="名称"
                  class-name="row-repository-title"
                  min-width="300px">
                  <template slot-scope="scope">
                    <div v-if="!scope.row.folder" :style="{'background-color': scope.row.icon.color}" class="icon-div-repository">
                      <i class="awsui-iconfont icon-dynamic-repository" v-html="scope.row.icon.code"></i>
                    </div>
                    <div v-else class="icon-div-repository">
                      <i class="awsui-iconfont icon-dynamic-repository" :style="{color: scope.row.icon.color, 'font-size': '32px'}" v-html="scope.row.icon.code"></i>
                    </div>
                    <div class="div-repository-title">
                      <p class="text-general-color" style="cursor: pointer;" @click="positionRepositoy(scope.row.id, scope.row.versionId, scope.row.pathData)">
                        {{scope.row.name}}
                      </p>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="second"
                  label="修改日期"
                  width="350">
                  <template slot-scope="scope">
                    <div class="div-update-date">
                      <p>
                        {{scope.row.updateUser}} 于 {{scope.row.updateDate}}修改
                      </p>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="operate"
                  label="操作"
                  align="right"
                  width="100">
                  <template slot-scope="scope">
                    <div class="div-operate text-second-color">
                      <div v-if="scope.row.isFavorite" class="div-cancel-favorite">
                        <el-tooltip content="取消收藏" placement="bottom" :hide-after=2000>
                          <i class="iconfont" style="cursor: pointer;color: #FFB800;" @click="setFavorite('0', scope.row.versionId, scope.row.id)">&#xe618;</i>
                        </el-tooltip>
                      </div>
                      <div v-if="!scope.row.isFavorite" class="non-favorite-display div-favorite">
                        <el-tooltip content="收藏" placement="bottom" :hide-after=2000>
                          <i class="iconfont icon-operate" style="cursor: pointer;" @click="setFavorite('1', scope.row.versionId, scope.row.id)">&#xe630;</i>
                        </el-tooltip>
                      </div>
                    </div>
                    <div class="div-operate text-second-color" style="margin: 0 10px 0 15px;">
                      <div class="operate-icon-display">
                        <el-tooltip v-if="scope.row.methodId=='default' && havingWritePerm" content="修改文件夹" placement="bottom" :hide-after=2000>
                          <i class="iconfont icon-operate" style="display: inline-block;cursor: pointer;" @click="openUpdateFolder(scope.row.id, scope.row.name, scope.row.desc, scope.row.methodId, scope.row.methodName)">&#xe8b5;</i>
                        </el-tooltip>
                        <el-tooltip v-if="scope.row.methodId !='default'" content="打开模型" placement="bottom" :hide-after=2000>
                          <i class="iconfont icon-operate" style="display: inline-block;cursor: pointer;" @click="openDesigner(scope.row.id)">&#xe8b5;</i>
                        </el-tooltip>
                      </div>
                    </div>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
        </div>
      </div>
    </el-main>
    <el-dialog
      title="修改文件夹"
      :visible.sync="folderDialog.dialogVisible"
      :modal-append-to-body=false
      :close-on-click-modal=false
      :close-on-press-escape=true
      :before-close="handleCloseFolder"
      width="600px">
      <div style="border: 1px solid #F2F2F2;padding: 0px 10px 10px 10px;">
        <awsui-form :model="folderDialog.folderForm" :rules="folderDialog.rules" ref="folderForm" label-position="top">
          <awsui-form-item label="名称" prop="name">
            <awsui-input v-model="folderDialog.folderForm.name"></awsui-input>
          </awsui-form-item>
          <awsui-form-item label="描述" prop="desc">
            <awsui-input type="textarea" v-model="folderDialog.folderForm.desc"></awsui-input>
          </awsui-form-item>
        </awsui-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <awsui-button class="button-general-color" type="primary" @click="updateFolderSave('folderForm')">确定</awsui-button>
        <awsui-button @click="closeFolderDlg">取消</awsui-button>
      </span>
    </el-dialog>
    <awsui-dialog
            :title="customFolderDialog.title"
            :visible.sync="customFolderDialog.dialogVisible"
            :modal-append-to-body=false
            :close-on-click-modal=false
            :close-on-press-escape=true
            :border="false"
            width="800px">
      <div>
        <repository-info-property v-if="customFolderDialog.dialogVisible" :id="customFolderDialog.id" :repositoryRefresh="repositoryRefresh"/>
      </div>
    </awsui-dialog>
  </el-container>
</template>

<script>
  import {openDesigner} from "../../api/commonFun";
  import awsuiAxios from "../../awsuiAxios";
  import RepositoryInfoProperty from "./RepositoryInfoProperty";

    export default {
      name: "RepositoryMainList",
      components: {RepositoryInfoProperty},
      props: {
        refreshTreeParentNode: {
          type: Function,
          default: null
        }
      },
      data(){
        return {
          mainHeight: (parseInt(this.$store.getters.getTopMainHeightFn)) + 'px',
          dataLoading: false,
          defaultCategoryName: '',
          recentData: [],
          commonData: [],
          havingWritePerm: false,
          folderDialog: {
            dialogVisible: false,
            folderForm: {
              id: '',
              name: '',
              desc: ''
            },
            rules: {
              name: [
                { required: true, message: '请输入名称', trigger: 'blur' },
                { min: 1, max: 120, message: '长度在 1 到 120 个字符', trigger: 'blur' }
              ]
            }
          },
          customFolderDialog: {// 扩展的文件夹类建模方法
            dialogVisible: false,
            title: '',
            id: ''
          },
          activeName:"recent",
          recentDisplay: "block",
          storeDisplay: "none",
        }
      },
      created() {
        this.initData();
      },
      methods: {
        handleRecnetOrStore(tab, event){
          this.switchTabCard(tab.name);
        },
        switchTabCard(type){
          if(type == 'recent'){
            this.recentDisplay = "block";
            this.storeDisplay = "none"
          }else if(type == 'store'){
            this.recentDisplay = "none";
            this.storeDisplay = "block"
          }
        },
        handleCloseFolder(done) {
          this.folderDialog.dialogVisible = false;
          done();
        },
        openUpdateFolder(id, name, desc, methodId, methodName) {
          if (methodId == 'default') {// 默认文件夹
            this.folderDialog.folderForm.name = name;
            this.folderDialog.folderForm.desc = desc;
            this.folderDialog.folderForm.id = id;
            this.folderDialog.dialogVisible = true;
          } else {// 扩展的文件夹类型的建模方法
            this.customFolderDialog.id = id;
            this.customFolderDialog.title = '修改' + methodName;
            this.customFolderDialog.dialogVisible = true;
          }
        },
        closeFolderDlg() {
          this.folderDialog.dialogVisible = false;
          this.folderDialog.folderForm.name = '';
          this.folderDialog.folderForm.desc = '';
          this.folderDialog.folderForm.id = '';
        },
        updateFolderSave(formName) {
          const that = this;
          that.$refs[formName].validate((valid) => {
            if (valid) {
              const name = that.folderDialog.folderForm.name;
              const desc = that.folderDialog.folderForm.desc;
              if (desc.length > 255) {
                that.$message({message: '[描述]不允许超过255个字符',type: 'warning'});
                return;
              }
              const id = that.folderDialog.folderForm.id;
              const wsId = that.$store.getters.getWsIdFn;
              const teamId = that.$store.getters.getTeamIdFn;
              const method = 'default';// 文件夹
              const data = {
                url:'jd',
                data:{
                  cmd: 'com.actionsoft.apps.coe.pal_processlevel_folder_create_save',
                  wsId: wsId,
                  teamId: teamId,
                  method: method,
                  name: name,
                  desc: desc,
                  id: id
                }
              };
              // 查询数据
              awsuiAxios.post(data).then(function (ro) {
                if(ro.result == 'ok') {
                  that.initData();
                  if (that.refreshTreeParentNode) {
                    that.refreshTreeParentNode(id);
                  }
                  that.closeFolderDlg();
                }
              }).catch(error=>{
                console.log(error);
              })
            } else {
              console.log('error submit!!');
              return false;
            }
          });
        },
        initData() {// 初始化
          const that = this;
          that.dataLoading = true;
          const data = {
            url:'jd',
            data:{
              cmd: 'com.actionsoft.apps.coe.pal_processlevel_recent_and_favorite_data_query',
              wsId: that.$store.getters.getWsIdFn,
              teamId: that.$store.getters.getTeamIdFn,
            }
          };
          // 查询数据
          awsuiAxios.post(data).then(function (ro) {
            if(ro.result == 'ok') {
              that.defaultCategoryName = ro.data.defaultCategoryName;
              that.recentData = ro.data.recentData;
              that.commonData = ro.data.commonData;
              that.havingWritePerm = ro.data.havingWritePerm;
              that.dataLoading = false;
            }
          }).catch(error=>{
            console.log(error);
            that.dataLoading = false;
          })
        },
        openDesigner(id) {
          openDesigner(this.$store.getters.getTeamIdFn, id, this.$store.state.sessionId);
        },
        setFavorite(state, versionId, id) {
          if (state == '0') {// 取消收藏
            const that = this;
            const data = {
              url:'jd',
              data:{
                cmd: 'com.actionsoft.apps.coe.pal_processlevel_favorite_cancel',
                wsId: that.$store.getters.getWsIdFn,
                teamId: that.$store.getters.getTeamIdFn,
                versionId: versionId
              }
            };
            // 查询数据
            awsuiAxios.post(data).then(function (ro) {
              if(ro.result == 'ok') {
                that.initData();
              }
            }).catch(error=>{
              console.log(error);
            })
          } else {// 收藏
            const that = this;
            const data = {
              url:'jd',
              data:{
                cmd: 'com.actionsoft.apps.coe.pal_processlevel_favorite_save',
                wsId: that.$store.getters.getWsIdFn,
                teamId: that.$store.getters.getTeamIdFn,
                versionId: versionId
              }
            };
            // 查询数据
            awsuiAxios.post(data).then(function (ro) {
              if(ro.result == 'ok') {
                that.initData();
              }
            }).catch(error=>{
              console.log(error);
            })
          }
        },
        repositoryRefresh(id) {// 刷新父页面树
          if (this.refreshTreeParentNode) {
            this.refreshTreeParentNode(id);
          }
          this.initData();
        },
        positionRepositoy(id, versionId, pathData) {// 定位树结构
          const arr = [];
          for (let i = 0; i < pathData.length; i++) {
            const tempId = pathData[i].versionId;
            arr.push(tempId);
          }
          this.$router.push({path: '/Repository', query: {id: id, versionId: versionId, path: arr.join(','), param: Math.random()}});
        }
      },
      computed: {
        listenTopMainHeight() {
          return this.$store.getters.getTopMainHeightFn;
        }
      },
      watch : {
        listenTopMainHeight: function (newd, old) {
          this.mainHeight = (parseInt(newd)) + 'px';
        }
      }
    }
</script>

<style scoped>
  .icon-div-repository {
    position: absolute;
    border-radius: 10%;
    left: 0px;
    display: inline-block;
    width: 32px;
    height: 32px;
    text-align: center;
    line-height: 32px;
    vertical-align: middle;
  }
  .icon-dynamic-repository {
    color: white;
    font-size: 18px;
  }
  .div-repository-title {
    display: inline-block;
    position: relative;
    left: 32px;
    text-align: center;
    height: 32px;
    line-height: 32px;
    font-size: 13px;
  }
  .div-repository-title :hover {
    color: #4E7FF9;
  }
  .div-cancel-favorite {
    display: inline-block;
    /*left: 30px;*/
  }
  .div-favorite {
    /*left: 30px;*/
  }
  .div-update-date {
    display: inline-block;
    position: relative;
    left: 30px;
    text-align: center;
    height: 30px;
    line-height: 30px;
    font-size: 12px;
    color: #92A2B2;
  }
  #repositoryMainList >>> .el-dialog__body {
    padding: 10px 20px;
    color: #606266;
    font-size: 14px;
    word-break: break-all;
  }
  #repositoryMainList >>> .el-form-item__label {
    line-height: 0;
  }
  #repositoryMainList >>> .el-table td, .el-table th {
    padding: 8px 0;
  }
  #repositoryMainList >>> .el-table td {
    border-bottom: 1px solid #F2F2F2;
  }
  #repositoryMainList >>> .el-table__row .non-favorite-display{
    display: none;
  }
  #repositoryMainList >>> .el-table__row:hover .non-favorite-display{
    display: inline-block;
  }
  #repositoryMainList >>> .el-table__row .operate-icon-display{
    display: none;
  }
  #repositoryMainList >>> .el-table__row:hover .operate-icon-display{
    display: inline-block;
  }
  .div-operate {
    display: inline-block;
    width: 16px;
    position: relative;
    top: 2px;
  }
  .icon-operate:hover {
    color: #4E7FF9 !important;
  }
  #repositoryMainList >>> .row-repository-title .cell {/*IE错位问题*/
    position: relative;
  }
  .recent{
    margin-left: 1rem;
    margin-top: 0.6rem;
  }
  #repositoryMainList >>> .el-tabs__nav-wrap:after{
    background-color: #ffffff !important;
  }
 #repositoryMainList >>>  .el-table:before{
   background-color: #ffffff !important;
 }
 #repositoryMainList >>> .el-tabs__item.is-active{
   color: #4E7FF9;
 }
 #repositoryMainList >>> .el-tabs__item:hover{
   color: #4E7FF9;
 }
  #repositoryMainList >>> .el-tabs__active-bar {
    background-color: #4E7FF9;
  }
</style>
