<template>
  <a-modal
    centered
    class="creatPurchaseReutn-modal"
    :footer="null"
    :maskClosable="false"
    title="生成采购退货单"
    v-model="isShow"
    @cancel="isShow=false"
    :width="600">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="creatPurchaseReutn-con" v-if="isShow">
        <div class="row-flex" v-if="detail">
          <div>供应商：{{ detail.returnTargetName }}</div>
          <div><div class="col">总公司实收数量：{{ detail.totalReceiveQty||'--' }} </div><div class="col">退货金额：{{ detail.totalAmount||'--' }}</div></div>
          <div>
            <div>采购退货单：</div>
            <div style="width:80%;padding-top:5px;">
              <a-row>
                <a-col :span="6">
                  <a-checkbox :value="1" @change="onChange" :checked="syncStep.indexOf(1)>=0">
                    同步提交 >
                  </a-checkbox>
                </a-col>
                <a-col :span="6">
                  <a-checkbox :value="2" @change="onChange" :checked="syncStep.indexOf(2)>=0">
                    同步审核 >
                  </a-checkbox>
                </a-col>
                <a-col :span="6">
                  <a-checkbox :value="3" @change="onChange" :checked="syncStep.indexOf(3)>=0">
                    同步出库 >
                  </a-checkbox>
                </a-col>
                <a-col :span="6">
                  <a-checkbox :value="4" @change="onChange" :checked="syncStep.indexOf(4)>=0">
                    同步收款
                  </a-checkbox>
                </a-col>
              </a-row>
            </div>
          </div>
        </div>
        <!-- 按钮 -->
        <div class="btn-con">
          <a-button
            id="creatPurchaseReutn-cancel"
            size="large"
            class="button-cancel"
            @click="isShow=false"
            style="padding: 0 36px;">取消</a-button>
          <a-button
            type="primary"
            id="creatPurchaseReutn-save"
            size="large"
            :loading="spinning"
            class="button-primary"
            @click="handleSave"
            style="padding: 0 36px;margin-left: 15px;">确定</a-button>
        </div>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { createPurchaseReturnBill } from '@/api/purchaseReturnApply'
export default {
  name: 'CreatPurchaseReutnModal',
  components: { STable },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    paramsData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      disabled: false, //  查询、重置按钮是否可操作
      spinning: false,
      detail: null,
      syncStep: []
    }
  },
  methods: {
    onChange (e) {
      this.syncStep = []
      const checked = e.target.checked
      const len = checked ? e.target.value : e.target.value - 1
      for (let i = 1; i <= len; i++) {
        this.syncStep.push(i)
      }
    },
    setData (data) {
      this.detail = data
    },
    // 保存
    handleSave () {
      const params = {
        purchaseReturnApplySn: this.detail.purchaseReturnApplySn,
        syncStep: this.syncStep.length ? this.syncStep[this.syncStep.length - 1] : 0
      }
      this.spinning = true
      createPurchaseReturnBill(params).then(res => {
        if (res.status == 200) {
          if (res.data && res.data.length) {
            this.$info({
              title: '以下产品库存不足',
              centered: true,
              content: res.data.toString()
            })
          } else {
            this.$emit('creatOk')
            this.$message.success(res.message)
          }
        }
        this.$emit('close')
        this.spinning = false
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.detail = null
        this.syncStep = []
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .creatPurchaseReutn-modal{
    .creatPurchaseReutn-con{
      padding: 0 20px;
      .row-flex{
        > div{
          display:flex;
          align-items:center;
          padding: 10px 0;
          width:100%;
          &:last-child{
            flex-grow:1;
          }
          .col{
            width:50%;
          }
        }
      }
      .btn-con{
        text-align: center;
        margin: 30px 0 20px;
        .button-cancel{
          font-size: 12px;
        }
      }
    }
  }
</style>
