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";

let modules = {};
modules = {
  props: {
    _dataId: [String, Number],
    fileObjId: Number,
    treeNodeArr: Array
  },
  components: {
    userDialog, positionDialog, saleOrgDialog, roleDialog
  },
  inject: ['current_prefix', 'getFileStoreArea'],
  data() {
    return {
      isEdit: false,
      tabIndex: 'first',
      dataId: '',
      fileObjAuth: {
        saleOrgName: null,
        positionName: null,
        roleName: null,
        nickName: 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
      },
      showUserDialog: false,
      showPositionDialog: false,
      showSaleOrgDialog: false,
      showRoleDialog: false
    };
  },
  created() {
    if (this._dataId && !isNaN(this._dataId)) this.dataId = this._dataId;
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      if (this.dataId && !isNaN(this.dataId)) {
        this.isEdit = true;
        this.$http({
          url: this.current_prefix + `/file_obj_auth/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.fileObjAuth = res.objx || {};
          }
        });
      } else {
        this.fileObjAuth.fileObjId = this.fileObjId;
      }
    },
    saveData() {
      this.$refs.editForm.$baseValidate(valid => {
        if (valid) {
          this.$baseConfirm(this.$t2('您确定要保存吗？', 'system.message.sureSave')).then(() => {
            var url = this.current_prefix + (this.isEdit ? `/file_obj_auth/update` : `/file_obj_auth/save`);
            this.$http({
              url: url,
              method: `post`,
              data: this.fileObjAuth,
              isLoading: true,
              success: res => {
                this.$message({
                  message: res.content,
                  type: 'success',
                  duration: 500,
                  onClose: t => {
                    if (this.isEdit) {
                      this.$baseReload();
                    } else {
                      this.$baseReload({
                        updateParam: {
                          _dataId: res.objx.id
                        }
                      });
                    }
                  }
                });
              }
            });
          });
        }
      });
    },
    confirmInsertUser(rows) {
      if (rows.length) {
        let row = rows[0];
        this.fileObjAuth.userId = row.id;
        this.fileObjAuth.nickName = row.nickName;
      }
    },
    confirmInsertSaleOrg(rows) {
      if (rows.length) {
        let row = rows[0];
        this.fileObjAuth.saleOrgId = row.id;
        this.fileObjAuth.saleOrgName = row.name;
      }
    },
    confirmInsertPosition(rows) {
      if (rows.length) {
        let row = rows[0];
        this.fileObjAuth.positionId = row.id;
        this.fileObjAuth.positionName = row.name;
      }
    },
    confirmInsertRole(rows) {
      if (rows.length) {
        let row = rows[0];
        this.fileObjAuth.roleId = row.id;
        this.fileObjAuth.roleName = row.name;
      }
    },
  }
};
export default modules;
