<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="面料类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeId">
              <j-popup
                v-model="model.typeName"
                field="typeName"
                org-fields="nums,content"
                dest-fields="typeId,typeName"
                code="zy_fabric_type"
                :multi="false"
                @input="popupCallback"
              />
              <a-input v-model="model.typeId" hidden />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="材质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="material">
              <j-search-select-tag v-model="model.material" dict="material" @change="gaibian" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fabricNumber">
              <a-input v-model="model.fabricNumber" placeholder="请输入编号"  disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="颜色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="color">
              <a-input v-model="model.color" placeholder="请输入颜色"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="花样" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pattern">
              <j-search-select-tag v-model="model.pattern" dict="pattern"  />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="材质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="material">-->
<!--              <j-search-select-tag v-model="model.material" dict="material"  />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">-->
<!--              <a-input v-model="model.description" placeholder="请输入描述"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="厚度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ply">
              <j-search-select-tag v-model="model.ply" dict="ply"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="money">
              <a-input-number v-model="model.money" placeholder="请输入单价" :precision="2" :min="0" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="零售价（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="retailPrice">
              <a-input-number v-model="model.retailPrice" placeholder="请输入零售价" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单位（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <j-dict-select-tag v-model="model.unit" type="radioButton" title="单位" dictCode="unit1" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="品牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="brandId">
              <j-search-select-tag v-model="model.brandId" dict="brand"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="密度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="density">
              <j-search-select-tag v-model="model.density" dict="density"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="幅宽（米）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="clothW">
              <j-search-select-tag v-model="model.clothW" dict="clothW"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="销售地区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="location">
              <a-input v-model="model.location" placeholder="请输入销售地区"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag v-model="model.status" type="radioButton" title="状态" dictCode="fabric_status" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="等级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fabricGrade">-->
<!--              <j-search-select-tag v-model="model.fabricGrade" dict="fabric_grade"  />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="供货商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierInfo">
              <j-select-depart v-model="model.supplierInfo" multi  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imageUrl">
              <j-image-upload isMultiple  v-model="model.imageUrl" ></j-image-upload>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="图片地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imageUrl">-->
<!--              &lt;!&ndash;<j-image-upload isMultiple  v-model="model.imageUrl" ></j-image-upload>&ndash;&gt;-->

<!--              <img style="width: 100%;" :src="model.imageUrl">-->
<!--              <a-input hidden="hidden"  v-model="model.imageUrl"></a-input>-->
<!--              <a-button type="primary" @click="onlineImage">-->
<!--              在线-->
<!--              </a-button>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="二维码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qRcode">
              <j-image-upload isMultiple  v-model="model.qRcode" ></j-image-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
        <online-camera ref="onlineCamera"  v-on:changeOnlineVisible="changeOnlineVisible" />
      </a-form-model>

    </j-form-container>
  </a-spin>

</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import OnlineCamera from './Components/OnlineCamera'
  import pick from 'lodash.pick'

  export default {
    name: 'ZyFabricForm',
    components: {
      OnlineCamera
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          unit : '1',
          status : '1',
          fabricNumber:'',
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          typeId: [
            { required: true, message: '请选择类型!'},
          ],
          color: [
            { required: true, message: '请输入颜色!'},
          ],
          pattern: [
            { required: true, message: '请输入花样!'},
          ],
          fabricNumber: [
            { required: true, message: '请输入面料编号!'},
          ],
          name: [
            { required: true, message: '请输入名称!'},
          ],
          money: [
            { required: true, message: '请输入单价!'},
            {pattern: /^\d+$/,message:'请输入正整数'}
          ],
          retailPrice: [
            { required: true, message: '请输入零售价!'},
            {pattern: /^\d+$/,message:'请输入正整数'}
          ],
          // money: [
          //   { required: false},
          //   { pattern: /^\d{0,11}$/, message: '长度不能超过11位数字'},
          // ],
          // retailPrice: [
          //   { required: false},
          //   { pattern: /^\d{0,11}$/, message: '长度不能超过11位数字'},
          // ],
          location: [
            { required: false},
            { pattern: /^.{0,30}$/, message: '长度不能超过30位字符'},
          ],
        },
        url: {
          add: "/fabric/zyFabric/add",
          edit: "/fabric/zyFabric/edit",
          queryById: "/fabric/zyFabric/queryById",
          sort:"/fabric/zyFabric/sort"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      console.log(this.model)
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      gaibian(){
        var n=10;
        console.log(this.model)
        getAction(this.url.sort,this.model).then((res)=>{
            console.log(res)
            var num=parseInt(res.result,10)+1
            num=num.toString()
            while(num.length<n){
              num="0"+num
            }
            this.model.fabricNumber="ML"+this.model.typeId+this.model.material+num;
            console.log(this.model.typeId)
      })
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },

      // 在线
      onlineImage(){
        this.$refs.onlineCamera.showDialog()
      },
      changeOnlineVisible(value){
        this.model.imageUrl=value;
      },
      popupCallback(value,row){
        this.model = Object.assign(this.model, row);
      },


    }
  }
</script>