import formOplogTable from "@base/components/formOplog/index.vue";
import preformDialog from "@base/views/bd/setting/form_script/preformDialog.vue";
import projectTagView from "@base/components/projectTag/view.vue";
import MenuKindDialog from "@base/views/bd/setting/menu_kind/dialog.vue";
import { getBdFlag } from "@base/api/user";
import otherAuthDialog from "../otherAuthDialog.vue";

let modules = {};
modules = {
  name: "user_form_scriptEdit",
  props: {
    _dataId: [String, Number],
    formCode: String,
    formTemplate: Object,
    currentFormType: Object,
    // readonly: Boolean,
    otherFlag: Boolean,
    scriptType: {
      type: Number,
      default: 0,
    },
    hData: Object,
    compareHData: Object,
    extractedObj: Object,
  },
  components: {
    formOplogTable,
    preformDialog,
    projectTagView,
    MenuKindDialog,
    otherAuthDialog,
  },
  data() {
    return {
      isEdit: false,
      tabIndex: "first",
      dataId: "",
      formScript: {
        scriptType: 0,
        enabled: true,
        formCode: null,
        menuKindCode: null,
        menuKindName: null,
        serviceName: null,
        transactions: 1,
        formScriptTagDTOs: [],
      },
      oldFormScript: {
        scriptType: 0,
        enabled: true,
        formCode: null,
        menuKindCode: null,
        menuKindName: null,
        serviceName: null,
        transactions: 1,
        formScriptTagDTOs: [],
      },
      showCodeEditor: false,
      showMenuKindDialog: false,
      isDev: true,
      showPreformDialog: false,
      menuKindAuth: {
        editAuth: 0,
      },
      sid: null,
      showOtherAuthDialog: false,
      readonly: false,
      isCompare: false,
    };
  },
  created() {
    if (this.hData) {
      this.readonly = true;
    } else {
      this.readonly = this.$attrs.readonly || false;
    }
    if (this._dataId && !isNaN(this._dataId)) {
      this.dataId = this._dataId;
    } else {
      if (this.formTemplate) {
        this.formScript.scriptType = 1;
        this.formScript.formCode = this.formTemplate.formCode;
        this.formScript.menuKindCode = this.formTemplate.menuKindCode;
        this.formScript.menuKindName = this.formTemplate.menuKindName;
        this.formScript.serviceName = this.formTemplate.serviceName;
      } else {
        this.formScript.scriptType = this.scriptType;
        this.formScript.formCode = "intf";
        this.formScript.menuKindName = this.currentFormType?.name || null;
        this.formScript.menuKindCode =
          this.currentFormType?.menuKindCode || null;
        this.formScript.serviceName = this.currentFormType?.serviceName || null;
      }
    }
  },
  mounted() {
    this.getBdEnv();
    this.getData();
  },
  methods: {
    initData(formData){
      if(!!formData?.id)this.isEdit = true;
      this.formScript = formData || {};
      this.showCodeEditor = true;
      this.getMenuKindAuth(this.formScript.menuKindCode);
    },
    getData() {
      if(this.extractedObj){
        this.dataId = this.extractedObj.id;
        this.initData(this.extractedObj)
        //获取操作日志数据
        this.$refs["oplogTable"].initData({
          param: () => {
            return {
              logType: "FormScript",
              logObjCode: this.formScript.sid,
            };
          },
        });
      }else if (this.dataId && !isNaN(this.dataId)) {
        this.isEdit = true;
        this.$commonHttp({
          aes: true,
          url: USER_PREFIX + `/formScript/get`,
          method: `post`,
          data: {
            id: this.dataId,
          },
          isLoading: true,
          modalStrictly: true,
          success: (res) => {
            // this.formScript = res.objx || {};
            // this.showCodeEditor = true;
            // this.getMenuKindAuth(this.formScript.menuKindCode);
            this.initData(res.objx)
            //获取操作日志数据
            this.$refs["oplogTable"].initData({
              param: () => {
                return {
                  logType: "FormScript",
                  logObjCode: this.formScript.sid,
                };
              },
            });
          },
        });
      } else if (this.hData) {
        this.dataId = this.hData.id;
        /* this.isEdit = true;
        this.formScript = this.$baseLodash.cloneDeep(this.hData);
        this.getMenuKindAuth(this.formScript.menuKindCode);
        this.showCodeEditor = true; */
        this.initData(this.$baseLodash.cloneDeep(this.hData))

        if (this.compareHData && this.$attrs.isCompare) {
          this.oldFormScript = this.$baseLodash.cloneDeep(this.compareHData);
          this.isCompare = true;
        }
      } else {
        this.getMenuKindAuth(this.formScript.menuKindCode);
        this.showCodeEditor = true;
      }
    },
    saveData() {
      this.$refs.editForm.$baseValidate((valid) => {
        if (valid) {
          this.openPreformDialog();
        }
      });
    },
    saveDataHandle(preformData) {
      var url =
        USER_PREFIX + (this.isEdit ? `/formScript/update` : `/formScript/save`);
      let formData = {
        ...this.formScript,
        logContent: preformData.logContent,
        transactions: preformData.transactions,
      };
      this.$http({
        aes: true,
        url: url,
        method: `post`,
        data: formData,
        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,
                  },
                });
              }
            },
          });
        },
      });
    },
    openPreformDialog() {
      this.showPreformDialog = true;
    },
    confirmPreformDialog(preformData) {
      this.saveDataHandle(preformData);
    },
    confirmInsertMenuKind(rows) {
      if (rows.length > 0) {
        let row = rows[0];
        this.$set(this.formScript, "menuKindCode", row.menuKindCode);
        this.$set(this.formScript, "menuKindName", row.name);
      }
    },
    getBdEnv() {
      getBdFlag({
        success: (res) => {
          this.isDev = res.objx === 1;
        },
      });
    },
    getMenuKindAuth(menuKindCode) {
      if (this.otherFlag) {
        this.getOtherMenuKindAuth();
      } else if (menuKindCode) {
        this.$http({
          aes: true,
          url: USER_PREFIX + "/menu_kind_auth/getAuth",
          method: `post`,
          data: { stringOne: menuKindCode },
          isLoading: true,
          success: (res) => {
            this.menuKindAuth = res.objx || {};
          },
        });
      }
    },

    getOtherMenuKindAuth() {
      if (this.otherFlag && this.formScript.sid) {
        this.$http({
          aes: true,
          url: USER_PREFIX + "/form_script_auth/getAuth",
          method: `post`,
          data: { stringOne: this.formScript.sid },
          isLoading: true,
          success: (res) => {
            this.menuKindAuth = res.objx || {};
          },
        });
      }
    },
    openOtherAuthDialog() {
      this.sid = this.formScript.sid;
      this.showOtherAuthDialog = true;
    },
    confirmOtherAuthDialog() {
      this.$baseReload();
    },
  },
};
export default modules;
