<template>
    <view>
        <view>

            <view class="uni-list" v-if="items.length>0">
                <radio-group @change="titleChange">
                    <label class="display-block border-bottom-5" v-for="(item, index) in items" :key="item.value">
                        <view class="elec-card bg-white">
                            <view class="elec-card-left float-left" >
                                <view class="elec-card-msg">{{item.name}}</view>
                                <view class="elec-card-msg">发票类型:{{item.invoiceType | invoiceTypeFilter}}</view>
                            </view>
                            <view class="elec-card-right float-right"  @click="toUse(JSON.stringify(item))">
                                <button class="mini-btn color-62c7ee" type="primary" size="mini" >
                                    去使用
                                </button>
                            </view>
                        </view>
                        <view class="uni-list-cell uni-list-cell-pd">
                            <view>
                                <radio :value="JSON.stringify(item)" :checked="item.isDefault == 1"/>
                            </view>
                            <view style="flex:1;">默认抬头</view>
                            <view>
                                <button class="mini-btn margin-left-5" type="primary" size="mini"
                                        @click="detailLookBtn(JSON.stringify(item), index)">查看
                                </button>
                                <button class="mini-btn margin-left-5" type="default" size="mini"
                                        @click="detailEditBtn(JSON.stringify(item), index)">编辑
                                </button>
                                <button class="mini-btn margin-left-5" type="warn" size="mini"
                                        @click="detailDelBtn(JSON.stringify(item), index)">删除
                                </button>
                            </view>
                        </view>
                    </label>
                </radio-group>
            </view>

            <view style="padding: 30px;text-align: center" v-else>暂无抬头,请先添加！</view>
            <view class="billing-all">
                <button type="primary" class="title-submit" @click="addInvoiceTitle">添加发票抬头</button>
            </view>
        </view>
    </view>
</template>

<script>
export default {
  data() {
    return {
      items: [
      //   {
      //   value: '1',
      //   checked: 'true'
      // },
      //   {
      //     value: '2'
      //   },
      //   {
      //     value: '3'
      //   },
      ],
      current: 0,
    };
  },
  onLoad(params) {

  },
  onShow(){
    this.queryCustInvoiceInfoList()
  },
  methods: {
    queryCustInvoiceInfoList() {
      let that = this
      var queryCustInvoiceInfoList = this.$common.queryCustInvoiceInfoList;
      var jsondata = {};
      uni.request({
        url: queryCustInvoiceInfoList,
        data: JSON.stringify(this.$common.requestSign(jsondata)),
        dataType: "json",
        method: "POST",
        success: (res) => {
          console.log(res)
          if (res.data.code == 0) {
            this.items = res.data.data
          }
        }
      })
    },
    titleChange: function (evt) {
      console.log(JSON.parse(evt.detail.value))
      let jsonData = JSON.parse(evt.detail.value)
      // for (let i = 0; i < this.items.length; i++) {
      //   if (this.items[i].default === evt.detail.value) {
      //     this.current = i;
      //     break;
      //   }
      // }
      var updateCustInvoiceInfo = this.$common.updateCustInvoiceInfo;
      var jsondata = {
        address:jsonData.address,
        bankName:jsonData.bankName,
        cardNo:jsonData.cardNo,
        isDefault:'1',
        phone:jsonData.phone,
        taxid:jsonData.taxid,
        name:jsonData.name,
        invoiceType:jsonData.invoiceType,
        id:jsonData.id,
        remark:jsonData.remark,
        email:jsonData.email,


        // name: this.baseFormDataPerson.invoicetitle,
        // email: this.baseFormDataPerson.email,
        // remark: this.baseFormDataPerson.remark,
        // isDefault: '1',// 是否默认客户抬头信息，1表示是默认抬头信息，0不是默认抬头信息（默认0）
        // invoiceType: '0', // 发票类型（个人、公司），0 表示个人，1表示公司


      };
      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: `设置成功`
            })
          }
        }
      })
    },
    detailLookBtn(val) {
      uni.setStorageSync('companyMsg',val)
      uni.navigateTo({
        url: '../companyMsg/companyMsg'
      });
    },
    detailEditBtn(val,i) {
      uni.setStorageSync('titleIfo',val)
      uni.navigateTo({
        url: '../editTitle/editTitle'
      });
    },
    detailDelBtn(val,i) {
      console.log(val)
      console.log(i)
      var deleteCustInvoiceInfoByID = this.$common.deleteCustInvoiceInfoByID;
      var jsondata = {
        id: JSON.parse(val).id,
      };
      uni.request({
        url: deleteCustInvoiceInfoByID,
        data: JSON.stringify(this.$common.requestSign(jsondata)),
        dataType: "json",
        method: "POST",
        success: (res) => {
          console.log(res)
          if (res.data.code == 0) {
            this.queryCustInvoiceInfoList()
            uni.showToast({
              icon:'none',
              title: `删除成功`
            })
          }
        }
      })
    },
    addInvoiceTitle() {
      uni.navigateTo({
        url: '../addTitle/addTitle'
      });
    },
    toUse(val) {
      console.log(val)
      uni.setStorageSync('useTitleIfo',val)
      let i = JSON.parse(val)
      uni.navigateTo({
        url: '../billing/billing?invoiceType='+i.invoiceType+'&email='+i.email+'&name='+i.name+'&remark='+i.remark+'&phone='+i.phone
      });
    }
  },
  filters:{
    invoiceTypeFilter(val){
      console.log(val)
      if(val=='0'){
        return '个人'
      }
      if(val=='1'){
        return '企业'
      }
    }
  }
}
</script>

<style lang="scss">
    .elec-card {
        width: 100%;
        height: 64upx;
        padding: 22upx 30upx;

        border-bottom: 1px solid #e5e5e5;
    }

    .elec-card-left {
        width: 70%;
        height: 64upx;

    }

    .elec-card-right {
        width: 26%;
        height: 64upx;
        line-height: 64upx;
    }

    .elec-card-msg {
        line-height: 32upx;
        color: rgba(0, 0, 0, .7);

    }

    .billing-all {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 46px;
        line-height: 46px;
        // font-size: 24px;

    }
</style>
