<template>
  <div v-if="visible">
    <el-dialog id="reviseplanDialog" :title="title" :fullscreen="dialogFull" :destroy-on-close="true" :visible.sync="visible" :close-on-click-modal="false" :modal-append-to-body="false" :append-to-body="true" :before-close="closeDialog" width="90%">
      <template slot="title">
        <div class="avue-crud__dialog__header">
          <span class="el-dialog__title">
            <span style="display:inline-block;width:3px;height:20px;margin-right:5px; float: left;margin-top:2px" />
            {{ title }}
          </span>
          <div class="avue-crud__dialog__menu" @click="dialogFull? dialogFull=false: dialogFull=true">
            <svg-icon v-if="dialogFull" icon-class="exit-fullscreen" />
            <svg-icon v-else icon-class="fullscreen" />
          </div>
        </div>
      </template>
      <el-row class="dialogMinHeight">
        <el-button class="successBorder" size="medium" style="position: absolute;z-index: 1;right: 2px;top: 2px;" @click="saveData()">应用</el-button>
        <el-tabs v-model="parentActiveName" type="border-card" @tab-click="handleClick">
          <el-tab-pane label="栏舍配方" name="栏舍配方">
            <DhedFormula ref="DhedFormula" :parent-date="date" />
          </el-tab-pane>
          <el-tab-pane label="撒料计划" name="撒料计划">
            <MaterialIssuancePlan ref="MaterialIssuancePlan" :parent-date="date" />
          </el-tab-pane>
          <el-tab-pane label="预混计划" name="预混计划">
            <PremixedPlan ref="PremixedPlan" :parent-date="date" />
          </el-tab-pane>
          <!-- <el-tab-pane label="剩料计划" name="剩料计划">
            <SurplusMaterialPlan ref="SurplusMaterialPlan" :parent-date="date" />
          </el-tab-pane> -->
        </el-tabs>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelClose cancelClose1" @click="closeDialog()">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { PostDataByName } from '@/api/common'
import DhedFormula from './typePage/dhedFormula.vue'
import MaterialIssuancePlan from './typePage/materialIssuancePlan.vue'
import PremixedPlan from './typePage/premixedPlan.vue'
import SurplusMaterialPlan from './typePage/surplusMaterialPlan.vue'
import { MessageBox } from 'element-ui'
import Cookies from 'js-cookie'
export default {
  name: 'RevisePlan',
  components: {
    DhedFormula, MaterialIssuancePlan, PremixedPlan,SurplusMaterialPlan
  },
  props: {
    show: { type: Boolean, default: false }, // 弹框可见标志
    parentActiveName: { type: String, defalut: '栏舍配方' },
    parentDate: { type: String, defalut: '' }
  },
  data() {
    return {
      dialogFull: false,
      typeList: [{ id: '1', effect: 'dark', label: '栏舍配方' }, { id: '2', effect: 'plain', label: '撒料计划' }, { id: '3', effect: 'plain', label: '预混计划' }],
      visible: this.show,
      dialogFormVisible: false,
      dialogStatus: '',
      rules: {},
      date: '',
      // parentActiveName: '',
      temp: {},
      title: '',
      isDhedFormula: true,
      isMaterialIssuancePlan: false,
      isPremixedPlan: false,

      textMap: {
        revisePlan: '修改计划'
      },
      isokDisable: false,
      requestParam: {}
    }
  },
  watch: {
    // 监听show,visible 随着show变化而变化
    show: {
      immediate: true,
      handler(show) {
        this.visible = show
      }
    },
    parentDate: {
      immediate: true,
      handler(newVal, oldVal) {
        console.log('newVal-date', newVal)
        this.date = newVal
        this.title = '修改计划——' + this.date
      }
    },
    parentActiveName: {
      immediate: true,
      handler(newVal, oldVal) {
        console.log('newVal===', newVal)
        console.log('oldVal', oldVal)
        this.parentActiveName = newVal
      }
    }
  },
  created() {

  },
  methods: {
    closeDialog() {
      this.dialogFull = false
      this.$emit('update:show', false) // 子组件更新弹框隐藏
      this.$emit('activeName', '栏舍配方')
    },
    saveData() {
      console.log('点击了应用')
      MessageBox.confirm('是否确认将当前修改计划应用至' + this.date, {
        confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        this.requestParam.name = 'updatedownloadedplanDone'
        this.requestParam.parammaps = {}
        this.requestParam.parammaps.pastureid = Cookies.get('pastureid')
        this.requestParam.parammaps.date = this.date
        PostDataByName(this.requestParam).then(response => {
          if (response.msg === 'fail') {
            this.$notify({ title: '保存失败', message: response.data, type: 'warning', duration: 2000 })
          } else {
            this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
            this.closeDialog()
            this.$parent.getList()
          }
        })
      }).catch(() => {
        this.$message({ type: 'info', message: '已取消应用' })
      })
    },
    handleClick(item) {
      console.log(item.name)
      if (item.name == '撒料计划') {
        this.$refs.MaterialIssuancePlan.getButtons()
        this.$refs.MaterialIssuancePlan.getIsDisplay()
        this.$refs.MaterialIssuancePlan.getList()
      } else if (item.name == '栏舍配方') {
        this.$refs.DhedFormula.getIsDisplay()
        this.$refs.DhedFormula.getDownList()
      } else if (item.name == '栏舍配方'){
        this.$refs.PremixedPlan.getDownList()
        this.$refs.PremixedPlan.getIsDisplay()
        this.$refs.PremixedPlan.getList()
      }else if (item.name == '剩料计划'){
        this.$refs.SurplusMaterialPlan.getButtons()
        this.$refs.SurplusMaterialPlan.getDownList()
        this.$refs.SurplusMaterialPlan.getIsDisplay()
        this.$refs.SurplusMaterialPlan.getList()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  // #reviseplanDialog {
  //    margin-top: -10vh !important
  // }
</style>
