<template>
    <view>
        <uni-section title="抬头类型" type="line">
            <view class="uni-list">
                <radio-group @change="radioChange">
                    <label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value">
                        <view>
                            <radio :value="item.value" :checked="index === current"/>
                        </view>
                        <view style="flex: 1;">{{item.name}}</view>
                    </label>
                </radio-group>
            </view>
        </uni-section>

        <!--个人-->
        <uni-section title="发票详情" type="line" v-if="current==0">
            <view class="paddinglr30 margin-top-30">
                <!-- 包含校验规则 -->
                <uni-forms ref="baseFormPerson" :rules="rulesPerson" :modelValue="baseFormDataPerson">
                    <uni-forms-item label="发票抬头" name="invoicetitle" required>
                        <uni-easyinput v-model="baseFormDataPerson.invoicetitle" placeholder="请输入发票抬头"/>
                    </uni-forms-item>

                    <uni-forms-item label="手机号" name="phone" required>
                        <uni-easyinput v-model="baseFormDataPerson.phone" placeholder="请输入手机号"/>
                    </uni-forms-item>

                    <uni-forms-item label="电子邮箱" name="email" required>
                        <uni-easyinput v-model="baseFormDataPerson.email" placeholder="请输入电子邮箱"/>
                    </uni-forms-item>

                    <uni-forms-item label="备注信息">
                        <uni-easyinput type="textarea" v-model="baseFormDataPerson.remark" placeholder="备注"/>
                    </uni-forms-item>
                </uni-forms>
            </view>
        </uni-section>


        <!--企业-->
        <uni-section title="发票详情" type="line" v-if="current==1">
            <view class="paddinglr30 margin-top-30">
                <!-- 基础用法，不包含校验规则 -->
                <uni-forms ref="baseForm" :rules="rules" :modelValue="baseFormData">
                    <uni-forms-item label="发票抬头" name="invoicetitle" required>
                        <uni-easyinput v-model="baseFormData.invoicetitle" placeholder="请输入发票抬头"/>
                    </uni-forms-item>
                    <uni-forms-item label="纳税人识别号" name="userId" required>
                        <uni-easyinput v-model="baseFormData.userId" placeholder="请输入纳税人识别号"/>
                    </uni-forms-item>
                    <uni-forms-item label="注册地址">
                        <uni-easyinput v-model="baseFormData.address" placeholder="请输入注册地址"/>
                    </uni-forms-item>
                    <uni-forms-item label="注册电话" name="phone" required>
                        <uni-easyinput v-model="baseFormData.phone" placeholder="请输入注册电话"/>
                    </uni-forms-item>
                    <uni-forms-item label="开户行">
                        <uni-easyinput v-model="baseFormData.bank" placeholder="请输入开户行"/>
                    </uni-forms-item>
                    <uni-forms-item label="开户账号">
                        <uni-easyinput v-model="baseFormData.bankAccount" placeholder="请输入开户账号"/>
                    </uni-forms-item>
                    <uni-forms-item label="电子邮箱" name="email" required>
                        <uni-easyinput v-model="baseFormData.email" placeholder="请输入电子邮箱"/>
                    </uni-forms-item>
                    <uni-forms-item label="备注">
                        <uni-easyinput type="textarea" v-model="baseFormData.remark" placeholder="备注"/>
                    </uni-forms-item>
                    <!--<uni-section title="纸质票邮寄方式" type="line">-->
                    <!--<uni-forms-item label="邮寄地址" name="emsAdress" required="">-->
                    <!--<uni-easyinput v-model="baseFormData.emsAdress" placeholder="请输入邮寄地址"/>-->
                    <!--</uni-forms-item>-->
                    <!--<uni-forms-item label="联系人">-->
                    <!--<uni-easyinput v-model="baseFormData.emsUsername" placeholder="请输入联系人"/>-->
                    <!--</uni-forms-item>-->
                    <!--<uni-forms-item label="联系方式">-->
                    <!--<uni-easyinput v-model="baseFormData.emsTel" placeholder="请输入联系方式"/>-->
                    <!--</uni-forms-item>-->
                    <!--</uni-section>-->
                </uni-forms>

            </view>
        </uni-section>

        <view class="paddinglr30 margin-top-30 uni-common-mb">
            <button type="primary" @click="submit">确认</button>
        </view>
    </view>
</template>

<script>
export default {
  data() {
    return {
      titleIfo:'',
      items: [{
        value: '1',
        name: '个人'
      },
        {
          value: '2',
          name: '企业'
        },
        // {
        //   value: '3',
        //   name: '非企业性单位'
        // },
      ],
      current: 0,
      baseFormDataPerson: {
        invoicetitle: '',
        email: '',
        remark: '',
        phone:''
      },
      baseFormData: {
        invoicetitle: '',
        userId: '',
        address: '',
        phone: '',
        bank: '',
        bankAccount: '',
        email: '',
        remark: '',
        // emsAdress: '',
        // emsUsername: '',
        // emsTel: '',
      },
      // 校验规则
      rulesPerson: {
        invoicetitle: {
          rules: [{
            required: true,
            errorMessage: '发票抬头不能为空'
          }]
        },

        email: {
          rules: [{
            required: true,
            errorMessage: '电子邮箱不能为空'
          },]
        },

        phone: {
          rules: [{
            required: true,
            errorMessage: '手机号不能为空'
          },]
        },



      },
      // 校验规则
      rules: {
        invoicetitle: {
          rules: [{
            required: true,
            errorMessage: '发票抬头不能为空'
          }]
        },
        userId: {
          rules: [{
            required: true,
            errorMessage: '纳税人识别号不能为空'
          }]
        },
        email: {
          rules: [{
            required: true,
            errorMessage: '电子邮箱不能为空'
          },]
        },
        emsAdress: {
          rules: [{
            required: true,
            errorMessage: '邮寄地址不能为空'
          },]
        },
        phone: {
          rules: [{
            required: true,
            errorMessage: '手机号不能为空'
          },]
        },
      },
    };
  },
  onLoad() {
    this.titleIfo = JSON.parse(uni.getStorageSync('titleIfo'))
    console.log(this.titleIfo)
    if(this.titleIfo.invoiceType == '0'){
      this.current = 0

      this.baseFormDataPerson.invoicetitle = this.titleIfo.name
      this.baseFormDataPerson.email = this.titleIfo.email
      this.baseFormDataPerson.remark = this.titleIfo.remark
      this.baseFormDataPerson.phone = this.titleIfo.phone

    }else{
      this.current = 1

      this.baseFormData.invoicetitle = this.titleIfo.name
      this.baseFormData.email = this.titleIfo.email
      this.baseFormData.remark = this.titleIfo.remark
      this.baseFormData.bank = this.titleIfo.bankName
      this.baseFormData.bankAccount = this.titleIfo.cardNo
      this.baseFormData.address = this.titleIfo.address
      this.baseFormData.userId  = this.titleIfo.taxid
      this.baseFormData.phone  = this.titleIfo.phone

    }
  },
  onReady() {
    // 设置自定义表单校验规则，必须在节点渲染完毕后执行
    // this.$refs.baseForm.setRules(this.rules)
  },
  methods: {
    radioChange: function (evt) {
      for (let i = 0; i < this.items.length; i++) {
        if (this.items[i].value === evt.detail.value) {
          this.current = i;
          break;
        }
      }
    },
    submit(ref) {
      console.log()
      if(this.current==0){
        this.$refs['baseFormPerson'].validate().then(res => {
          console.log('success', res);


          let updateCustInvoiceInfo = this.$common.updateCustInvoiceInfo;
          let jsondata = {
            id: this.titleIfo.id,
            name: this.baseFormDataPerson.invoicetitle,
            email: this.baseFormDataPerson.email,
            remark: this.baseFormDataPerson.remark,
            invoiceType:'0',
            isDefault:this.titleIfo.isDefault,
            phone:this.baseFormDataPerson.phone
          };
          uni.request({
            url: updateCustInvoiceInfo,
            data: JSON.stringify(this.$common.requestSign(jsondata)),
            dataType: "json",
            method: "POST",
            success: (res) => {
              console.log(res)
              if (res.data.code == 0) {
                uni.showToast({
                  icon:'none',
                  title: `编辑成功`
                })
                uni.navigateBack({
                  delta: 1
                })
              }else{
                uni.showToast({
                  icon:'none',
                  title: res.data.message
                })

              }
            }
          })

        }).catch(err => {
          console.log('err', err);
        })
      }
      if(this.current==1){
        this.$refs['baseForm'].validate().then(res => {

          let updateCustInvoiceInfo = this.$common.updateCustInvoiceInfo;
          let jsondata = {

            id: this.titleIfo.id,
            name: this.baseFormData.invoicetitle,
            email: this.baseFormData.email,
            remark: this.baseFormData.remark,
            invoiceType:'1',
            isDefault:this.titleIfo.isDefault,
            taxid:this.baseFormData.userId,
            bankName:this.baseFormData.bank,
            cardNo:this.baseFormData.bankAccount,
            address:this.baseFormData.address,
            phone:this.baseFormData.phone,
          };
          uni.request({
            url: updateCustInvoiceInfo,
            data: JSON.stringify(this.$common.requestSign(jsondata)),
            dataType: "json",
            method: "POST",
            success: (res) => {
              console.log(res)
              if (res.data.code == 0) {
                uni.showToast({
                  icon:'none',
                  title: `编辑成功`
                })
                uni.navigateBack({
                  delta: 1
                })
              }else{
                uni.showToast({
                  icon:'none',
                  title: res.data.message
                })

              }
            }
          })

        }).catch(err => {
          console.log('err', err);
        })
      }
    }
  },
}
</script>

<style lang="scss">

</style>
