<template>
  <a-modal
    centered
    class="intelligentReplenishmentLevel-modal"
    :footer="null"
    :maskClosable="false"
    title="选择服务水平"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <div>
      <a-table
        class="sTable"
        ref="table"
        :row-selection="{ type: 'radio', columnTitle: '选择', selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :columns="columns"
        :data-source="listData"
        :scroll="{ y: 400 }"
        :pagination="false"
        bordered>
      </a-table>
      <div class="btn-cont">
        <a-button type="primary" id="intelligentReplenishmentLevel-save" @click="handleSave">保存</a-button>
        <a-button id="intelligentReplenishmentLevel-back" @click="isShow = false" style="margin-left: 15px;">关闭</a-button>
      </div>
    </div>
  </a-modal>
</template>

<script>
import { STable } from '@/components'
import { predictTrendList } from '@/api/predict'
export default {
  name: 'IntelligentReplenishmentLevelModal',
  components: { STable },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      detailsData: null, //  详情数据
      columns: [
        { title: '服务水平（%）', dataIndex: 'serviceLevel', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '安全系数', dataIndex: 'safetyFactor', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      listData: [],
      selectedRowKeys: []
    }
  },
  methods: {
    getTableData(){
      predictTrendList({}).then(res => {
        if (res.status == 200) {
          this.listData = res.data
          this.listData.map(item => {
            item.key = Number(item.trendSn)
          })
        }
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      console.log('selectedRowKeys changed: ', selectedRowKeys, selectedRows)
      this.selectedRowKeys = selectedRowKeys
    },
    //  保存
    handleSave () {
      if (this.selectedRowKeys.length > 0) {
        const row = this.listData.find(item => item.trendSn == this.selectedRowKeys[0])
        if(row){
          const params = {
            trendSn: row.trendSn,
            serviceLevel: row.serviceLevel,
            safetyFactor: row.safetyFactor
          }
          this.$emit('ok', params)
        }else{
          this.$message.warning('请选择服务水平后再进行保存！')
        }
      } else {
        this.$message.warning('请选择服务水平后再进行保存！')
      }
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        console.log(this.itemSn)
        this.selectedRowKeys = [Number(this.itemSn)]
        this.getTableData()
      }
    }
  }
}
</script>

<style lang="less">
  .intelligentReplenishmentLevel-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>
