<template>
  <div>
    <a-modal
      centered
      wrapClassName="addGoodsShelve-modal"
      :footer="null"
      :maskClosable="false"
      :title="titleText"
      v-model="isshow"
      @cancle="isshow=false"
      :width="800">
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="addGoodsShelve-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-row :gutter="24">
            <a-col :span="12">
              <a-form-model-item label="配件经销商" prop="dealerSn">
                <dealerList ref="dealerList" @change="dealerChange" v-if="!nowData?true:false" id="addGoodsShelve-dealerChange"></dealerList>
                <span v-else>{{ form.dealerName }}</span>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="汽车修理厂" prop="storeSn">
                <storeList ref="storeList" @change="storeChange" v-if="!nowData?true:false" id="addGoodsShelve-storeChange"></storeList>
                <span v-else>{{ form.storeName }}</span>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="20">
            <a-col :span="24">
              <a-form-model-item label="货架名称" prop="shelfName" :label-col="{span: 3}" :wrapper-col="{span: 20}">
                <a-input id="addGoodsShelve-shelfName" v-model.trim="form.shelfName" :maxLength="30" allowClear placeholder="请输入货架名称(最多30个字符)"/>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <!-- 状态 -->
            <a-col :span="12">
              <a-form-model-item label="货架归属" prop="ownerType">
                <a-radio-group name="radioGroup" v-model="form.ownerType" @change="changeRadio" id="addGoodsShelve-changeRadio">
                  <a-radio :value="1">平台</a-radio>
                  <a-radio :value="0">自有</a-radio>
                </a-radio-group>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24" v-if="form.ownerType===0">
            <a-col :span="24">
              <a-form-model-item label="货架图片" prop="images" help="说明：请上传清晰、完整的图片，支持jpg/jpeg/png格式，不超过500KB(最多5张)。" :label-col="{span:3}" :wrapper-col="{span:20}">
                <Upload
                  class="upload"
                  id="addGoodsShelve-images"
                  v-model="form.images"
                  :fileSize="500/1024"
                  ref="images"
                  @change="changeImage"
                  :maxNums="5"
                  listType="picture-card" ></Upload>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col :span="24">
              <a-form-model-item label="备注" prop="remarks" :label-col="{span: 3}" :wrapper-col="{span: 20}">
                <a-textarea id="addGoodsShelve-remarks" v-model.trim="form.remarks" :maxLength="500" allowClear placeholder="请输入备注(最多500个字符)"/>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-form-model-item :label-col="{span: 0}" :wrapper-col="{span: 24}" style="text-align: center;">
            <a-button type="primary" @click="handleSubmit()" :style="{ marginRight: '8px' }" id="addGoodsShelve-handleSubmit">保存</a-button>
            <a-button :style="{ marginLeft: '8px' }" @click="isshow=false" id="addGoodsShelve-close">取消</a-button>
          </a-form-model-item>
        </a-form-model>
      </a-spin>
    </a-modal>

  </div>
</template>

<script>
import { STable, VSelect, Upload } from '@/components'
import { saveShelf, shelfDetail } from '@/api/shelf.js'
import dealerList from '@/views/common/dealerList.vue'
import storeList from '@/views/common/storeList.vue'
export default {
  name: 'AddGoodsShelve',
  components: { STable, VSelect, Upload, dealerList, storeList },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    nowData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },

  data () {
    return {
      spinning: false,
      titleText: '新增数字货架',
      isshow: this.visible,
      roleList: [],
      pageInfo: null,
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      form: {
        dealerSn: undefined, // 经销商sn
        ownerType: 1, // 平台
        storeSn: undefined, // 汽车修理厂sn
        shelfName: '', // 货架名称
        images: '', // 图片
        remarks: ''// 备注
      },
      rules: {
        shelfName: [ { required: true, message: '请输入用户名称', trigger: 'blur' }, { pattern: '^[^<|>]{1,30}$', message: '请输入不含<或>的字符，最多30个字符' } ],
        dealerSn: [ { required: true, message: '请选择配件经销商', trigger: 'change' } ],
        ownerType: [ { required: true, message: '请选择货架归属', trigger: 'change' } ],
        storeSn: [ { required: true, message: '请选择汽车修理厂', trigger: 'change' } ],
        images: [{ required: true, message: '请上传品牌图片', trigger: 'change' } ]
      }
    }
  },
  methods: {
    // 经销商 change
    dealerChange (obj) {
      this.form.dealerSn = obj.key || undefined
    },
    // 汽车修理厂 change
    storeChange (obj) {
      this.form.storeSn = obj.key || undefined
    },
    // 产品图片上传
    changeImage (file) {
      this.form.images = file
    },
    // 改变货架归属
    changeRadio (e) {
      this.form.ownerType = e.target.value
      console.log(e.target.value, '=========')
    },
    // 查询货架详情
    getShelfDetail () {
      this.spinning = true
      shelfDetail({ shelfSn: this.nowData.shelfSn }).then(res => {
        if (res.status == 200) {
          this.form = Object.assign({}, res.data)
          this.form.ownerType = Number(this.form.ownerType)
          if (this.form.ownerType == 0) {
            this.$nextTick(() => {
              this.$refs.images.setFileList(this.form.images)
            })
          }
        } else {
          this.form = {}
          this.$refs.images.setFileList('')
        }
        this.spinning = false
      })
    },
    // 保存提交
    handleSubmit () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(_this.form))
          if (params.images == '') {
            delete params.images
          }
          if (_this.nowData && _this.nowData.id) {
            params.id = _this.nowData.id
          }
          _this.spinning = true
          saveShelf(params).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('refresh')
              setTimeout(function () {
                _this.isshow = false
              }, 300)
            }
            _this.spinning = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  watch: {
    visible (newValue, oldValue) {
      this.isshow = newValue
    },
    isshow (newValue, oldValue) {
      if (!newValue) {
        this.form = {}
        this.form.ownerType = 1
        if (!this.nowData) {
          this.$refs.storeList.resetForm()
          this.$refs.dealerList.resetForm()
        }
        this.$refs.ruleForm.resetFields()
        this.titleText = '新增数字货架'
        this.$emit('close')
      } else {
        if (this.nowData && this.nowData.id) { //  编辑
          console.log(this.nowData.id, 'this.nowData.id')
          this.titleText = '编辑数字货架'
          this.getShelfDetail()
        }
      }
    }
  }
}
</script>
<style lang="less">

</style>
