<template>
  <div style="height: 100%;display: flex;flex-direction: column;">
    <div class="list-wrap">
      <div class="noDada" v-if="list.length == 0">
        <img src="../../../assets/images/parkInvoice/parkInvoiceNo.png" width="180px" height="140px">
        <p>暂无发票抬头,请添加发票抬头才可以发票哦</p>

        <!--<div style="padding: 10px;">-->
        <!--<x-button type="warn">添加发票抬头</x-button>-->
        <!--</div>-->
      </div>

      <group v-else>
        <div v-for="(i, index) in list" style="border-bottom: 1px solid #D9D9D9 ">
          <cell :title="i.name" :inline-desc="`发票类型: ${i.invoiceType==0?'个人':'企业'}`">
            <div>
              <span @click="toUse(i)"
                    style="color: #FFF;padding: 2px 12px;background:red;border-radius: 15px;font-size: 14px;">去使用</span>
            </div>
          </cell>

          <cell>
            <div slot="title">
              <input type="radio" name="radioInvoice" :checked="i.isDefault == 1" @change="changDefault(i)">
              <span class="colorBlue">默认抬头</span>
            </div>
            <div>
              <x-button mini @click.native="checkOrEdit(i,0)">查看</x-button>
              <x-button mini type="primary" style="margin-top: 0" @click.native="checkOrEdit(i,1)">编辑</x-button>
              <x-button mini type="warn" style="margin-top: 0" @click.native="deleteHandle(i)">删除</x-button>
            </div>
          </cell>
        </div>
      </group>
    </div>
    <x-button type="warn" style="border-radius: 0" @click.native="toFill">新增抬头</x-button>

    <div transfer-dom>
      <confirm v-model="deleShow"
               title="删除"
               @on-cancel=""
               @on-confirm="onConfirm"
               @on-show=""
               @on-hide="">
        <p style="text-align:center;">您是否确定删除发票抬头？</p>
      </confirm>
    </div>
  </div>
</template>

<script>

import { queryCustInvoiceInfoList, deleteCustInvoiceInfoByID, updateCustInvoiceInfo } from "@/api/invoice/invoice.js";
export default {
  name: "invoiceOpt",
  data() {
    return {
      deleShow: false,
      list: [],// 抬头列表
      deleteID: ""
    };
  },
  mounted() {
    this.queryCustInvoiceInfoList(); //  获取抬头
  },
  methods: {
    // 前往使用
    toUse: function(i) {
      this.$router.push({
        path: "parkInvoice",
        query: {
          name: i.name,
          email: i.email,
          remark: i.remark,
          type: i.invoiceType == 0 ? "个人" : "企业"
        }
      });
    },
    // 设置默认
    changDefault: function(i) {
      console.log(i);
      let jsondata = {};
      if (i.invoiceType == 0) {
        let obj = {
          invoiceType: "0",//  0 ：个人  ， 1:企业   ，3:非营业单位
          name: i.name,
          email: i.email,
          remark: i.remark,
          isDefault: "1",
          id: i.id
        };
        jsondata = Object.assign(obj, this.$utils.commonParams());
      }
      if (i.invoiceType == 1) {
        let obj = {
          invoiceType: "1",//  0 ：个人  ， 1:企业   ，3:非营业单位
          name: i.name,
          email: i.email,
          remark: i.remark,
          phone: i.iphone,//企业注册电话
          address: i.address,//企业注册地址
          phone: i.phone,//企业注册电话
          taxid: i.taxid,//企业纳税人识别号
          bankName: i.bankName,
          cardNo: i.bankNum,
          isDefault: "1",
          id: i.id
        };
        jsondata = Object.assign(obj, this.$utils.commonParams());
      }
      jsondata.sign = this.$utils.signObject(jsondata);
      updateCustInvoiceInfo(jsondata).then(response => {
        console.log(jsondata);
        console.log(response);
        if (response.code == 0) {
          this.queryCustInvoiceInfoList();
        } else {
          alert(response.message);
        }
      });
    },
    // 删除
    deleteHandle: function(i) {
      this.deleteID = i.id;
      this.deleShow = true;
    },
    onConfirm() {
      console.log("on confirm");
      let obj = {
        id: this.deleteID
      };
      let jsondata = Object.assign(obj, this.$utils.commonParams());
      jsondata.sign = this.$utils.signObject(jsondata);
      console.log("传参  " + JSON.stringify(jsondata));
      deleteCustInvoiceInfoByID(jsondata).then(response => {
        this.queryCustInvoiceInfoList();
      });
    },
    //  获取抬头列表
    queryCustInvoiceInfoList: function() {
      let jsondata = this.$utils.commonParams();
      jsondata.sign = this.$utils.signObject(jsondata);
      console.log("传参  " + JSON.stringify(jsondata));
      queryCustInvoiceInfoList(jsondata).then(response => {
        console.log(response);
        this.list = response.data;
      });
    },
    // 前往开发票
    toFill: function() {
      this.$router.push({
        path: "fill"
      });
    },
    // 前往查看或者编辑发票
    checkOrEdit: function(i, status) {
      sessionStorage.setItem("invoiceData", JSON.stringify(i));
      this.$router.push({
        path: "checkEdit",
        query: {
          status: status, // 0 是查看  1是编辑
          type: i.invoiceType // 0 是个人  1是企业
        }
      });
    }
  }
};

</script>

<style scoped lang="scss">
  .list-wrap {
    flex: 1;
    overflow-y: auto;
    > > > .weui-cells {
      margin-top: 0;
    }
  }

  .colorBlue {
    color: #0d72e2;
  }

  .noDada {
    text-align: center;

    img {
      margin: 20px auto;
    }

  }
</style>
