<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
	  draggable
      @close="close"
      @keyup.enter.native="doSubmit"
      v-model="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
               label-width="130px" @submit.native.prevent>
        <el-row  :gutter="15">
          <el-col :span="12">
            <el-form-item label="产品名称" prop="name"
                          :rules="[
                          {required: true, message:'产品名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写产品名称"></el-input>
            </el-form-item>
          </el-col>
			<el-col :span="12">
				<el-form-item label="排序" prop="sort"
							  :rules="[
                          {required: true, message:'排序不能为空', trigger:'blur'}
                 ]">
					<el-input-number v-model="inputForm.sort"
									 style="width:100%;"
									 :min="0"
									 placeholder="请填写排序"
					></el-input-number>
				</el-form-item>
			</el-col>


          <el-col :span="24">
            <el-form-item label="种类数量" prop="salesVolume"
                          :rules="[

                 ]">
              <el-input v-on:input="inputForm.salesVolume=inputForm.salesVolume.replace(/[^-0-9.]/g,'')
												.replace(/^\./g,'')
												.replace(/\.{2,}/g,'.')
												.replace('.','$#$').replace(/\./g,'').replace('$#$','.')
												.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')"
						v-model="inputForm.salesVolume" placeholder="请填写种类数量"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="销售额(万元)" prop="saleroom"
                          :rules="[

                 ]">
              <el-input v-on:input="inputForm.saleroom=inputForm.saleroom.replace(/[^-0-9.]/g,'')
												.replace(/^\./g,'')
												.replace(/\.{2,}/g,'.')
												.replace('.','$#$').replace(/\./g,'').replace('$#$','.')
												.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')"
						v-model="inputForm.saleroom" placeholder="请填写产品销售额"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="月销售额(万元)" prop="monthlySalesVolume"
                          :rules="[

                 ]">
              <el-input v-on:input="inputForm.monthlySalesVolume=inputForm.monthlySalesVolume.replace(/[^-0-9.]/g,'')
												.replace(/^\./g,'')
												.replace(/\.{2,}/g,'.')
												.replace('.','$#$').replace(/\./g,'').replace('$#$','.')
												.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')"
						v-model="inputForm.monthlySalesVolume" placeholder="请填写产品月销售额"></el-input>
            </el-form-item>
          </el-col>



          <el-col :span="24">
            <el-form-item label="备注" prop="remarks"
                          :rules="[
                 ]">
              <el-input type="textarea" maxlength="255" v-model="inputForm.remarks" placeholder="请填写备注"  show-word-limit   ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
		<template #footer>
			<span class="dialog-footer">
			  <el-button @click="close()" icon="el-icon-circle-close">关闭</el-button>
			  <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
			</span>
		</template>
    </el-dialog>
  </div>
</template>

<script>
	import merchantProductService from '@/api/wdt/merchantProduct/MerchantProductService'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
			name: '',
			salesVolume: '',
			saleroom: '',
			monthlySalesVolume: '',
			sort: 0,
         	remarks: '',
        }
      }
    },
    created () {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm = {
          name: '',
			salesVolume: '',
			saleroom: '',
			monthlySalesVolume: '',
          sort: '',
          remarks: '',
          parentId: ''
        }
        if (method === 'add') {
          this.title = `新增产品信息`
        } else if (method === 'edit') {
          this.inputForm.id = id
          this.title = '修改产品信息'
        } else if (method === 'view') {
          this.inputForm.id = id
          this.title = '查看产品信息'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
			  merchantProductService.queryById(this.inputForm.id).then((data) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
			  merchantProductService.save(this.inputForm).then((data) => {
              this.close()
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      close () {
        this.$refs.inputForm.resetFields()
        this.visible = false
      }
    }
  }
</script>
