<template>
<view class="customer-form">
  <u-navbar
        :title="id?'编辑客商':'新增客商'"
        :autoBack="true"
        :placeholder="true"
      >
      </u-navbar>
  <u-form 
        labelPosition="left"
				:model="form"
				:rules="rules"
				ref="form1"
        labelWidth='90'>
    <u-form-item :label="$t('customer_meter.enterpriseTaxNo')" borderBottom prop="taxNo">
      <u--input
          v-model="form.taxNo"
          border="none"
          :placeholder="$t('base.common.placeholderI')+$t('customer_meter.enterpriseTaxNo')"
      ></u--input>
    </u-form-item>
    <u-form-item :label="$t('customer_meter.enterpriseName')" borderBottom prop="name">
      <u--input
          v-model="form.name"
          border="none"
          :placeholder="$t('base.common.placeholderI')+$t('customer_meter.enterpriseName')"
      ></u--input>
    </u-form-item>
    <u-form-item :label="$t('customer_meter.contacts')" borderBottom prop="companyContact">
      <u--input
          v-model="form.companyContact"
          border="none"
         :placeholder="$t('base.common.placeholderI')+$t('customer_meter.contacts')"
      ></u--input>
    </u-form-item>
    <u-form-item :label="$t('customer_meter.contactNumber')" borderBottom prop="companyContactPhone">
      <u--input
          v-model="form.companyContactPhone"
          border="none"
          :placeholder="$t('base.common.placeholderI')+$t('customer_meter.contactNumber')"
      ></u--input>
    </u-form-item>
    <u-form-item :label="$t('customer_meter.customerAttribute')" borderBottom prop="types">
      <u-radio-group 
          v-model="form.types"
          placement="row">
        <u-radio activeColor="#4680F9" name="102" :label="$t('customer_meter.sale')"></u-radio>
        <u-radio activeColor="#4680F9" name="104" :label="$t('customer_meter.purchase')"></u-radio>
      </u-radio-group>
    </u-form-item>
    <u-form-item :label="$t('customer_meter.private')"  borderBottom prop="privateFlag">
      <u-switch v-model="form.privateFlag" activeColor="#4680F9" size="20"></u-switch>
    </u-form-item>
    <u-form-item :label="$t('customer_meter.enterpriseAddress')" borderBottom prop="address">
      <u--input
          v-model="form.address"
          border="none"
          :placeholder="$t('base.common.placeholderI')+$t('customer_meter.enterpriseAddress')+'(选填)'"
      ></u--input>
    </u-form-item>
    <div style="margin: 16px 0;">
      <u-button type="primary" native-type="submit" @click="submit('form1')">{{$t('base.common.submit')}}</u-button>
    </div>
  </u-form>
</view>
  
</template>

<script>
import {addCustomer,editCustomer,getCustomerById} from '../../api/customer'
export default {
  data(){
    return {
      id:'',
      form:{
        taxNo:'',
        name:'',
        companyContact:'',
        companyContactPhone:'',
        types:'102',
        privateFlag:true,
        address:'',
      },
      rules:{
        name:[{ required: true, message: this.$t('base.common.placeholderI')+this.$t('customer_meter.enterpriseName') }]
      }
    }
  },
 onLoad(){
  this.getCustomer()
 },
 methods:{
  //  onClickLeft(){
  //   uni.navigateBack()
  // },
  getCustomer(){
    let _this=this
    const routers = getCurrentPages()
    _this.id = routers[routers.length - 1].options?routers[routers.length - 1].options.id:''
    if(_this.id){
      getCustomerById(_this.id).then(res=>{
        if(res.statusCode==200){
          let info=res.data.data
          _this.form.taxNo=info.taxNo
          _this.form.name=info.name
          _this.form.companyContact=info.companyContact
          _this.form.companyContactPhone=info.companyContactPhone
          _this.form.types=info.types[0]
          _this.form.privateFlag=info.privateFlag=='1'?true:false
          _this.form.address=info.address
        }
      })
    }
  },
  submit(ref){
    let _this=this
    this.$refs[ref].validate().then(()=>{
      let data = JSON.parse(JSON.stringify(_this.form))
      data.privateFlag ? data.privateFlag='1':data.privateFlag='0'
      data.types = [data.types]
      if(_this.id){
        data.id=_this.id
        console.log('编辑');
         editCustomer(data).then(res=>{
          if(res.statusCode == 200 ){
            uni.showModal({
							showCancel: false,
            // title: '提示',
            content: _this.$t("customer_meter.editOk"),
            success:function(res){
              if(res.confirm){
                uni.navigateBack({ delta: 1 })
              }
            }
            })
            _this.$refs[ref].resetFields()
          }
        })
      }else{
        console.log('新增');
        addCustomer(data).then(res=>{
          if(res.statusCode == 200 ){
            uni.showModal({
							showCancel: false,
            // title: '提示',
            content: _this.$t("customer_meter.addOk"),})
            _this.$refs[ref].resetFields()
          }
        })
      }
      
    })
    .catch(err=>{
      console.log('err', err);
    })
  }
 }
}
</script>

<style lang="less" scoped>
.customer-form{
  padding: 0 32rpx 32rpx;
  background-color: #fff;
}
/deep/ .u-form-item__body{
  padding: 40rpx 0 20rpx;
}
/deep/ .u-form-item__body__left__content__label{
  font-weight: 600;
}
/deep/ .u-button--primary{
  background-color: #4680F9;
  border-color: #4680F9;
}
</style>