import editView from "../fileObjAuthEdit";
import userDialog from "@base/views/user/user/dialog";
import positionDialog from "@base/views/user/position/dialog";
import saleOrgDialog from "@base/views/user/sale_org/dialog";
import roleDialog from "@base/views/user/role/dialog";
import fileObjAuthEditDialog from "../fileObjAuthEditDialog.vue";
import fileObjNotifyEdit from "../fileObjNotifyEdit.vue";

let modules = {};
modules = {
  name: "fileObjAuthDialog",
  props: {
    fileObjId: Number,
    treeNodeArr: Array,
  },
  components: {
    editView,
    userDialog,
    positionDialog,
    saleOrgDialog,
    roleDialog,
    fileObjAuthEditDialog,
    fileObjNotifyEdit,
  },
  inject: ["current_prefix", "getFileStoreArea"],
  mounted() {
    this.initTableList();
  },
  data() {
    let that = this;
    return {
      showDialog: true,
      activeName: "first",
      value10: "",
      dataId: "",
      showEdit: false,
      formData: {
        saleOrgName: null,
        positionName: null,
        nickName: null,
      },
      vxeOption: {},
      showUserDialog: false,
      showPositionDialog: false,
      showSaleOrgDialog: false,
      operateIndex: 0,
      showViewDialog: false,

      tableData: [],
      showUserRowDialog: false,
      showSaleOrgRowDialog: false,
      showPositionRowDialog: false,
      showRoleRowDialog: false,

      showUserAddDialog: false,
      showSaleOrgAddDialog: false,
      showPositionAddDialog: false,
      showRoleAddDialog: false,
      showDetailDialog: false,
      currentRow: null,
      showFileObjNotifyEdit: true,
    };
  },
  computed: {
    fileStoreArea() {
      return this.getFileStoreArea();
    },
  },
  methods: {
    searchEvent() {
      this.$refs["table-m1"].commitProxy("reload");
    },
    resetEvent() {
      this.formData = {};
      this.$refs["table-m1"].commitProxy("reload");
    },
    openEditDialog(id) {
      this.dataId = !id || typeof id == "object" ? 0 : id;
      this.activeName = "first";
      this.showViewDialog = true;
      this.$openEditView("showEdit");
    },

    initTableList() {
      let that = this;
      let valMap = {
        0: this.$t2("否", "components.fileLibrary.auThLaebl0"),
        1: this.$t2("是", "components.fileLibrary.auThLaebl1"),
      };
      let tableOption = {
        vue: this,
        tableRef: "table-m1",
        tableName: "user_fileLibrary-fileAuto-list-m1",
        /*path: this.current_prefix + '/file_obj_auth/listPage',
        param: () => {
          return {
            fileObjId: this.fileObjId,
            ...this.formData
          };
        },*/
        config: {
          height: "auto",
          rowStyle: ({ row, rowIndex }) => {
            return row._isParent ? "background-color: #f3f3f3;" : "";
          },
        },
        columns: [
          { type: "checkbox", width: 48, resizable: false, fixed: "left" },
          {
            title: this.$t2("机构", "components.fileLibrary.saleOrg"),
            field: "saleOrgName",
            width: 150,
            slots: {
              default: "saleOrg",
            },
          },
          {
            title: this.$t2("岗位", "components.fileLibrary.position"),
            field: "positionName",
            width: 150,
            slots: {
              default: "position",
            },
          },
          {
            title: this.$t2("用户", "components.fileLibrary.user"),
            field: "nickName",
            width: 150,
            slots: {
              default: "user",
            },
          },
          {
            title: this.$t2("角色", "components.fileLibrary.role"),
            field: "roleName",
            width: 150,
            slots: {
              default: "role",
            },
          },
          {
            title: this.$t2("新增", "components.fileLibrary.add"),
            field: "addAuth",
            width: 150,
            slots: {
              default: ({ row }) => {
                return [
                  <el-switch
                    v-model={row.addAuth}
                    active-value={1}
                    inactive-value={0}
                    disabled={row._isParent}
                  ></el-switch>,
                ];
              },
            },
          },
          {
            title: this.$t2("编辑", "components.fileLibrary.edit"),
            field: "editAuth",
            width: 150,
            slots: {
              default: ({ row }) => {
                return [
                  <el-switch
                    v-model={row.editAuth}
                    active-value={1}
                    inactive-value={0}
                    disabled={row._isParent}
                  ></el-switch>,
                ];
              },
            },
          },
          {
            title: this.$t2("查看", "components.fileLibrary.view"),
            field: "viewAuth",
            width: 150,
            slots: {
              default: ({ row }) => {
                return [
                  <el-switch
                    v-model={row.viewAuth}
                    active-value={1}
                    inactive-value={0}
                    disabled={row._isParent}
                  ></el-switch>,
                ];
              },
            },
          },
          {
            title: this.$t2("上传", "components.fileLibrary.upload"),
            field: "uploadAuth",
            width: 150,
            slots: {
              default: ({ row }) => {
                return [
                  <el-switch
                    v-model={row.uploadAuth}
                    active-value={1}
                    inactive-value={0}
                    disabled={row._isParent}
                  ></el-switch>,
                ];
              },
            },
          },
          {
            title: this.$t2("下载", "components.fileLibrary.download"),
            field: "downloadAuth",
            width: 150,
            slots: {
              default: ({ row }) => {
                return [
                  <el-switch
                    v-model={row.downloadAuth}
                    active-value={1}
                    inactive-value={0}
                    disabled={row._isParent}
                  ></el-switch>,
                ];
              },
            },
          },
          {
            title: this.$t2("移动", "components.fileLibrary.move"),
            field: "moveAuth",
            width: 150,
            slots: {
              default: ({ row }) => {
                return [
                  <el-switch
                    v-model={row.moveAuth}
                    active-value={1}
                    inactive-value={0}
                    disabled={row._isParent}
                  ></el-switch>,
                ];
              },
            },
          },
          {
            title: this.$t2("复制", "components.fileLibrary.copy"),
            field: "copyAuth",
            width: 150,
            slots: {
              default: ({ row }) => {
                return [
                  <el-switch
                    v-model={row.copyAuth}
                    active-value={1}
                    inactive-value={0}
                    disabled={row._isParent}
                  ></el-switch>,
                ];
              },
            },
          },
          {
            title: this.$t2("替换", "components.fileLibrary.replace"),
            field: "replaceAuth",
            width: 150,
            slots: {
              default: ({ row }) => {
                return [
                  <el-switch
                    v-model={row.replaceAuth}
                    active-value={1}
                    inactive-value={0}
                    disabled={row._isParent}
                  ></el-switch>,
                ];
              },
            },
          },
          {
            title: this.$t2("删除文件", "components.fileLibrary.deleteFile"),
            field: "deleteAuth",
            width: 150,
            slots: {
              default: ({ row }) => {
                return [
                  <el-switch
                    v-model={row.deleteAuth}
                    active-value={1}
                    inactive-value={0}
                    disabled={row._isParent}
                  ></el-switch>,
                ];
              },
            },
          },
          {
            title: this.$t2(
              "删除文件夹",
              "components.fileLibrary.deleteCategory"
            ),
            field: "deleteDirAuth",
            width: 150,
            slots: {
              default: ({ row }) => {
                return [
                  <el-switch
                    v-model={row.deleteDirAuth}
                    active-value={1}
                    inactive-value={0}
                    disabled={row._isParent}
                  ></el-switch>,
                ];
              },
            },
          },
          {
            title: this.$t2("查看历史", "components.fileLibrary.checkHistory"),
            field: "historyAuth",
            width: 150,
            slots: {
              default: ({ row }) => {
                return [
                  <el-switch
                    v-model={row.historyAuth}
                    active-value={1}
                    inactive-value={0}
                    disabled={row._isParent}
                  ></el-switch>,
                ];
              },
            },
          },
          {
            title: this.$t2("分享", "components.fileLibrary.share"),
            field: "shareAuth",
            width: 150,
            slots: {
              default: ({ row }) => {
                return [
                  <el-switch
                    v-model={row.shareAuth}
                    active-value={1}
                    inactive-value={0}
                    disabled={row._isParent}
                  ></el-switch>,
                ];
              },
            },
          },
          ...(this.fileStoreArea.toDownWatermark
            ? [
                {
                  title: this.$t1("下载打水印 "),
                  field: "downWatermarkAuth",
                  width: 150,
                  slots: {
                    default: ({ row }) => {
                      return [
                        <el-switch
                          v-model={row.downWatermarkAuth}
                          active-value={1}
                          inactive-value={0}
                          disabled={row._isParent}
                        ></el-switch>,
                      ];
                    },
                  },
                },
              ]
            : []),

          {
            title: this.$t1("设置权限"),
            field: "authAuth",
            width: 150,
            slots: {
              default: ({ row }) => {
                return [
                  <el-switch
                    v-model={row.authAuth}
                    active-value={1}
                    inactive-value={0}
                    disabled={row._isParent}
                  ></el-switch>,
                ];
              },
            },
          },
          {
            title: this.$t2("创建人", "system.label.createBy"),
            field: "_createBy",
            width: 150,
          },
          {
            title: this.$t2("创建时间", "system.label.createDate"),
            field: "createDate",
            width: 150,
          },
          {
            title: this.$t2("更新人", "system.label.modifyBy"),
            field: "_modifyBy",
            width: 150,
          },
          {
            title: this.$t2("更新时间", "system.label.modifyDate"),
            field: "modifyDate",
            width: 150,
          },
          {
            width: 100,
            fixed: "right",
            title: "",
            sortable: false,
            slots: {
              default: ({ row, rowIndex, $table }) => {
                return [
                  <a
                    href="javascript:void(0);"
                    class="a-link"
                    onClick={() => {
                      this.openDetailDialog(row, rowIndex);
                    }}
                  >
                    <el-tooltip
                      enterable={false}
                      effect="dark"
                      content="查看"
                      placement="top"
                      popper-class="tooltip-skin"
                    >
                      <i class="el-icon-edit" />
                    </el-tooltip>
                  </a>,
                  <a
                    href="javascript:void(0);"
                    class="a-link"
                    onClick={() => {
                      this.tableData.splice(rowIndex, 1);
                    }}
                    v-show={!row._isParent}
                  >
                    <el-tooltip
                      enterable={false}
                      effect="dark"
                      content="删除"
                      placement="top"
                      popper-class="tooltip-skin"
                    >
                      <i class="el-icon-delete" />
                    </el-tooltip>
                  </a>,
                ];
              },
            },
          },
        ],
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then((opts) => {
        this.vxeOption = opts;
      });
      this.initData();
    },
    initData() {
      let tableData = [];
      this.$http({
        url: this.current_prefix + "/file_obj_auth/listParentPage",
        method: `post`,
        data: {
          fileObjId: this.fileObjId,
        },
        isLoading: true,
        modalStrictly: true,
        success: (res1) => {
          tableData = res1.objx?.records || [];
          tableData.forEach((item) => {
            item._isParent = true;
          });

          this.$http({
            url: this.current_prefix + "/file_obj_auth/listPage",
            method: `post`,
            data: {
              fileObjId: this.fileObjId,
            },
            isLoading: true,
            modalStrictly: true,
            success: (res2) => {
              tableData.push(...(res2.objx?.records || []));
              this.tableData = tableData;
            },
          });
        },
      });
    },
    confirmInsertUser(rows) {
      if (rows.length) {
        let row = rows[0];
        this.formData.userId = row.id;
        this.formData.nickName = row.nickName;
      }
    },
    confirmInsertSaleOrg(rows) {
      if (rows.length) {
        let row = rows[0];
        this.formData.saleOrgId = row.id;
        this.formData.saleOrgName = row.name;
      }
    },
    confirmInsertPosition(rows) {
      if (rows.length) {
        let row = rows[0];
        this.formData.positionId = row.id;
        this.formData.positionName = row.name;
      }
    },
    dialogClose() {
      this.$emit("update:visiable", false);
    },
    deleteRows() {
      let checkRows = this.$refs["table-m1"].getCheckboxRecords(true);
      if (!checkRows.length) {
        this.$message({
          message: this.$t2(
            "请选择需要删除的文件权限",
            "components.fileLibrary.deleteAuthTip1"
          ),
          type: "error",
          showClose: true,
          duration: 3000,
        });
        return;
      }
      this.$baseConfirm(
        this.$t2(
          "您确定要删除吗？",
          "components.fileLibrary.deleteAuthConfirmTip"
        )
      ).then(() => {
        let ids = checkRows.map((checkRow) => checkRow.id);
        this.$http({
          method: "post",
          url: this.current_prefix + "/file_obj_auth/delete",
          data: ids,
          isLoading: true,
          success: (res) => {
            this.$message({
              message: res.content,
              type: "success",
              duration: 500,
            });
            this.searchEvent();
          },
        });
      });
    },
    openUserRowDialog(row, rowIndex) {
      this.operateIndex = rowIndex;
      this.showUserRowDialog = true;
    },
    openSaleOrgRowDialog(row, rowIndex) {
      this.operateIndex = rowIndex;
      this.showSaleOrgRowDialog = true;
    },
    openPositionRowDialog(row, rowIndex) {
      this.operateIndex = rowIndex;
      this.showPositionRowDialog = true;
    },
    openRoleRowDialog(row, rowIndex) {
      this.operateIndex = rowIndex;
      this.showRoleRowDialog = true;
    },
    confirmtUserRow(rows) {
      if (rows.length) {
        let row = rows[0];
        let item = this.tableData[this.operateIndex];
        item.userId = row.id;
        item.nickName = row.nickName;
      }
    },
    confirmSaleOrgRow(rows) {
      if (rows.length) {
        let row = rows[0];
        let item = this.tableData[this.operateIndex];
        item.saleOrgId = row.id;
        item.saleOrgName = row.name;
      }
    },
    confirmPositionRow(rows) {
      if (rows.length) {
        let row = rows[0];
        let item = this.tableData[this.operateIndex];
        item.positionId = row.id;
        item.positionName = row.name;
      }
    },
    confirmRoleRow(rows) {
      if (rows.length) {
        let row = rows[0];
        let item = this.tableData[this.operateIndex];
        item.roleId = row.id;
        item.roleName = row.name;
      }
    },

    openUserAddDialog(row, rowIndex) {
      this.showUserAddDialog = true;
    },
    openSaleOrgAddDialog(row, rowIndex) {
      this.showSaleOrgAddDialog = true;
    },
    openPositionAddDialog(row, rowIndex) {
      this.showPositionAddDialog = true;
    },
    openRoleAddDialog(row, rowIndex) {
      this.showRoleAddDialog = true;
    },
    confirmtUserAddDialog(rows) {
      if (rows.length) {
        const tableData = this.tableData;
        const map = {};
        tableData
          .filter((item) => !!item.userId)
          .forEach(function (item) {
            map[item.userId] = 1;
          });
        let items = [];

        rows.forEach((row, index) => {
          let item = this.createRow();
          item.userId = row.id;
          item.nickName = row.nickName;
          if (!map[item.userId]) {
            items.push(item);
          }
        });
        this.tableData.push(...items);
      }
    },
    confirmSaleOrgAddDialog(rows) {
      if (rows.length) {
        const tableData = this.tableData;
        const map = {};
        tableData
          .filter((item) => !!item.saleOrgId && !item.positionId)
          .forEach(function (item) {
            map[item.saleOrgId] = 1;
          });
        let items = [];

        rows.forEach((row, index) => {
          let item = this.createRow();
          item.saleOrgId = row.id;
          item.saleOrgName = row.name;
          if (!map[item.saleOrgId]) {
            items.push(item);
          }
        });
        this.tableData.push(...items);
      }
    },
    confirmPositionAddDialog(rows) {
      if (rows.length) {
        const tableData = this.tableData;
        const map = {};
        tableData
          .filter((item) => !!item.positionId && !item.saleOrgId)
          .forEach(function (item) {
            map[item.positionId] = 1;
          });
        let items = [];

        rows.forEach((row, index) => {
          let item = this.createRow();
          item.positionId = row.id;
          item.positionName = row.name;
          if (!map[item.positionId]) {
            items.push(item);
          }
        });
        this.tableData.push(...items);
      }
    },
    confirmRoleAddDialog(rows) {
      if (rows.length) {
        const tableData = this.tableData;
        const map = {};
        tableData
          .filter((item) => !!item.roleId)
          .forEach(function (item) {
            map[item.roleId] = 1;
          });
        let items = [];

        rows.forEach((row, index) => {
          let item = this.createRow();
          item.roleId = row.id;
          item.roleName = row.name;
          if (!map[item.roleId]) {
            items.push(item);
          }
        });
        this.tableData.push(...items);
      }
    },
    saveData() {
      this.$baseConfirm(
        this.$t2("您确定要保存吗？", "system.message.sureSave")
      ).then(() => {
        var url = this.current_prefix + `/file_obj_auth/saves`;
        let formData = this.tableData.filter((item) => !item._isParent);
        this.$http({
          url: url,
          method: `post`,
          data: {
            fileObjId: this.fileObjId,
            fileObjAuthDTOs:formData
          },
          isLoading: true,
          success: (res) => {
            this.$message({
              message: res.content,
              type: "success",
              duration: 500,
              onClose: (t) => {
                this.dialogClose();
              },
            });
          },
        });
      });
    },
    createRow() {
      let row = {
        saleOrgName: null,
        saleOrgId: null,
        positionName: null,
        positionId: null,
        roleName: null,
        roleId: null,
        nickName: null,
        userId: null,
        addAuth: 1,
        editAuth: 1,
        viewAuth: 1,
        uploadAuth: 1,
        downloadAuth: 1,
        moveAuth: 1,
        copyAuth: 1,
        replaceAuth: 1,
        deleteAuth: 1,
        deleteDirAuth: 1,
        historyAuth: 1,
        shareAuth: 1,
        downWatermarkAuth: 0,
        authAuth: 1,
        fileObjId: this.fileObjId,
      };
      return row;
    },
    openDetailDialog(row, rowIndex) {
      this.currentRow = this.$baseLodash.cloneDeep(row);
      this.operateIndex = rowIndex;
      this.showDetailDialog = true;
    },
    confirmDetailDialog(row) {
      this.tableData.splice(this.operateIndex, 1, row);
      this.showDetailDialog = false;
    },
  },
};
export default modules;
