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

let modules = {};
modules = {
  name: 'bd_form_templateEdit',
  props: {
    _dataId: [String, Number],
    currentFormType: Object,
    // readonly: Boolean,
    otherFlag:Boolean,
    extractedObj: {
      type:Object,
      default:()=> null
    },
    hData:{
      type:Object,
      default:()=> null
    },
    compareHData: Object,
  },
  components: {
    projectTagView,
    MenuKindDialog,
    preformDialog,
    formOplogTable,
    otherAuthDialog
  },
  data() {
    return {
      isEdit: false,
      tabIndex: 'first',
      dataId: '',
      formTemplate: {
        enabled: true,
        history: false,
        serviceId: 'user',
        formTypeCode: null,
        menuKindName: null,
        menuKindCode: null,
        serviceName: null,
        formTemplateTagDTOs: [],
        hasWf: false
      },
      oldFormTemplate: {
        enabled: true,
        history: false,
        serviceId: 'user',
        formTypeCode: null,
        menuKindName: null,
        menuKindCode: null,
        serviceName: null,
        formTemplateTagDTOs: [],
        hasWf: false
      },
      isInited: false,
      serviceIds: ['user'],
      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.extractedObj?.objx?.id){
      this.dataId = this.extractedObj.obj.id;
    }else if (this._dataId) {
      this.dataId = this._dataId;
    } else {
      this.formTemplate.menuKindName = this.currentFormType?.name || null;
      this.formTemplate.menuKindCode = this.currentFormType?.menuKindCode || null;
      this.formTemplate.serviceName = this.currentFormType?.serviceName || null;
    }
  },
  mounted() {
    this.getBdEnv();
    this.getData();
  },
  methods: {
    initData(formData){
      this.isEdit = true;
      this.formTemplate = formData || {};
      this.getMenuKindAuth(this.formTemplate.menuKindCode);
      this.$nextTick(() => {
        this.isInited = true;
      });
    },
    getData() {
      if(this.extractedObj){
        this.dataId = this.extractedObj.id;
        this.initData(this.extractedObj)
        //获取操作日志数据
        this.$refs['oplogTable'].initData({
          param: () => {
            return {
              logType: "FormTemplate",
              logObjCode: this.formTemplate.formCode
            };
          }
        });
      }else if (this.dataId) {
        this.$commonHttp({
          aes: true,
          url: USER_PREFIX + `/formTemplate/getByFormCode`,
          method: `post`,
          data: {
            stringOne: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.initData(res.objx)
            //获取操作日志数据
            this.$refs['oplogTable'].initData({
              param: () => {
                return {
                  logType: "FormTemplate",
                  logObjCode: this.formTemplate.formCode
                };
              }
            });
            /* this.formTemplate = res.objx || {};
            this.getMenuKindAuth(this.formTemplate.menuKindCode);
            this.$nextTick(() => {
              this.isInited = true;
            });
            //获取操作日志数据
            this.$refs['oplogTable'].initData({
              param: () => {
                return {
                  logType: "FormTemplate",
                  logObjCode: this.formTemplate.formCode
                };
              }
            }); */
          }
        });
      } else if (this.hData){
        this.isEdit = true;
        this.formTemplate = this.$baseLodash.cloneDeep(this.hData);
        this.dataId = this.hData.id
        this.isInited = true;
        this.getMenuKindAuth(this.formTemplate.menuKindCode);
        if (this.compareHData && this.$attrs.isCompare) {
          this.oldFormTemplate = this.$baseLodash.cloneDeep(this.compareHData);
          this.isCompare = true;
        }
      } else {
        this.getMenuKindAuth(this.formTemplate.menuKindCode);
        this.isInited = true;
      }
    },
    saveData() {
      this.$refs.editForm.$baseValidate(valid => {
        if (valid) {
          this.openPreformDialog();
        }
      });
    },
    saveDataHandle(preformData) {
      var url = USER_PREFIX + (this.isEdit ? `/formTemplate/update` : `/formTemplate/save`);
      let formData = {
        ...this.formTemplate,
        logContent: preformData.logContent
      };
      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: this.formTemplate.formCode
                  }
                });
              }
            }
          });
        }
      });
    },
    openPreformDialog() {
      this.showPreformDialog = true;
    },
    confirmPreformDialog(preformData) {
      this.saveDataHandle(preformData);
    },
    confirmInsertMenuKind(rows) {
      if (rows.length > 0) {
        let row = rows[0];
        this.$set(this.formTemplate, 'menuKindCode', row.menuKindCode);
        this.$set(this.formTemplate, 'menuKindName', row.name);
      }
    },
    getBdEnv() {
      getBdFlag({
        success: res => {
          this.isDev = res.objx === 1
        }
      });
    },
    openDesignDialog() {
      let readonly = this.readonly || !this.menuKindAuth.editAuth
      this.$emit('openDesignDialog', {
        row: this.formTemplate,
        readonly,
        callback: () => {
          this.$baseReload()
        }
      })
    },
    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.formTemplate.sid) {
        this.$http({
          aes: true,
          url: USER_PREFIX + "/form_template_auth/getAuth",
          method: `post`,
          data: {stringOne: this.formTemplate.sid},
          isLoading: true,
          success: (res) => {
            this.menuKindAuth = res.objx || {};
          },
        });
      }
    },
    openOtherAuthDialog(){
      this.sid = this.formTemplate.sid
      this.showOtherAuthDialog = true
    },
    confirmOtherAuthDialog(){
      this.$baseReload()
    },
  }
};
export default modules
