<template>
  <a-modal
    centered
    class="purchaseReturn-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    :title="itemSn?'编辑':'新增'"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="purchaseReturn-basicInfo-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol" >
        <a-form-model-item label="供应商名称" prop="supplierSn">
          <supplier v-model="form.supplierSn" isPermission :disabledFlag="itemSn?true:false" placeholder="请输入供应商名称"></supplier>
        </a-form-model-item>
        <a-form-model-item label="退货原因" prop="returnReason">
          <v-select
            v-model="form.returnReason"
            ref="returnReason"
            id="purchaseReturn-basicInfo-returnReason"
            code="SPARE_PARTS_RETURN_REASON"
            placeholder="请选择退货原因"
            allowClear></v-select>
        </a-form-model-item>
        <a-form-model-item label="退货仓库" prop="warehouseSn" v-show="isShowWarehouse">
          <warehouse
            v-model="form.warehouseSn"
            id="purchaseReturn-basicInfo-warehouseSn"
            :disabled="itemSn?true:false"
            isPermission
            placeholder="请选择退货仓库"
          />
        </a-form-model-item>
        <a-form-model-item label="是否抓单" prop="grabFlag">
          <a-radio-group v-model="form.grabFlag" :disabled="itemSn?true:false">
            <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="explainInfo">
          <a-textarea
            id="purchaseReturn"
            :maxLength="60"
            v-model="form.explainInfo"
            placeholder="具体描述下退货原因"
            allowClear />
        </a-form-model-item>
        <a-form-model-item label="附件" help="（支持图片、word、excel、PDF等格式，最多10个附件）">
          <Upload
            style="height: 100%;"
            id="allocateBill-attachList"
            v-model="form.attachmentList"
            ref="attachList"
            :fileSize="10"
            :maxNums="10"
            fileType="*"
            uploadType="attach"
            :action="attachAction"
            @change="changeAttach"
            upText="上传附件"></Upload>
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button id="purchaseReturn-basicInfo-modal-back" @click="isShow = false">取消</a-button>
        <a-button style="margin-left: 15px;" type="primary" id="purchaseReturn-modal-save" @click="handleSave">确定</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { VSelect, Upload } from '@/components'
import supplier from '@/views/common/supplier.js'
import warehouse from '@/views/common/chooseWarehouse.js'
import { sparePartsReturnSave, sparePartsReturnInfo } from '@/api/sparePartsReturn'
export default {
  name: 'PurchaseReturnBasicInfoModal',
  mixins: [commonMixin],
  components: { VSelect, Upload, supplier, warehouse },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      formItemLayout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 16 }
      },
      form: {
        supplierSn: undefined, // 供应商
        returnReason: '', // 退货原因
        explainInfo: '', // 补充说明
        warehouseSn: undefined,
        attachmentList: '',
        grabFlag: 1
      },
      attachList: [], // 附件
      rules: {
        supplierSn: [
          { required: true, message: '请选择供应商名称', trigger: 'change' }
        ],
        returnReason: [
          { required: true, message: '请选择退货原因', trigger: 'change' }
        ],
        warehouseSn: [{ required: true, message: '请选择退货仓库', trigger: 'change' }],
        grabFlag: [{ required: true, message: '请选择是否抓单', trigger: 'change' }]
      },
      attachAction: process.env.VUE_APP_API_BASE_URL + '/uploadGetFileInfo'
    }
  },
  methods: {
    //  附件上传
    changeAttach (file) {
      this.attachList = JSON.parse(file)
      this.attachList.map(item => {
        item.fileType = item.extName
      })
    },
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          form.attachmentList = _this.attachList
          _this.spinning = true
          sparePartsReturnSave(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 {
          return false
        }
      })
    },
    // 获取编辑详情
    getDetail () {
      sparePartsReturnInfo({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          const resultObj = res.data
          this.attachList = resultObj.attachmentList
          const obj = {
            supplierSn: resultObj.supplierSn,
            returnReason: resultObj.returnReason, // 退货原因
            explainInfo: resultObj.explainInfo, // 补充说明
            warehouseSn: resultObj.warehouseSn,
            sparePartsReturnSn: this.itemSn,
            grabFlag: Number(resultObj.grabFlag)
          }
          this.$nextTick(() => {
            this.$refs.attachList.setFileList(this.attachList)
          })
          this.form = { ...this.form, ...obj }
        } else {
          this.detailsData = null
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.attachList = []
        this.supplierName = null
        this.$nextTick(() => {
          this.$refs.attachList.setFileList('')
          this.$refs.ruleForm.resetFields()
        })
      } else {
        if (this.itemSn) {
          this.getDetail()
        } else {
          // 默认仓库
          this.form.warehouseSn = this.isShowWarehouse ? undefined : this.defWarehouse && this.defWarehouse.warehouseSn
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .purchaseReturn-basicInfo-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
    .ant-form-item:nth-child(5){
      margin-bottom: 0px !important;
    }
  }

</style>
