<template>
  <a-modal
    centered
    class="bulkReturnGoods-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    title="新增散件退货单"
    v-model="isShow"
    @cancle="isShow = false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="bulkReturnGoods-basicInfo-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol">
        <a-form-model-item label="供应商" prop="supplierSn">
          <a-select
            placeholder="请输入名称查询，如果没有请点击 '+' 新增"
            allowClear
            v-model="form.supplierSn"
            :showSearch="true"
            option-filter-prop="children"
            :filter-option="filterOption"
            style="width: 80%;">
            <a-select-option v-for="item in supplierList" :key="item.supplierSn" :value="item.supplierSn" :disabled="item.enabledFlag==0">{{ item.supplierName }}</a-select-option>
          </a-select>
          <a-button icon="plus" size="small" @click="openSupplierModal=true" id="bulkWarehousingOrder-basicInfo-add-btn" style="margin-left: 10px;"></a-button>
        </a-form-model-item>
        <a-form-model-item label="是否抓单" prop="isGrab">
          <a-radio-group v-model="form.isGrab">
            <a-radio value="1">是</a-radio>
            <a-radio value="0">否</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="备注" prop="remark">
          <a-textarea id="bulkReturnGoods-basicInfo-remark" :maxLength="120" v-model="form.remark" placeholder="请输入备注(最多120个字符)" allowClear />
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button type="primary" id="bulkReturnGoods-basicInfo-modal-save" @click="handleSave">保存</a-button>
        <a-button id="bulkReturnGoods-basicInfo-modal-back" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
      </div>
    </a-spin>
    <!-- 供应商 -->
    <supplier-info-edit-modal :openModal="openSupplierModal" @close="closeSupplierModal" @ok="handleOk" :isState="false" />
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { VSelect } from '@/components'
import supplierInfoEditModal from '@/views/supplierManagement/supplierInfo/editModal.vue'
import { supplierAllList } from '@/api/supplier'
import { sparePartsRetSave } from '@/api/sparePartsRet'
export default {
  name: 'BulkReturnGoodsBasicInfoModal',
  components: { VSelect, supplierInfoEditModal },
  mixins: [commonMixin],
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        supplierSn: undefined,
        isGrab: '1',
        remark: ''
      },
      rules: {
        supplierSn: [ { required: true, message: '请选择供应商', trigger: 'change' } ],
        isGrab: [ { required: true, message: '请选择是否抓单', trigger: 'change' } ]
      },
      supplierList: [], //  供应商  下拉数据
      openSupplierModal: false //  供应商 弹框
    }
  },
  methods: {
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          _this.spinning = true
          sparePartsRetSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.isShow = false
                _this.$emit('ok', res.data)
                _this.spinning = false
              }, 1000)
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    filterOption (input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    // 新增供应商 成功
    handleOk () {
      //  获取供应商数据列表，并赋值
      this.getSupplierList('val')
    },
    // 关闭供应商弹框
    closeSupplierModal () {
      this.openSupplierModal = false
    },
    // 供应商下拉数据
    getSupplierList (val) {
      supplierAllList().then(res => {
        if (res.status == 200) {
          this.supplierList = res.data
          if (val) { //  需将新增加的供应商回填，即不需要用户再选一下
            this.form.supplierSn = this.supplierList[0].supplierSn
            this.$refs.ruleForm.clearValidate('supplierSn')
          }
        } else {
          this.supplierList = []
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
      } else {
        this.getSupplierList()
      }
    }
  }
}
</script>

<style lang="less">
.bulkReturnGoods-basicInfo-modal {
  .ant-modal-body {
    padding: 40px 40px 24px;
  }
  .btn-cont {
    text-align: center;
    margin: 35px 0 10px;
  }
}
</style>
