<template>
  <a-modal
    title="发票数量"
    v-model="invoiceQuantityModVis"
    :confirmLoading="confirmLoading"
    width="86%"
    :footer="null"
  >
    <!-- tabel 加载 -->
    <a-spin :spinning="confirmLoading">
       <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
               <a-button type="primary" icon="download" @click="exportExcel" style="margin-left: 8px">导出</a-button>
                <JsonExcel
                  :fetch="getExportDataList"
                  :fields="exportFields"
                  :header="exportTitle"
                  name="发票数量.xls"
                  style="display:none"
                >
                  <!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
                  <a-button type="primary" icon="download" ref="realExportExcel">导出</a-button>
                </JsonExcel>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- table -->
      <div style="marginTop:30px;">
        <a-table
          :loading="loading"
          :columns="invoiceQuantityColumns"
          :data-source="invoiceQuantityData"
          :pagination="false"
          bordered
          :scroll="{ y: 500 }"
          :footer="showTotal"
        >
        </a-table>

        <!-- 导出 打印 返回  -->
        <a-row>
          <a-col :md="24" :sm="12" style="marginTop:20px;">
            <span style="float: right;" class="table-operator">
              <!-- <a-button type="primary" icon="download" @click="handleExportXls('采购数量')">导出</a-button>
              <a-button type="primary" @click="print" icon="printer" style="margin:0 10px;">打印</a-button> -->
              <a-button type="primary" @click="backFabricLossTable" icon="rollback">关闭</a-button>
            </span>
          </a-col>
        </a-row>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import JsonExcel from 'vue-json-excel'
export default {
  name: 'InvoiceQuantityModal', // 发票数量 弹框
  components: {JsonExcel},
  data() {
    return {
       exportTitle:"发票数量",
      // 表头
      invoiceQuantityColumns: [
        {
          title: '序号',
          width:80,
           customRender: (text, record, index) => {
            if (record.cinvName == "合计")
              return "合计";
            else
              return index + 1;
           }
        },
        {
          title: '开票数',
          dataIndex: 'iquantityInvoice',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '未开票数',
          dataIndex: 'iquantityInvoiceN',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '结算数量',
          dataIndex: 'iquantitySettle',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '未结算数量',
          dataIndex: 'iquantitySettleN',
          width: 120,
          key: '',
          className: 'replacecolor'
        }
      ],
      invoiceQuantityData: [],
      allDataList:[],
      size: 'small', // 查询按钮变小
      loading: false, // 表格加载
      // orderDataform: this.$form.createForm(this),
      confirmLoading: false,
      invoiceQuantityModVis: false
    }
  },
  // 接收父组件 方法
  props: {
    father: {
      type: Function,
      default: null
    }
  },
  created() {},
  computed: {
    // 合计展示
    totalDataSource(){
      // 开票成本-衬衣 合计
      var item = {
        "cinvName":"合计"
      };
      var iquantityInvoice = 0,iquantityInvoiceN = 0, iquantitySettle=0, iquantitySettleN=0;
      for (let row of this.invoiceQuantityData){
        iquantityInvoice += row.iquantityInvoice*1;
        iquantityInvoiceN += row.iquantityInvoiceN*1;
        iquantitySettle += row.iquantitySettle*1;
        iquantitySettleN += row.iquantitySettleN*1;
      }
      
      item.iquantityInvoice= parseFloat(iquantityInvoice.toFixed(4));
      item.iquantityInvoiceN= parseFloat(iquantityInvoiceN.toFixed(4));
      item.iquantitySettle= parseFloat(iquantitySettle.toFixed(4));
      item.iquantitySettleN= parseFloat(iquantitySettleN.toFixed(4));
      return [item];

    },
    // 获取导出json定义
    exportFields(){
      var ret = {};
      this.invoiceQuantityColumns.forEach((record,index)=>{
        if (record.title != "序号"){
          if (record.title != "入库单号")
            ret[record.title] = record.dataIndex;
          else
            ret[record.title] = record.dataIndex+"_ex";
        }

      });
      return ret;
    }
  },
  methods: {
    // 第一行 导出
    handleExportXls() {},
    // 打印
    print() {},
    // 返回
    backFabricLossTable() {
      console.log('返回到面料损耗表')
      // this.$router.push('fabricLoss-table')
      // this.invoiceQuantityModVis = false
      this.close()
    },

    close() {
      this.$emit('close')
      this.invoiceQuantityModVis = false
    },
    showTotal(data) {
      return (
        <a-table
          rowKey={Math.random}
          bordered={false}
          pagination={false}
          columns={this.invoiceQuantityColumns}
          dataSource={this.totalDataSource || []}
          showHeader={false}
        ></a-table>
      )
    },
     // 导出excel
    exportExcel(){
      this.$refs.realExportExcel.$el.click();
    },
    // 生成导出数据
    getExportDataList(){
      this.invoiceQuantityData.forEach((item,index)=>{item["ccode_ex"]="&nbsp;"+item["ccode"]});
      return this.invoiceQuantityData;
    }
  }
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
@import '~@assets/less/overwriter.less';
/deep/ .ant-table-thead > tr > th {
  text-align: center;
  // font-weight: 700;
}

/deep/ .ant-table-tbody {
  text-align: center;
}

// /deep/ th.replacecolor {
//   background-color: #ccc;
// }
</style>
