<template>
  <div class="intelligentReplenishmentImport-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-page-header :ghost="false" :backIcon="false" class="intelligentReplenishmentImport-back">
        <!-- 自定义的二级文字标题 -->
        <template slot="subTitle">
          <a id="intelligentReplenishmentImport-back-btn" href="javascript:;" @click="handleBack"><a-icon type="left" /> 返回列表</a>
        </template>
      </a-page-header>
      <a-card size="small" :bordered="false" class="intelligentReplenishmentImport-cont">
        <div>
          <p class="total">共 <span>{{ listData.length }}</span> 条数据,红色字体表示数据不符合规范,请返回上一步修改文件后重新上传</p>
          
        <ve-table
              border-y
              :border-around="false"
              :max-height="tableHeight"
              :row-style-option="{clickHighlight: true}"
              :cellSelectionOption="{enable: false}"
              :virtual-scroll-option="{enable: true}"
              :columns="columns"
              :table-data="listData"
              row-key-field-name="id"
            />
          <div class="btn-cont">
            <a-button
              type="primary"
              v-if="errorFlag.indexOf('1')==-1"
              id="intelligentReplenishmentLevel-submit"
              size="large"
              class="button-primary"
              @click="handleSubmit"
              style="padding: 0 60px;">确认导入</a-button>
            <a-button
              type="primary"
              v-if="errorFlag.indexOf('1')!=-1"
              id="intelligentReplenishmentLevel-set"
              size="large"
              class="button-primary"
              @click="handleBackSet"
              style="padding: 0 60px;">上一步</a-button>
            <a-button
              id="intelligentReplenishmentLevel-back"
              size="large"
              class="button-cancel"
              @click="handleBack"
              style="padding: 0 60px;">取消</a-button>
          </div>
        </div>
      </a-card>
    </a-spin>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { predictProductInfoGoods, predictProductInfoInsert } from '@/api/predict'
export default {
  name: 'IntelligentReplenishmentImport',
  mixins: [commonMixin],
  components: { STable },
  data () {
    return {
      spinning: false,
      tableHeight: 500,
      columns: [
        { title: '序号', field: 'no',key: "a", width: 80, align: 'center', fixed: 'left',operationColumn: false },
        { title: '产品编码', field: 'productCode',key: "b", width: 220, align: 'center',operationColumn: false,renderBodyCell: ({ row, column, rowIndex }, h) => { return row[column.field] || '--'} },
        { title: '产品名称', field: 'productName',key: "c", align: 'center',operationColumn: false , ellipsis: { showTitle: true },renderBodyCell: ({ row, column, rowIndex }, h) => { return row[column.field] || '--'}},
        { title: '产品状态', field: 'productStateDictValue',key: "d", width: 100, align: 'center',operationColumn: false,renderBodyCell: ({ row, column, rowIndex }, h) => { return row[column.field] || '--'} },
        { title: '订货周期(天)', field: 'cycle',key: "e", width: 120, align: 'center',operationColumn: false,renderBodyCell: ({ row, column, rowIndex }, h) => { return row[column.field] || row[column.field] == 0 ? row[column.field] : '--'}},
        { title: '是否淘汰', field: 'eliminateText',key: "f", width: 100, align: 'center',operationColumn: false,renderBodyCell: ({ row, column, rowIndex }, h) => { return row[column.field] || '--'}},
        { title: '淘汰描述', field: 'eliminateDesc',key: "g", width: 200, align: 'center',operationColumn: false, ellipsis: { showTitle: true },renderBodyCell: ({ row, column, rowIndex }, h) => { return row[column.field] || '--'}},
        { title: '备注', field: 'remark', width: 200,key: "h", align: 'center',operationColumn: false, ellipsis: { showTitle: true },renderBodyCell: ({ row, column, rowIndex }, h) => { return row[column.field] || '--'} },
        { title: '错误原因', field: 'errorDesc',key: "i", width: 200, align: 'center',operationColumn: false, ellipsis: { showTitle: true } ,renderBodyCell: ({ row, column, rowIndex }, h) => { return row[column.field] || '--'}}
      ],
      listData: [],
      errorFlag: []
    }
  },
  methods: {
    // 返回
    handleBack () {
      this.$router.push({ path: `/inventoryManagement/intelligentReplenishment/list`, query: { closeLastOldTab: true } })
    },
    // 返回基础设置页
    handleBackSet () {
      this.$router.push({ path: `/inventoryManagement/intelligentReplenishment/set` })
    },
    getTable(){
      this.tableHeight = window.innerHeight - 325
      this.spinning = true
      predictProductInfoGoods({ path: JSON.parse(this.$cookies.get('REPLENISHMENT_PATH')) }).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = i + 1
              this.errorFlag.push(data.list[i].errorFlag)
            }
            this.listData = data.list
          }
          this.spinning = false
        })
    },
    // 确认导入
    handleSubmit () {
      const _this = this
      _this.spinning = true
      predictProductInfoInsert(this.listData).then(res => {
        _this.spinning = false
        if (res.status == 200) {
          _this.$success({
            content: '数据导入成功!',
            centered: true,
            onOk () {
              _this.handleBack()
            }
          })
        } else {
          _this.$error({
            content: '数据导入失败,请重试!',
            centered: true,
            onOk () {}
          })
        }
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.getTable()
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页(例:存在列表第2条数据编辑页页签时再打开第4条数据的编辑页),则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.getTable()
    }
  }
}
</script>
<style lang="less">
  .intelligentReplenishmentImport-wrap{
    .intelligentReplenishmentImport-back{
      margin-bottom: 10px;
    }
    .intelligentReplenishmentImport-cont{
      .total{
        margin: 20px 0 30px;
        span{
          font-weight: bold;
        }
      }
      .btn-cont{
        margin: 30px 0 20px;
        text-align: center;
        .button-cancel{
          font-size: 12px;
          margin-left: 15px;
        }
      }
    }
    .redBg-row{
      background-color: #f5cdc8;
    }
  }
</style>