<template>
  <awsui-layout id="repMark">
    <awsui-aside width="50%">
      <div class="treeWrapper">
        <div class="treeHeader" :style="{ height: headerHeight }">
          <div class="textWrapper">
            <span class="title">文件密级</span>
            <span class="desc" v-if="fileUnMarkNum !=-1">共有<span style="color: red">{{fileUnMarkNum}}</span>个待标密文件</span
            >
          </div>
          <div class="button">
            <awsui-button
              style="height: 36px; margin: 0; font-size: 14px"
              class="button-general-color"
              type="primary"
              @click="quickMark"
              >快速定位
            </awsui-button>
          </div>
        </div>
        <div
          class="treeMain"
          :style="{ 'padding-left': '2rem', height: mainHeight }"
        >
          <el-tree
            ref="tree"
            :props="treeProps"
            :expand-on-click-node="false"
            :highlight-current="true"
            @node-click="openNode"
            @node-expand="expandNode"
            @node-collapse="closeNode"
            node-key="id"
            lazy
            :load="loadNode"
          >
            <span slot-scope="{ node, data }">
              <i
                class="awsui-iconfont tree-content-icon tree-content-icon-padding"
                :style="{ color: node.data.icon.color }"
                v-html="node.data.icon.icon"
              ></i>
              <span
                :style="{ 'font-weight': data.id.length < 36 ? '600' : '' }"
              >
                {{ node.label }}
                <span
                  v-if="data.id.length >= 26 &&!data.folder && data.securityLevelName != '未标密' "
                  class="font12 blue"
                >
                  {{ data.securityLevelName }}
                </span>
                <span
                  v-if=" data.id.length >= 26 && !data.folder && data.securityLevelName == '未标密' "
                  class="font12 red"
                  @click="showMarkDialog(data, -1, 'file')"
                >
                  {{ data.securityLevelName }}
                </span>
                <span
                  v-if="
                    data.id.length >= 26 && !data.folder && data.isUpfileUnmark
                  "
                  class="font12 red"
                >
                  附件未标密
                </span>
              </span>
            </span>
          </el-tree>
        </div>
      </div>
    </awsui-aside>
    <awsui-main>
      <div class="markWrapper">
        <div class="treeHeader">
          <div class="textWrapper">
            <span class="title">附件密级</span>
            <span class="desc">
              共有
              <span class="red">{{ upFileUnMark.length }}</span>
              个待标密文件
            </span>
          </div>
          <div class="button">
            <awsui-button
              style="height: 36px; margin: 0; font-size: 14px"
              class="button-general-color"
              type="primary"
              @click="showMark"
            >
              快速定位
            </awsui-button>
          </div>
        </div>
        <div class="treeMain noData" v-if="upFileTable.length <= 0 && shapeTable.length <= 0">
          <div class="wrapper">
            <div style="text-align: center">
              <span>
                <i class="iconfont" style="color: #c2c2c2; font-size: 60px">
                  &#xe65e;
                </i>
              </span>
              <p class="text-general-color" style="margin: 15px 0">
                暂无未标定密级文件
              </p>
            </div>
          </div>
        </div>
        <div v-else class="fileWrapper">
          <!--附件密级显示-->
          <div v-if="upFileTable.length > 0" >
            <div class="title font16">文件密级</div>
            <div class="fileList">
              <ul>
                <li
                  v-for="(file, index) in upFileTable"
                  :key="file.id"
                  :style="{'cursor': file.securityLevel === -1 ? 'pointer' : 'default'}"
                  @click="showMarkDialog(file, index,'upfile')"
                >
                  <span>
                    <i class="awsui-iconfont"> &#xe705; </i>
                    <span>{{ file.fileName }}</span>
                    <span
                      v-if="file.securityLevel === -1"
                      class="font12 red ml12"
                    >
                      未标密
                    </span>
                    <span v-else class="font12 blue ml12">{{
                      file.securityLevel
                    }}</span>
                  </span>
                </li>
              </ul>
            </div>
          </div>
          <!--图形密级显示-->
          <div class="mt1rem" v-if="shapeTable.length > 0">
            <div class="title font16">形状密级</div>
            <div class="fileList">
              <ul>
                <li
                  v-for="(file, index) in shapeTable"
                  :key="file.id"
                  @click="showMarkDialog(file, index,'shape')"
                >
                  <span>
                    <i class="awsui-iconfont"> &#xe705; </i>
                    <span>{{ file.fileName }}</span>
                    <span
                      v-if="file.securityLevel === -1"
                      class="font12 red ml12"
                    >
                      未标密
                    </span>
                    <span v-else class="font12 blue ml12">{{
                      file.securityLevel
                    }}</span>
                  </span>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </awsui-main>
    <awsui-dialog
      title="密级标定"
      :visible.sync="securityVisible"
      :border="false"
      append-to-body
      width="500px"
    >
      <div style="max-height:500px;overflow-y: auto">
        <awsui-form
          ref="securityDialog"
          label-width="200px"
          id="securityDialog"
          :rules="securityRules"
          :model="securityForm"
        >
          <awsui-form-item :label="securityForm.name" prop="securityLevel">
            <awsui-select
              v-model="securityForm.securityLevel"
              :options="securityOptions"
              style="width: 70%"
            ></awsui-select>
          </awsui-form-item>
        </awsui-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <awsui-button type="primary" @click="securityMark">确定</awsui-button>
        <awsui-button @click="securityVisible = false">取 消</awsui-button>
      </div>
    </awsui-dialog>
  </awsui-layout>
</template>
<script>
import awsuiAxios from "../../awsuiAxios";
export default {
  name: "RepositoryMark",
  data() {
    var securityValidate = (rule, value, callback) => {
      if (value === undefined) {
        callback(new Error("请选择文件密级"));
      } else {
        callback();
      }
    };
    return {
      treeProps: {
        value: "id",
        label: "name",
        isLeaf: "leaf",
      },
      headerHeight: "30px",
      securityVisible: false,
      securityRules: {
        securityLevel: [
          {
            required: true,
            trigger: "change",
            validator: securityValidate,
            type: "number",
          },
        ],
      },
      securityForm: {},
      securityOptions: [],
      securityList: {},
      upFileTable: [],
      shapeTable: [],
      fileUnMarkNum: -1,
      upFileUnMark: [],
      pl_uuid: "",
      resolveFun: [],
      path: [],
      pid: "",
    };
  },
  computed: {
    mainHeight() {
      return (
        parseInt(this.$store.getters.getTopMainHeightFn) -
        parseInt(this.headerHeight) +
        "px"
      );
    },
  },
  watch: {
    //密级选择dialogVisible 变为false，清空securityFileList
    securityVisible(newval, oldval) {
      if (!newval) {
        this.securityForm = {};
      }
    },
  },
  created() {},
  mounted() {},
  methods: {
    loadUpfile(uuid) {
      this.pl_uuid = uuid;
      const that = this;
      //获取node下属全部附件
      const data = {
        url: "jd",
        data: {
          cmd: "com.actionsoft.apps.coe.pal_processlevel_upfile_load_all",
          pl_uuid: uuid,
          type: "file",
          wsId: that.$store.getters.getWsIdFn,
          teamId: that.$store.getters.getTeamIdFn,
        },
      };
      // 查询数据
      awsuiAxios
        .post(data)
        .then(function (ro) {
          if (ro.result == "ok") {
            // 清空未标密table
            that.upFileUnMark = [];
            //设置密级options
            that.securityList = ro.data.securityList;
            that.securityOptions = [];
            Object.keys(that.securityList).map((key) => {
              let option = {
                value: key,
                label: that.securityList[key],
              };
              that.securityOptions.push(option);
            });
            
            // 筛选显示字段
            const list = ro.data.upfileList;
            let fileTable = [];
            for (let i = 0; i < list.length; i++) {
              const curr = list[i];
              if (curr.securityLevel == -1) {
                that.upFileUnMark.push(curr);
              }
              const tableRow = {
                id: curr.uuid,
                fileName: curr.fileName,
                securityLevel:
                  curr.securityLevel == -1
                    ? -1
                    : ro.data.securityList[curr.securityLevel],
              };
              fileTable.push(tableRow);
            }
            that.upFileTable = fileTable;
            //---图形附件
            let shapeList = ro.data.shapeList;
            fileTable = [];
            for (let i = 0; i < shapeList.length; i++) {
              let curr = shapeList[i];
              if (curr.securityLevel == -1) {
                that.upFileUnMark.push(curr);
              }
              const tableRow = {
                id: curr.uuid,
                fileName: curr.fileName,
                securityLevel:
                  curr.securityLevel == -1
                    ? -1
                    : ro.data.securityList[curr.securityLevel],
              };
              fileTable.push(tableRow);
            }
            that.shapeTable = fileTable;

            //回显附件未标密
            if (that.upFileUnMark.length == 0) {
              const tree = that.$refs.tree;
              let node = tree.getNode(that.pl_uuid);
              node.data.isUpfileUnmark = false;
            }
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    quickMark() {
      const that = this;
      const data = {
        url: "jd",
        data: {
          cmd: "com.actionsoft.apps.coe.pal_processlevel_upfile_unmark_path",
          wsId: that.$store.getters.getWsIdFn,
          teamId: that.$store.getters.getTeamIdFn,
        },
      };
      // 查询数据
      awsuiAxios
        .post(data)
        .then(function (ro) {
          if (ro.result == "ok") {
            that.path = [];
            that.pid = "";
            if (ro.data.path.length == 0) {
              that.$message({
                message: "已全部标定密级",
                type: "success",
              });
            } else {
              that.path = ro.data.path;
              for (let i = 0; i < that.path.length; i++) {
                const tree = that.$refs.tree;
                const node = tree.getNode(that.path[i]);
                if (node != null) {
                  node.expand();
                  let index = that.path.indexOf(that.path[i]);
                  if (index != -1) {
                    that.pid = that.path[index];
                    that.path.splice(index, 1);
                    i--;
                  }
                  if (that.path.length == 0) {
                    tree.setCurrentKey(that.pid);
                    that.loadUpfile(that.pid);
                    //文件标密dialog
                    if(that.fileUnMarkNum != -1){
                      that.showMarkDialog(node.data,-1,"file");
                    }
                  }
                }
              }
              const tree = that.$refs.tree;
              const node = tree.getNode(that.pid);
              node.expand();
            }
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    showMark() {
      if (this.upFileUnMark.length > 0) {
        let node = this.upFileUnMark[0];
        let list = [];
        if (node.type === "f") {
          list = this.upFileTable;
        } else {
          list = this.shapeTable;
        }
        for (let i = 0; i < list.length; i++) {
          if (node.uuid === list[i].id) {
            if (node.type === "f") {
              this.showMarkDialog(node, i, "upfile");
            } else {
              this.showMarkDialog(node, i, "shape");
            }
            break;
          }
        }
      } else {
        this.$message({
          message: "暂无可标定密级文件",
          type: "success",
        });
      }
    },
    showMarkDialog(node, index,type) {
      // 开启密级标定对话框
      if (node.securityLevel === -1) {
        this.securityVisible = true;
        this.securityForm.index = index;
        this.securityForm.name = node.fileName == undefined ? node.name : node.fileName;
        this.securityForm.type = type;
        this.securityForm.node = node;
      }
    },
    securityMark() {
      const that = this;
      let node = this.securityForm.node;
      this.$refs.securityDialog.validate((valid) => {
        if (valid) {
          if (this.securityForm.type == "file") {
            //流程文件更改密级
            const data = {
              url: "jd",
              data: {
                cmd: "com.actionsoft.apps.coe.pal_pl_file_security_level_update",
                uuid: node.id,
                securityLevel: that.securityForm.securityLevel,
              },
            };
            awsuiAxios
              .post(data)
              .then(function (ro) {
                that.securityVisible = false;
                if (ro.result == "ok") {
                  //重新加载附件
                  that.loadUpfile(that.pl_uuid);

                  //更新流程文件密级
                  let name = that.securityList[that.securityForm.securityLevel];
                  let node = that.$refs.tree.getNode(that.pl_uuid);
                  node.data.securityLevelName = name;

                  //更新未标密数量
                  that.fileUnMarkNum--;

                  that.$message({
                    message: "密级标定成功",
                    type: "success",
                  });
                }
              })
              .catch((error) => {
                console.log(error);
                that.securityVisible = false;
                that.$message.error("密级标定失败");
              });
          } else {
            //验证通过，附件更改密级
            const data = {
              url: "jd",
              data: {
                cmd: "com.actionsoft.apps.coe.pal_processlevel_upfile_security_level_update",
                uuid: node.id == undefined ? node.uuid : node.id,
                securityLevel: that.securityForm.securityLevel,
              },
            };
            awsuiAxios
              .post(data)
              .then(function (ro) {
                that.securityVisible = false;
                if (ro.result == "ok") {
                  that.loadUpfile(that.pl_uuid);
                  that.$message({
                    message: "密级标定成功",
                    type: "success",
                  });
                }
              })
              .catch((error) => {
                console.log(error);
                that.securityVisible = false;
                that.$message.error("密级标定失败");
              });
          }
        }
      });
    },
    openNode(obj, node, tree) {
      this.loadUpfile(node.data.id);
    },
    expandNode(obj, node, tree) {
      // 展开节点
    },
    closeNode(obj, node, tree) {
      // 关闭时清空，下次展开重新请求动态加载
    },
    loadNode(node, resolve) {
      const that = this;
      const data = {
        url: "jd",
        data: {},
      };
      data.data.wsId = that.$store.getters.getWsIdFn;
      data.data.teamId = that.$store.getters.getTeamIdFn;
      data.data.cmd = "com.actionsoft.apps.coe.pal_processlevel_tree_data";

      if (node.level === 0) {
        // 获取根目录
        data.data.pid = "";
      } else {
        // 获取其他目录
        data.data.pid = node.data.id;
      }
      // 查询数据
      awsuiAxios
        .post(data)
        .then(function (ro) {
          if (ro.result == "ok") {
            resolve(ro.data);
            // 快速定位
            that.$nextTick(() => {
              if (that.path.length > 0) {
                that.pid = that.path[0];
                const tree = that.$refs.tree;
                const curnode = tree.getNode(that.path[0]);
                if (curnode != null) {
                  curnode.expand();
                  that.path.splice(0, 1);
                }
                if (that.path.length == 0) {
                  tree.setCurrentKey(that.pid);
                  that.loadUpfile(that.pid);
                  if(that.fileUnMarkNum != -1){
                    //文件标密dialog
                    that.showMarkDialog(curnode.data,-1,"file");
                  }
                }
              }
            });
            //  默认展开tree层级
            if (node.level == 0 && ro.data.length > 0) {
              const tree = that.$refs.tree;
              tree.getNode(ro.data[0].id).expand();
              setTimeout(function () {
                const childNode = tree.getNode(ro.data[0].id).childNodes[0];
                if (childNode != null) {
                  childNode.expand();
                }
              }, 500);
            }
            //流程文件未标密数量
            if (ro.data.length > 0) {
              let node = ro.data[0];
              if(node.isFileSecurity != undefined && node.isFileSecurity && that.fileUnMarkNum == -1){
                that.getUnmarkFileNum(data.data.wsId,data.data.teamId);
              }
            }
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    getUnmarkFileNum(wsId, teamId) {
      let that = this;
      const data = {
        url: "jd",
        data: {
          cmd: "com.actionsoft.apps.coe.pal_pl_file_unmark_num_query",
          wsId: wsId,
          teamId: teamId,
        },
      };
      awsuiAxios
        .post(data)
        .then(function (ro) {
          if (ro.result == "ok") {
            that.fileUnMarkNum = ro.data.unmarkFileNum;
          }
        })
        .catch((error) => {
          console.log(error);
          that.securityVisible = false;
          that.$message.error("获取未标密文件失败");
        });
    },
  },
};
</script>
<style scoped>
.awsui-main {
  padding: 0;
}
.treeWrapper {
  padding-right: 2rem;
  border-right: 3px dashed gray;
}
.markWrapper {
  padding-right: 2rem;
  height: 100%;
}
.treeHeader {
  padding-top: 1rem;
  padding-left: 2rem;
  padding-bottom: 1rem;
}
.treeMain {
  overflow-y: auto;
}
.treeMain::-webkit-scrollbar {
  display: none;
}
.treeHeader .textWrapper {
  display: inline;
}
.treeHeader .title {
  font-size: 1.2rem;
  margin-right: 1rem;
}
.treeHeader .desc {
  margin-left: 1rem;
}
.treeHeader .button {
  display: inline;
  float: right;
}
.noData {
  width: 100%;
  height: 90%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.noData .wrapper {
  width: 300px;
  margin: auto;
}
.fileWrapper {
  padding-left: 2rem;
}
.fileWrapper .fileList {
  padding-left: 1rem;
}
.fileWrapper .fileList li {
  list-style: none;
  margin-top: 1rem;
  cursor: pointer;
}

.font12 {
  font-size: 12px;
}
.font16 {
  font-size: 16px;
}
.blue {
  color: #4e7ff9;
}
.red {
  color: #f56c6c;
}
.ml12 {
  margin-left: 12px;
}
.noView {
  display: none;
}
.mt1rem {
  margin-top: 1rem;
}
</style>