<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 :bordered="false" class="intelligentReplenishmentImport-cont">
        <div>
          <p class="total">共 <span>{{ listData.length }}</span> 条数据，红色字体表示数据不符合规范，请返回上一步修改文件后重新上传</p>
          <s-table
            class="sTable"
            ref="table"
            size="small"
            :rowKey="(record) => record.id"
            :columns="columns"
            :data="loadData"
            :scroll="{ x: 1440 }"
            :rowClassName="(record, index) => record.cost ? 'redBg-row':''"
            :showPagination="false"
            :defaultLoadData="false"
            bordered>
          </s-table>
          <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,
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center', fixed: 'left' },
        { title: '产品编码', dataIndex: 'productCode', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品状态', dataIndex: 'productStateDictValue', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '订货周期（天）', dataIndex: 'cycle', width: 120, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '是否淘汰', dataIndex: 'eliminateText', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '淘汰描述', dataIndex: 'eliminateDesc', width: 200, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '备注', dataIndex: 'remark', width: 200, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '错误原因', dataIndex: 'errorDesc', width: 200, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return 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
          }
          return data
        })
      },
      listData: [],
      errorFlag: []
    }
  },
  methods: {
    // 返回
    handleBack () {
      this.$router.push({ path: `/inventoryManagement/intelligentReplenishment/list`, query: { closeLastOldTab: true } })
    },
    // 返回基础设置页
    handleBackSet () {
      this.$router.push({ path: `/inventoryManagement/intelligentReplenishment/set` })
    },
    // 确认导入
    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.$refs.table.refresh(true)
    }
  },
  activated () {
    // 如果是新页签打开或者进入新的子页（例：存在列表第2条数据编辑页页签时再打开第4条数据的编辑页），则重置当前页面
    if (this.$store.state.app.isNewTab || !this.$store.state.app.isNewSubTab) {
      this.$refs.table.refresh(true)
    }
  }
}
</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>
