<template>
  <div class="bpm-form">
    <a-spin :spinning="confirmLoading>0">
<!--      <bpm-button :father="this" @save="save" ref="bpmButton" @afterBpmRefresh="afterBpmRefresh"></bpm-button>-->
      <a-card id="formContent" size="small" class="bpm-module-card">
        <template slot="title">
          <span class="bpm-module-title">{{$locale('m.eform.appform.common.business_form_title')}}</span>
        </template>
        <!--<a href="#" slot="extra">more</a>-->
        <k-form-build :process-def-id="processDefId"
                      @refreshParent="refreshParent"
                      @close="close"
                      v-if="refreshForm"
                      :value="jsonData"
                      @submit="handleSubmit"
                      ref="KFormBuild"
                      :allDataSource="allDataSource"
                      @refreshAllData="refreshAllData"
                      :dsCode="dsCode"
                      :dictMap="dictMap"
                      :elValue="elValue"
                      :formId="id"
                      :fks="fks"
                      :stores="stores"
                      :button-datas="buttonDatas"
                      :step-id="stepId"
                      @changeStep="changeStep"
                      @addModelKey="addModelKey"
                      :uploadTimestamp="uploadTimestamp"
        />
      </a-card>
      <br/>
      <a-card id="bpmIdea" size="small" class="bpm-module-card">
        <template slot="title">
          <span class="bpm-module-title">{{$locale('m.eform.appform.common.approval_opinions_title')}}</span>
        </template>
        <!--<a href="#" slot="extra">more</a>-->
        <bpm-idea :process-instance-id="processInstanceId" :refreshFlg="refreshProcess"></bpm-idea>
      </a-card>
      <br/>
      <a-card id="bpmPic" size="small" class="bpm-module-card">
        <template slot="title">
          <span class="bpm-module-title">{{$locale('m.eform.appform.common.process_chart_title')}}</span>
        </template>
        <!--<a href="#" slot="extra">more</a>-->
        <bpm-pic :process-instance-id="processInstanceId"
                 :process-def-id="processDefId"
                 :refreshFlg="refreshProcess"
        ></bpm-pic>
      </a-card>
    </a-spin>
  </div>
</template>

<script>

  import {getAction} from '../../utils/manage'
  import YchConstant from "../../utils/YchConstant";
  // import BpmButton from '../../bpm/modules/BpmButton'
  import { BpmMixin } from '../../utils/BpmMixin'
  import { FormMixin } from '../../utils/FormMixin'
  import KFormBuild from '../../../packages/KFormBuild/index.vue'
  import BpmPic from '../bpm/bpmTrack/BpmPic'
  import BpmIdea from '../bpm/bpmTrack/BpmIdea'
  import {handleCreateFunc, handleExecFunc} from "../../utils/actions";
  import locleUtil from "../../utils/LocleUtil";

  export default {
    name: 'BpmForm',
    mixins: [BpmMixin, FormMixin],
    components: {
      // BpmButton,
      KFormBuild,
      BpmPic,
      BpmIdea
    },
    props: {
      fks: {
        type: Array,
        default: () => []
      },
      parentValue: {
        type: Object,
        default: () => {}
      },
      stores: {
        type: Object,
        default: () => {
          return {}
        }
      },
      relationList: Array,
      refreshMap: Object,
      tabConfig: Object,
      relationStatus: String,
      relationOpenFks: String,
    },
    provide() {
      return {
        getBpmButtonVm: this.getBpmButtonVm,
      }
    },
    data () {
      return {
        isFlow: true,
        tableName: null,
        getTableNameByFormCode: YchConstant.PREFIX_EFORM + '/engine/formEngine/getTableNameByFormCode',
        relationValue: null,
        refreshForm: true,
        stepId: null, //点击导航条回传的id
      }
    },
    methods: {
      getBpmButtonVm() {
        return this.$refs.bpmButton
      },
      //切换流程
      async changeStep(itemData,stepClickCurrent,stepCurrentId){
        this.refreshForm = false;
        const formKey = (itemData.formInfoVo && itemData.formInfoVo.formDefKey) ||this.formCode
        const formId = (itemData.formInfoVo && itemData.formInfoVo.id) ||this.id
        this.stepId = stepClickCurrent;
        await this.open(this.url.getData + '?formKey=' + formKey + '&id=' + formId)
        this.refreshForm = true;
      },
      editableModel(jsonList) {
        if (!jsonList) {
          return
        }
        jsonList.forEach(vo => {
          if (vo.options && typeof vo.options.disabled !== "undefined" && typeof vo.options.disabledBak === "undefined") {
            // 备份原始数据
            vo.options.disabledBak = vo.options.disabled
          }
          if (this.startPage && vo.options && typeof vo.options.disabled !== "undefined") {
            // 未启动时按表单默认配置
            vo.options.disabled = vo.options.disabledBak
          } else if (vo.dataId) {
            vo.options.disabled = !(this.editableList.indexOf(vo.dataId) !== -1)
          } else if (vo.model) {
            vo.options.disabled = !(this.editableList.indexOf(vo.model) !== -1)
          } else if (vo.type === 'editableTable' && vo.tableName) {
            vo.options.disabled = !(this.editableList.indexOf(vo.tableName) !== -1)
          } else {
            this.editableModel(vo.list)
            this.editableModel(vo.trs)
            this.editableModel(vo.tds)
            this.editableModel(vo.columns)
            this.editableModel(vo.tabs)
          }
        })
      },
      requiredModel(jsonList) {
        if (!jsonList) {
          return
        }
        jsonList.forEach(vo => {
          if (vo.rules && typeof vo.rulesBak === "undefined") {
            // 备份原始数据
            vo.rulesBak = vo.rules
          }
          if (this.startPage && vo.rules) {
            // 未启动时按表单默认配置
            vo.rules = vo.rulesBak
          } else if (vo.dataId && vo.rules) {
            const requiredRule = vo.rules.find(item => typeof item.required !== "undefined");
            if (requiredRule) {
              requiredRule.message = requiredRule.message || this.$locale('m.common.tip.verify_required')
              requiredRule.required = this.requiredList.indexOf(vo.dataId) !== -1
            }
          } else if (vo.model && vo.rules) {
            const requiredRule = vo.rules.find(item => typeof item.required !== "undefined");
            if (requiredRule) {
              requiredRule.message = requiredRule.message || this.$locale('m.common.tip.verify_required')
              requiredRule.required = this.requiredList.indexOf(vo.model) !== -1
            }
          } else if (vo.type === 'editableTable' && vo.tableName && vo.rules) {
            const requiredRule = vo.rules.find(item => typeof item.required !== "undefined");
            if (requiredRule) {
              requiredRule.message = requiredRule.message || this.$locale('m.common.tip.verify_required')
              requiredRule.required = this.requiredList.indexOf(vo.tableName) !== -1
            }
          } else {
            this.requiredModel(vo.list)
            this.requiredModel(vo.trs)
            this.requiredModel(vo.tds)
            this.requiredModel(vo.columns)
            this.requiredModel(vo.tabs)
          }
        })
      },
      $locale(params){
        return locleUtil.getLocale(params)
      },
      hiddenBpmModel(jsonList) {
        if (!jsonList) {
          return
        }
        jsonList.forEach(vo => {
          if (vo.options && typeof vo.options.hidden !== "undefined" && typeof vo.options.hiddenBak === "undefined") {
            // 备份原始数据
            vo.options.hiddenBak = vo.options.hidden
          }
          if (this.startPage && vo.options && typeof vo.options.hidden !== "undefined") {
            // 未启动时按表单默认配置
            vo.options.hidden = vo.options.hiddenBak
          } else if (vo.dataId) {
            vo.options.hidden = this.hiddenList.indexOf(vo.dataId) !== -1 || vo.options.hiddenBak
          } else if (vo.model) {
            vo.options.hidden = this.hiddenList.indexOf(vo.model) !== -1 || vo.options.hiddenBak
          } else if (vo.type === 'editableTable' && vo.tableName) {
            vo.options.hidden = this.hiddenList.indexOf(vo.tableName) !== -1 || vo.options.hiddenBak
          } else {
            this.hiddenBpmModel(vo.list)
            this.hiddenBpmModel(vo.trs)
            this.hiddenBpmModel(vo.tds)
            this.hiddenBpmModel(vo.columns)
            this.hiddenBpmModel(vo.tabs)
          }
        })
      },
      hiddenModel(jsonList) {
        if (!jsonList) {
          return
        }
        jsonList.forEach(vo => {
          if (vo.options && typeof vo.options.hidden !== "undefined" && typeof vo.options.hiddenBak === "undefined") {
            // 备份原始数据
            vo.options.hiddenBak = vo.options.hidden
          }
          if (this.startPage && vo.options && typeof vo.options.hidden !== "undefined") {
            // 未启动时按表单默认配置
            vo.options.hidden = vo.options.hiddenBak
          } else if (vo.dataId) {
            vo.options.hidden = this.hiddenList.indexOf(vo.dataId) !== -1 || vo.options.hiddenBak
          } else if (vo.model) {
            vo.options.hidden = this.hiddenList.indexOf(vo.model) !== -1 || vo.options.hiddenBak
          } else if (vo.type === 'editableTable' && vo.tableName) {
            vo.options.hidden = this.hiddenList.indexOf(vo.tableName) !== -1 || vo.options.hiddenBak
          } else {
            this.hiddenModel(vo.list)
            this.hiddenModel(vo.trs)
            this.hiddenModel(vo.tds)
            this.hiddenModel(vo.columns)
            this.hiddenModel(vo.tabs)
          }
        })
      },
      refreshAllData(){
        // 重新加载
        this.detail()
      },
      setPk() {
        if (this.formValues && this.tableName) {
          this.$emit('setPk', {
            record: this.formValues,
            tableName: this.tableName,
            relationStatus: this.relationStatus,
            isForm: true,
          })
        }
      },
    },
    created() {
      console.log(this.$props,'BpmForm')
      getAction(this.getTableNameByFormCode, {formCode: this.formCode}).then((res) => {
        if (res.success) {
          this.tableName = res.result
        }
      })

      // 把当前对象放入stores，方便其他对象使用
      if (!this.stores.$vm) {
        this.stores.$vm = {}
      }
      this.stores.$vm[this.formCode] = this
    },
    watch: {
      jsonData: {
        handler (value) {
          this.editableModel(value.list)
          this.requiredModel(value.list)
          this.hiddenBpmModel(value.list)
          this.hiddenModel(value.list)
        },
        immediate: true
      },
      editableList: {
        handler () {
          this.editableModel(this.jsonData.list)
        },
        immediate: true
      },
      requiredList: {
        handler () {
          this.requiredModel(this.jsonData.list)
        },
        immediate: true
      },
      hiddenList: {
        handler () {
          this.hiddenBpmModel(this.jsonData.list)
        },
        immediate: true
      },
      hiddenList: {
        handler () {
          this.hiddenModel(this.jsonData.list)
        },
        immediate: true
      },
    }
  }
</script>
<style lang="less">
  .bpm-form {
    .bpm-module-card{
      margin: 0px 20px;
      .bpm-module-title {
        font-size: 16px;
        font-weight: 600;
      }
    }
  }
</style>
<style lang="css" scoped>
/deep/ .ant-spin-blur::after {
  opacity: 1;
}
</style>

