<template>
  <a-modal
    centered
    class="setPrice-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    :title="title"
    v-model="isShow"
    @cancel="cancel"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="setPrice-title">
        <div>以下产品没有成本价，无法进行财务盘点审核！</div>
        <div>请先设置成本价，然后再审核。</div>
      </div>
      <div class="setPrice-table">
        <a-table
          class="sTable fixPagination"
          ref="table"
          size="small"
          :rowKey="(record,index) => record.id +'-'+ index"
          :columns="columns"
          :data-source="list"
          :scroll="{ y:400 }"
          :pagination="false"
          bordered>
          <template slot="action" slot-scope="text,record">
            <a-input-number
              :id="'setPrice-const-'+record.id"
              style="width: 100%;"
              size="small"
              v-model="record.lastStockCost"
              :precision="2"
              :min="0"
              :max="999999"
              placeholder="请输入成本价"
            />
          </template>
        </a-table>
      </div>
      <div class="btn-cont">
        <a-button id="setPrice-save" type="primary" @click="handleSubmit">保存</a-button>
        <a-button id="setPrice-cancel" @click="cancel" style="margin-left: 15px;">取消</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { stockWarnSaveBatch } from '@/api/checkWarehouse'
export default {
  name: 'SettleModal',
  components: {},
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    title: { // 标题
      type: String,
      default: '设置成本价'
    }
  },
  data () {
    return {
      spinning: false,
      confirmLoading: false, // 确认按钮loading
      isShow: this.openModal, //  是否打开弹框
      row: null, // 当前操作数据
      list: [], // 列表数据
      // 表头
      columns: [
        { title: '产品编码', dataIndex: 'productCode', width: '30%', align: 'center', customRender: function (text) { return ((text || text != ' ') ? text : '--') } },
        { title: '产品名称', dataIndex: 'productName', width: '50%', align: 'left', customRender: function (text) { return (text || '--') } },
        { title: '成本价', scopedSlots: { customRender: 'action' }, width: '20%', align: 'center' }
      ]
    }
  },
  methods: {
    //  保存
    handleSubmit (e) {
      const _this = this
      const hasEmpty = this.list.filter(item => !item.lastStockCost && item.lastStockCost != 0)
      if (hasEmpty.length) {
        this.$message.info('成本价不能为空！')
        return
      }
      const params = []
      this.list.map(item => {
        params.push({
          'id': item.id,
          'lastStockCost': item.lastStockCost
        })
      })
      stockWarnSaveBatch(params).then(res => {
        _this.isShow = false
        _this.$emit('ok', _this.row, true)
      })
    },
    // 取消
    cancel () {
      this.isShow = false
    },
    // 编辑信息
    setData (data, row) {
      this.list = data
      this.row = row
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    }
  }
}
</script>

<style lang="less">
  .setPrice-basicInfo-modal{
    .setPrice-title{
      text-align: center;
      padding-bottom: 20px;
      line-height: 24px;
    }
    .btn-cont{
      padding: 30px 0 0;
      text-align: center;
    }
  }
</style>
