<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="temporarySave"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭"
    okText="保存">
    <itdm-weituo-info-form ref="realForm" @ok="submitCallback" :disabled="disableSubmit"></itdm-weituo-info-form>
  </j-modal>
</template>

<script>
import { postAction  } from '@api/manage'

  import ItdmWeituoInfoForm from './ItdmWeituoInfoForm'
  export default {
    name: 'ItdmWeituoInfoModal',
    components: {
      ItdmWeituoInfoForm
    },
    data () {
      return {
        title:'',
        width:1300,
        visible: false,
        disableSubmit: false
      }
    },
    computed: {
      baseInfos() {
        return this.$store.getters.baseInfos
      },
      yangpinInfos() {
        return this.$store.getters.yangpinInfos
      },
      shiyanInfos() {
        return this.$store.getters.shiyanInfos
      },
    },
    methods: {
      initStore(){
        this.$store.commit('SET_BASEINFO', {});
        this.$store.commit('SET_YANGPININFO', []);
        this.$store.commit('SET_SHIYANINFO', []);
      },
      add () {
        this.initStore()
        this.visible=true
        this.$nextTick(()=>{
          this.$refs.realForm.add();
        })
      },
      edit (record) {
        this.initStore()
        this.visible=true
        this.$nextTick(()=>{
          this.$refs.realForm.edit(record);
        })
      },
      close () {
        this.initStore()
        this.$emit('close');
        this.visible = false;
      },
      // 临时保存
      temporarySave () {
        var weituoInfo = {}
        var weituoId = this.$refs.realForm.model.id

        switch (this.$refs.realForm.currentTab) {
          case 0:
            weituoInfo = this.$refs.realForm.$refs.data001.model
            weituoInfo.yangpins = this.yangpinInfos
            weituoInfo.pinShiYans = this.shiyanInfos
            break;
          case 1:
            weituoInfo = this.baseInfos
            weituoInfo.yangpins = this.$refs.realForm.$refs.ypxx.model.data
            weituoInfo.pinShiYans = this.shiyanInfos
            break;
          case 2:
            weituoInfo = this.baseInfos
            weituoInfo.yangpins = this.yangpinInfos
            var pinShiYans = this.$refs.realForm.$refs.syxx.model.list
            weituoInfo.pinShiYans = pinShiYans.map(item => {
              item.standardRequirement = item.standardRequirementQ.join(',')
              return item
            })
            break;
        
          default:
            break;
        }
        weituoInfo.id = weituoId ? weituoId : null
        console.log(weituoInfo)
        postAction('/weituo/itdmWeituoInfo/staging', weituoInfo ).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.$emit('ok')
            this.visible = false;
          } else {
            this.$message.warning(res.message)
            // this.visible = false;
          }
        }).finally(() => {
          // that.confirmLoading = false
        })
      },
      // 
      submitCallback(){
        this.$emit('ok');
        this.visible = false;
      },
      handleCancel () {
        this.close()
      }
    }
  }
</script>