<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: {
      add () {

        sessionStorage.removeItem("data")
        sessionStorage.removeItem("yangpin")
        sessionStorage.removeItem("yangpinkz")

        sessionStorage.removeItem("count")
        sessionStorage.removeItem("count1")

        this.$store.commit('SET_BASEINFO', {});
        this.$store.commit('SET_YANGPININFO', []);
        this.$store.commit('SET_SHIYANINFO', []);


        this.visible=true
        this.$nextTick(()=>{
          this.$refs.realForm.add();
        })
      },
      edit (record) {
        sessionStorage.removeItem("data")
        sessionStorage.removeItem("yangpin")
        sessionStorage.removeItem("yangpinkz")
        sessionStorage.removeItem("count")
        sessionStorage.removeItem("count1")
        this.$store.commit('SET_BASEINFO', {});
        this.$store.commit('SET_YANGPININFO', []);
        this.$store.commit('SET_SHIYANINFO', []);
        this.visible=true
        this.$nextTick(()=>{
          this.$refs.realForm.edit(record);
        })
      },
      close () {
        // sessionStorage.removeItem("data")
        // sessionStorage.removeItem("yangpin")
        // sessionStorage.removeItem("yangpinkz")
        // sessionStorage.removeItem("count")
        // sessionStorage.removeItem("count1")
        this.$store.commit('SET_BASEINFO', {});
        this.$store.commit('SET_YANGPININFO', []);
        this.$store.commit('SET_SHIYANINFO', []);
        this.$emit('close');
        this.visible = false;
      },
      // 临时保存
      temporarySave () {
        var weituoInfo = {}
        // console.log(this.$refs.realForm.model.id)
        var weituoId = this.$refs.realForm.model.id

        // weituoInfo.baseInfos = this.baseInfos
        // weituoInfo.yangpins = this.yangpinInfos
        
        // var pinShiYans = this.shiyanInfos
        // if(this.shiyanInfos.length > 0){
        //   weituoInfo.pinShiYans = pinShiYans.map(item => {
        //     item.standardRequirement = item.standardRequirementQ.toString()
        //     return item
        //   })
        // }

        switch (this.$refs.realForm.currentTab) {
          case 0:
            // console.log(this.$refs.realForm.$refs.data001.model)
            weituoInfo = this.$refs.realForm.$refs.data001.model
            weituoInfo.yangpins = this.yangpinInfos
        
            if(this.shiyanInfos.length > 0){
              weituoInfo.pinShiYans = this.shiyanInfos.map(item => {
                item.standardRequirement = item.standardRequirement.toString()
                return item
              })
            } else {
              weituoInfo.pinShiYans = this.shiyanInfos
            }
            break;
          case 1:
            // console.log(this.$refs.realForm.$refs.ypxx.model)
            // weituoInfo = JSON.parse(sessionStorage.getItem('data'))
            weituoInfo = this.baseInfos
            weituoInfo.yangpins = this.$refs.realForm.$refs.ypxx.model.data
            if(this.shiyanInfos.length > 0){
              weituoInfo.pinShiYans = this.shiyanInfos.map(item => {
                item.standardRequirement = item.standardRequirement.toString()
                return item
              })
            } else {
              weituoInfo.pinShiYans = this.shiyanInfos
            }
            break;
          case 2:
            // console.log(this.$refs.realForm.$refs.syxx.model)
            // weituoInfo = JSON.parse(sessionStorage.getItem('data'))
            // weituoInfo.yangpins = JSON.parse(sessionStorage.getItem('yangpin'))
            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(',')
              // item.standardRequirement = JSON.stringify(item.standardRequirementQ)
              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>