<template>
  <a-modal
    centered
    class="purchaseOrder-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    title="新增采购退货单"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="purchaseOrder-basicInfo-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol" >
        <a-form-model-item label="供应商" prop="returnTargetSn">
          <a-radio-group
            name="radioGroup"
            v-model="form.returnTargetSn"
            @change="tragetTypeChange"
            v-if="purchaseTragetType.length"
            id="purchaseOrder-basicInfo-returnTargetSn" >
            <a-radio v-for="item in purchaseTragetType" :key="item.purchaseTargetSn" :value="item.purchaseTargetSn" class="radio-s">{{ item.purchaseTargetName }}</a-radio>
          </a-radio-group>
          <span v-else>没有可选择的供应商</span>
        </a-form-model-item>
        <a-form-model-item label="是否抓单" prop="grabFlag">
          <v-select
            code="FLAG"
            showType="radio"
            id="purchaseOrder-grabFlag"
            v-model="form.grabFlag"
            @change="changeGrabFlag"
            allowClear
            placeholder="请选择"></v-select>
        </a-form-model-item>
        <a-form-model-item :wrapper-col="{ span: 12, offset: 6 }" style="text-align: center;">
          <a-button type="primary" :loading="confirmLoading" @click="handleSubmit" id="chooseCustom-btn-submit">保存</a-button>
          <a-button @click="isShow=false" style="margin-left: 15px" id="chooseCustom-btn-back">取消</a-button>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { VSelect } from '@/components'
import { purchaseTargetList } from '@/api/purchase'
import { purchaseReturnSaveByNoSync } from '@/api/purchaseReturn'
export default {
  name: 'BasicInfoModal',
  components: { VSelect },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false,
      confirmLoading: false,
      isShow: this.openModal, //  是否打开弹框
      settleStyleList: [], // 支付方式
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        returnTargetType: '', // 供应商
        returnTargetSn: undefined,
        returnTargetName: '',
        grabFlag: '1'
      },
      rules: {
        returnTargetSn: [
          { required: true, message: '请选择供应商', trigger: 'change' }
        ],
        grabFlag: [
          { required: true, message: '请选择抓单类型', trigger: 'change' }
        ]
      },
      purchaseTragetType: []
    }
  },
  mounted () {
    this.getParentDealer()
  },
  methods: {
    changeGrabFlag (e) {
      console.log(e)
    },
    resetForm () {
      this.$refs.ruleForm.resetFields()
      this.form.returnTargetType = ''
      this.form.returnTargetSn = undefined
      this.form.returnTargetName = ''
      this.form.grabFlag = '1'
    },
    // 供应商change
    tragetTypeChange (e) {
      const val = e.target.value
      const ind = this.purchaseTragetType.findIndex(item => item.purchaseTargetSn == val)
      if (ind != -1) {
        this.form.returnTargetType = this.purchaseTragetType[ind].purchaseTargetType
        this.form.returnTargetName = this.purchaseTragetType[ind].purchaseTargetName
      }
    },
    //  保存
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.spinning = true
          purchaseReturnSaveByNoSync(_this.form).then(res => {
            if (res.status == 200) {
              _this.isShow = false
              _this.$emit('ok', res.data)
              _this.spinning = false
              _this.$message.info(res.message)
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    },
    // 编辑信息
    setData (data) {
      this.form = JSON.parse(JSON.stringify(data))
    },
    getParentDealer () {
      const zb = this.$hasPermissions('B_PR_outSnycZB')
      const sj = this.$hasPermissions('B_PR_outSnycSJ')
      let params = {}
      // 只能向上级退货
      if (sj) {
        params.purchaseTargetType = 'DEALER_UP'
      }
      // 只能向总部退货
      if (zb) {
        params.purchaseTargetType = 'SUPPLIER_SYS'
      }
      // 总部和上级都可以
      if (sj && zb) {
        params = {}
      }
      console.log(params)
      purchaseTargetList(params).then(res => {
        if (res.status == 200 && res.data[0]) {
          this.purchaseTragetType = res.data
          this.$emit('load', res.data)
        } else {
          this.purchaseTragetType = []
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetForm()
      } else {
        this.getParentDealer()
      }
    }
  }
}
</script>

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