<template>
  <a-modal
    title="采购数量"
    v-model="purchaseAmountModVis"
    :confirmLoading="confirmLoading"
    width="86%"
    :footer="null"
    @cancel="close"
  >
    <!-- 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-form-item label="单据号">
                <a-input placeholder="请输入单据号" v-model="queryParam.ccode"></a-input>
              </a-form-item>
            </a-col>
             <a-col :md="6" :sm="8">
              <a-form-item label="批号">
                <a-input placeholder="请输入批号" v-model="queryParam.cbatch"></a-input>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <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-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!-- table -->
      <div>
        <a-table
           ref="tableRef"
          :loading="loading"
          bordered
          :columns="purchaseAmountColumns"
          :data-source="purchaseAmountData"
          :pagination="false"
          :scroll="{ y: 500,x:1500 }"
          :footer="showTotal"
        >
        </a-table>
        <!-- 导出 打印 返回  -->
        <a-row style="marginTop:20px;">
          <a-col :md="24" :sm="12">
            <span style="float: right;" class="table-operator">
              
              <a-button type="primary" @click="backFabricLossTable" icon="rollback">关闭</a-button>
              
            </span>
          </a-col>
        </a-row>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JEllipsis from '@/components/jeecg/JEllipsis'
import JsonExcel from 'vue-json-excel'
export default {
  name: 'PurchaseAmountModal', // 采购数量 弹框
  mixins: [JeecgListMixin],
  components: { JEllipsis ,JsonExcel},
  data() {
    return {
      exportTitle:"采购数量",
      

      // 表头
      purchaseAmountColumns: [
        {
          title: '序号',
          width:80,
           customRender: (text, record, index) => {
            if (record.ccode == "合计")
              return "";
            else
              return index + 1;
           }
        },
        {
          title: '单据号',
          dataIndex: 'ccode',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '存货编码',
          dataIndex: 'cinvCode',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '存货名称',
          dataIndex: 'ccode',
          width: 240,
          key: '',
          className: 'replacecolor',
          customRender: (text, record, index) => {
            if (record.ccode == "合计")
              return "";
            else
            return record.cinvName + ' ' + record.ccolor;
          }
        },
        {
          title: '批号',
          dataIndex: 'cbatch',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '入库单数量',
          dataIndex: 'iquantity',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '含税单价',
          dataIndex: 'iprice',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '开票数',
          dataIndex: 'iquantityInvoice',
          width: 100,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '未开票数',
          dataIndex: 'iquantityNInvoice',
          width: 100,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '结算数量',
          dataIndex: 'iquantitySettle',
          width: 100,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '未结算数量',
          dataIndex: 'iquantityNSettle',
          width: 100,
          key: '',
          className: 'replacecolor'
        }
      ],
      purchaseAmountData: [],
      allDataList:[],
      loading: false, // 表格加载
      // orderDataform: this.$form.createForm(this),
      confirmLoading: false,
      purchaseAmountModVis: false,

      // 查询条件
      queryParam: {
        ccode: '', // 纱批
        cbatch: '' // 采购单价
      }
    }
  },
  // 接收父组件 方法
  props: {
    father: {
      type: Function,
      default: null
    }
  },
  computed: {
    // 合计展示
    totalDataSource(){
      // 开票成本-衬衣 合计
      var item = {
        "ccode":"合计"
      };
      var iquantity = 0,iquantityInvoice=0,iquantityNInvoice=0,iquantitySettle=0,iquantityNSettle=0;
      for (let row of this.purchaseAmountData){
        iquantity += row.iquantity*1;
        iquantityInvoice += row.iquantityInvoice*1;
        iquantityNInvoice += row.iquantityNInvoice*1;
        iquantitySettle += row.iquantitySettle*1;
        iquantityNSettle += row.iquantityNSettle*1;
      }
      
      item.iquantity= parseFloat(iquantity.toFixed(4));
      item.iquantityInvoice = parseFloat(iquantityInvoice.toFixed(4));
      item.iquantityNInvoice = parseFloat(iquantityNInvoice.toFixed(4));
      item.iquantitySettle = parseFloat(iquantitySettle.toFixed(4));
      item.iquantityNSettle = parseFloat(iquantityNSettle.toFixed(4));
      return [item];

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

      });
      return ret;
    }
  },
  created() {},
   watch:{
    purchaseAmountData(){
      this.$nextTick(()=>{  
         const dom = this.$refs.tableRef.$el.getElementsByClassName('ant-table-body')[0];
          dom.addEventListener(
          'scroll',
          () => {
            this.$refs.tableInfo.$el.querySelectorAll(
                '.ant-table-body'
            )[0].scrollLeft = dom.scrollLeft
          },
          true
      )
      })
    }
  },
  methods: {
    // 第一行 导出
    // handleExportXls() {},
    // 打印
    print() {},
    // 返回
    backFabricLossTable() {
      console.log('返回到面料损耗表')
      // this.$router.push('fabricLoss-table')
      // this.purchaseAmountModVis = false
      this.close()
    },
    // 弹框查询按钮
    searchQuery() {
      var data = []
       this.allDataList.map(item=>{
           if(!item.ccode){item.ccode = ''}
           if(!item.cbatch){item.cbatch = ''}
            if(item.ccode.includes(this.queryParam.ccode) && item.cbatch.includes(this.queryParam.cbatch)){
             data.push(item)
           }     
      });
      this.purchaseAmountData =data
      
    },
    // 重置
    searchReset() {
      this.queryParam ={
        ccode:'',
        cbatch:''
      }
      this.purchaseAmountData = this.allDataList
      // this.getShipmentList()
    },
    close() {
      this.$emit('close')
      this.purchaseAmountModVis = false
      this.searchReset()
    },
    showTotal(data) {
      return (
        <a-table
        ref="tableInfo"
          rowKey={Math.random}
          bordered={false}
          pagination={false}
          columns={this.purchaseAmountColumns}
          dataSource={this.totalDataSource || []}
          showHeader={false}
           scroll={{x:1500 }}
        ></a-table>
      )
    },
    // 导出excel
    exportExcel(){
      this.$refs.realExportExcel.$el.click();
    },
    // 生成导出数据
    getExportDataList(){
      this.purchaseAmountData.forEach((item,index)=>{item["ccode_ex"]="&nbsp;"+item["ccode"]});
      return this.purchaseAmountData;
    }
  }
}
</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/ .ant-table-footer .ant-table-body {
    overflow: hidden !important;
  }
  /deep/.ant-modal-body{
  height: auto !important;
    overflow-y: scroll;
}
// /deep/ th.replacecolor {
//   background-color: #ccc;
// }
</style>
