<template>
  <a-modal
    centered
    class="shelfSet-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    title="数字货架基础设置"
    v-model="isShow"
    @cancel="isShow = false"
    :width="600">
    <a-spin :spinning="spinning" tip="Loading...">
      <p v-if="type==0" style="text-align: center;margin: 0 0 20px;font-size: 14px;font-weight: bold;">{{ nowData && nowData.shelfName || '--' }}</p>
      <a-form-model
        id="shelfSet-basicInfo-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol">
        <a-form-model-item label="货架名称" v-if="type==1" prop="shelfName">
          <a-input v-model="form.shelfName" :maxLength="30" placeholder="请输入货架名称"></a-input>
        </a-form-model-item>
        <a-form-model-item prop="customerSn">
          <template slot="label">
            <a-tooltip placement="top">
              <template slot="title">
                <span>此数字货架，归属于您的哪一位客户。如果没有搜索到客户，请在客户管理功能中新建客户。</span>
              </template>
              关联客户<a-icon type="question-circle" style="color: rgba(0,0,0,.65);font-size: 16px;margin-left: 2px;vertical-align: sub;cursor: pointer;" />
            </a-tooltip>
          </template>
          <custList ref="custList" :isEnabled="true" @change="custChange"></custList>
        </a-form-model-item>
        <!-- <a-form-model-item prop="showPrice">
          <template slot="label">
            <a-tooltip placement="top">
              <template slot="title">
                1、非铺货产品，是指不是货架上的产品<br>
                2、终端会员价，对于汽修厂来说，即进货价<br>
                3、如果该产品在销售单里有销售记录，则使用最近一次销售价，如果没有销售记录，则使用箭冠总公司的终端会员价
              </template>
              价格显示<a-icon type="question-circle" style="color: rgba(0,0,0,.65);font-size: 16px;margin-left: 2px;vertical-align: sub;cursor: pointer;" />
            </a-tooltip>
          </template>
          <a-select v-model="form.showPrice" placeholder="请选择价格显示">
            <a-select-option value="1">
              非铺货产品——显示价格
            </a-select-option>
            <a-select-option value="0">
              非铺货产品——不显示价格
            </a-select-option>
          </a-select>
        </a-form-model-item> -->
      </a-form-model>
      <div class="btn-cont">
        <a-button type="primary" id="shelfSet-basicInfo-modal-save" @click="handleSave">保存</a-button>
        <a-button id="shelfSet-basicInfo-modal-back" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { VSelect } from '@/components'
import custList from '@/views/common/custList.vue'
// , updateShelfPriceShow
import { shelfSave } from '@/api/shelf'
export default {
  name: 'ShelfSetBasicInfoModal',
  components: { VSelect, custList },
  mixins: [commonMixin],
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    },
    nowData: {
      type: Object,
      default: () => {
        return {}
      }
    },
    type: {
      type: [String, Number],
      default: '1'
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 17 }
      },
      form: {
        customerSn: undefined,
        shelfName: ''
        // showPrice: undefined
      },
      rules: {
        shelfName: [{ required: true, message: '请输入货架名称', trigger: 'change' }],
        customerSn: [{ required: true, message: '请选择关联客户', trigger: 'change' }]
        // showPrice: [{ required: true, message: '请选择价格显示', trigger: 'change' }]
      }
    }
  },
  methods: {
    // 客户 change
    custChange (obj, row) {
      this.form.customerSn = row && row.customerSn || undefined
    },
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          form.shelfSn = _this.nowData && _this.nowData.shelfSn
          _this.spinning = true
          shelfSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.isShow = false
                _this.$emit('ok', res.data)
                _this.spinning = false
              }, 100)
              // 更新价格显示
              // updateShelfPriceShow({
              //   shelfSn: form.shelfSn,
              //   paramValue: form.showPrice
              // }).then(ret => {
              //   if (ret.status == 200) {
              //     _this.$message.success(ret.message)
              //     setTimeout(() => {
              //       _this.isShow = false
              //       _this.$emit('ok', res.data)
              //       _this.spinning = false
              //     }, 100)
              //   } else {
              //     _this.spinning = false
              //   }
              // })
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
        this.$refs.custList.resetForm()
      } else {
        const _this = this
        this.$nextTick(() => {
          _this.form.shelfName = _this.nowData.shelfName
          _this.form.showPrice = _this.nowData.showPrice
          const custome = _this.nowData.customerEntity
          if (custome) {
            _this.form.customerSn = custome.customerSn || ''
            _this.$refs.custList.fetchUser(custome.customerName || '')
            _this.$refs.custList.dealerName = {
              key: custome.customerSn,
              label: custome.customerName,
              row: _this.nowData
            }
          }
        })
      }
    }
  }
}
</script>

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