<template>
  <a-modal
    centered
    class="intelligentReplenishmentDetail-modal"
    :footer="null"
    :maskClosable="false"
    title="预测单详情"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <div>
        <a-alert type="info" style="margin-bottom:10px">
          <div slot="message">此预测单的各项参数信息如下</div>
        </a-alert>
        <div class="module-cont" v-if="detailData">
          <p class="title">产品范围</p>
          <a-tag color="blue" style="margin-bottom: 10px;">{{ detailData&&detailData.stockPredictRange==0?'自选产品':'全部产品' }}</a-tag>
          <a-table
            class="sTable"
            ref="table"
            size="small"
            :rowKey="(record) => record.id"
            :columns="columns"
            :data-source="loadData"
            :loading="loading"
            :scroll="{ y: 400 }"
            :pagination="false"
            v-if="detailData&&detailData.stockPredictRange == 0"
            bordered>
          </a-table>
        </div>
        <div class="module-cont">
          <p class="title">销售保障法</p>
          <p class="explain">保障倍数：<span>{{ (detailData&&detailData.saleEnsureNum) || '--' }}</span></p>
        </div>
        <div class="module-cont">
          <p class="title">趋势平均法</p>
          <p class="explain">服务水平：<span>{{ (detailData&&detailData.trendServiceLevel) || '--' }}%（对应的安全系数为{{ (detailData&&detailData.trendSafetyFactor) || '--' }}）</span></p>
        </div>
        <div class="spin-cont" v-if="detailData&&detailData.state=='RUN'">
          按照以上参数设置，系统正在计算中，请耐心等待...
          <a-spin style="margin-left: 15px;" />
        </div>
        <div class="btn-cont">
          <a-button type="primary" v-if="detailData&&detailData.state=='FINISH'" id="intelligentReplenishmentDetail-save" @click="handleDownload" style="margin-right: 15px;">结果下载</a-button>
          <a-button id="intelligentReplenishmentDetail-back" @click="isShow = false">关闭</a-button>
        </div>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import moment from 'moment'
import { STable } from '@/components'
import { predictDetail, predictRangeList, predictstockPredictExport } from '@/api/predict'
export default {
  name: 'IntelligentReplenishmentDetailModal',
  mixins: [commonMixin],
  components: { STable },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      detailsData: null, //  详情数据
      columns: [
        { title: '序号', dataIndex: 'no', width: '10%', align: 'center' },
        { title: '类型', dataIndex: 'rangeTypeName', width: '45%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '类型名称', dataIndex: 'rangeDataName', width: '45%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      loadData: [],
      detailData: null,
      loading: false
    }
  },
  methods: {
    // 详情
    getDetail () {
      predictDetail({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          this.detailData = res.data
          if (this.detailData.stockPredictRange == 0) { //  自选产品
            this.getPredictRange()
          }else{
            this.loadData = []
          }
        } else {
          this.detailData = null
        }
      })
    },
    // 获取产品范围
    getPredictRange () {
      this.loading = true
      predictRangeList({ 'sn': this.detailData.stockPredictSn }).then(res => {
        this.loading = false
        if (res.status == 200) {
          res.data.map((item, index) => {
            item.no = index + 1
          })
          this.loadData = res.data
        } else {
          this.loadData = []
        }
      })
    },
    //  结果下载
    handleDownload () {
      const _this = this
      const params = {
        sn: (this.detailData && this.detailData.stockPredictSn) || undefined
      }
      _this.downloadLoading = true
      _this.spinning = true
      predictstockPredictExport(params).then(res => {
        _this.downloadLoading = false
        _this.spinning = false
        if (res.type == 'application/json') {
          var reader = new FileReader()
          reader.addEventListener('loadend', function () {
            const obj = JSON.parse(reader.result)
            _this.$notification.error({
              message: '提示',
              description: obj.message
            })
          })
          reader.readAsText(res)
        } else {
          this.download(res)
        }
      })
    },
    download (data) {
      if (!data) { return }
      const url = window.URL.createObjectURL(new Blob([data]))
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      const a = moment().format('YYYYMMDDHHmmss')
      const fname = '智能补货' + a
      link.setAttribute('download', fname + '.xlsx')
      document.body.appendChild(link)
      link.click()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.detailData = null
         this.loadData = []
      }else{
        this.getDetail()
      }
    }
  }
}
</script>

<style lang="less">
  .intelligentReplenishmentDetail-modal{
    .module-cont{
      border: 1px dashed #e8e8e8;
      border-radius: 12px;
      position: relative;
      margin: 30px 0;
      padding: 20px 15px 15px;
      .title{
        position: absolute;
        left: 50px;
        top: -12px;
        padding: 0 10px;
        background-color: #fff;
      }
      .explain{
        margin-bottom: 5px;
        font-weight: bold;
      }
    }
    .spin-cont{
      text-align: center;
    }
    .btn-cont {
      text-align: center;
      font-weight: bold;
      margin: 35px 0 10px;
    }
  }
</style>
