<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="usernamme">
              <j-popup
                style="max-width:none; width: 200%"
                v-model="model.usernamme"
                field="usernamme"
                org-fields="id,realname"
                dest-fields="userId,usernamme"
                code="findcust"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="城市" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cityId">
             <j-area-linkage style="max-width:none; width: 200%" type="cascader" v-model="model.cityId" placeholder="请输入省市区"  />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-textarea style="max-width:none; width: 200% ;height: 55px" v-model="model.address" rows="4" placeholder="请输入地址" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="邮编" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zipCode">
              <a-input style="max-width:none; width: 200%" v-model="model.zipCode" placeholder="请输入邮编"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="收货人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiver">
              <a-input style="max-width:none; width: 200%" v-model="model.receiver" placeholder="请输入收货人"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mobile">
              <a-input style="max-width:none; width: 200%" v-model="model.mobile" placeholder="请输入联系电话"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sort">
              <a-input-number style="max-width:none; width: 200%" v-model="model.sort" placeholder="请输入排序"  />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="标识" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="labelcl">
              <j-dict-select-tag style="max-width:none; width: 200%" type="radio" v-model="model.labelcl" dictCode="shdzbs" placeholder="请选择标识" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import store from '@/store'

  export default {
    name: 'CustomerReceivingAddressForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          mobile : store.getters.userInfo.phone,
          receiver : store.getters.userInfo.realname
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          zipCode: [
            { required: true, message: '请输入邮编!'},
            { pattern: /^[1-9]\d{5}$/, message: '请输入正确的邮政编码!'},
          ],
          receiver: [
            { required: true, message: '请输入收货人!'},
          ],
          mobile: [
            { required: true, message: '请输入联系电话!'},
            { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
          ],
          sort: [
            { required: false},
            { validator: (rule, value, callback) => validateDuplicateValue('customer_receiving_address', 'sort', value, this.model.id, callback)},
          ],
          labelcl: [
            { required: false},
            { validator: (rule, value, callback) => validateDuplicateValue('customer_receiving_address', 'labelcl', value, this.model.id, callback)},
          ],
        },
        url: {
          add: "/customerreceivingaddress/customerReceivingAddress/add",
          edit: "/customerreceivingaddress/customerReceivingAddress/edit",
          queryById: "/customerreceivingaddress/customerReceivingAddress/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      console.log(store.getters.userInfo)
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      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;
            })
          }

        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },
    }
  }
</script>