<template>
  <a-modal
    title="采购入库数量"
    v-model="purchaseInQuantityModVis"
    :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-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>
                <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                  {{ toggleSearchStatus ? '收起' : '展开' }}
                  <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
                </a> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!-- table -->
      <div>
        <a-table
          ref="tableRef"
          :loading="loading"
          bordered
          :columns="purchaseInQuantityColumns"
          :data-source="purchaseInQuantityData"
          :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" 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 { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JEllipsis from '@/components/jeecg/JEllipsis'
import JsonExcel from 'vue-json-excel'
export default {
  name: 'PurchaseInQuantityModal', // 采购入库数量 弹框
  mixins: [JeecgListMixin],
  components: { JEllipsis,JsonExcel },
  data() {
    return {
       exportTitle:"采购入库数量",
      // 表头
      purchaseInQuantityColumns: [
        {
          title: '序号',
          width:80,
           customRender: (text, record, index) => {
            if (record.cbatch == "合计")
              return "";
            else
              return index + 1;
           }
        },
        {
          title: '批号',
          dataIndex: 'cbatch',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '委外订单号',
          dataIndex: 'cplanCode',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '采购入库数量',
          dataIndex: 'iquantityIn',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '单个物料成本',
          dataIndex: 'iprice',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '开票数',
          dataIndex: 'isumBillQuantity',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '未开票数',
          dataIndex: 'isumBillQuantityN',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '结算数量',
          dataIndex: 'isquantity',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '未结算数量',
          dataIndex: 'isquantityN',
          width: 120,
          key: '',
          className: 'replacecolor'
        }
      ],
      purchaseInQuantityData: [],
      allDataList:[],
      loading: false, // 表格加载
      // orderDataform: this.$form.createForm(this),
      confirmLoading: false,
      purchaseInQuantityModVis: false,
      // 查询条件
      queryParam: {
        downAboardorderNum: '', // 下游委外订单号
        aboardorderNum: '' // 委外订单号
      }
    }
  },
  // 接收父组件 方法
  props: {
    father: {
      type: Function,
      default: null
    }
  },
  created() {},
   watch:{
    purchaseInQuantityData(){
      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
      )
      })
    }
  },
  computed: {
    // 合计展示
    totalDataSource(){
      // 开票成本-衬衣 合计
      var item = {
        "cbatch":"合计"
      };
      var iquantityIn = 0,isumBillQuantity = 0,isumBillQuantityN=0,isquantity=0,isquantityN=0;
      for (let row of this.purchaseInQuantityData){
        iquantityIn += row.iquantityIn*1;
        isumBillQuantity += row.isumBillQuantity*1;
        isumBillQuantityN += row.isumBillQuantityN*1;
        isquantity += row.isquantity*1;
        isquantityN += row.isquantityN*1;
      }
      
      item.iquantityIn= parseFloat(iquantityIn.toFixed(4));
      item.isumBillQuantity= parseFloat(isumBillQuantity.toFixed(4));
      item.isumBillQuantityN= parseFloat(isumBillQuantityN.toFixed(4));
      item.isquantity= parseFloat(isquantity.toFixed(4));
      item.isquantityN= parseFloat(isquantityN.toFixed(4));
      return [item];

    },
    // 获取导出json定义
    exportFields(){
      var ret = {};
      this.purchaseInQuantityColumns.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.materialsOutQuantityModVis = false
      this.close()
    },
    // 弹框查询按钮
    searchQuery() {
      this.purchaseInQuantityData = this.allDataList.filter(e=>
        (this.queryParam.ccode == null || e.ccode.toLowerCase().indexOf(this.queryParam.ccode.toLowerCase())>-1) && 
        (this.queryParam.cbatch == null || e.cbatch.toLowerCase().indexOf(this.queryParam.cbatch.toLowerCase())>-1) 
      );
    },
    // 重置
    searchReset() {
      this.queryParam = {}
      // this.getShipmentList()
    },
    close() {
      this.$emit('close')
      this.purchaseInQuantityModVis = false
    },
    showTotal(data) {
      return (
        <a-table
          ref="tableInfo"
          rowKey={Math.random}
          bordered={false}
          pagination={false}
          columns={this.purchaseInQuantityColumns}
          dataSource={this.totalDataSource || []}
          showHeader={false}
          scroll={{x:1500 }}
        ></a-table>
      )
    },
    // 导出excel
    exportExcel(){
      this.$refs.realExportExcel.$el.click();
    },
    // 生成导出数据
    getExportDataList(){
      this.purchaseInQuantityData.forEach((item,index)=>{item["ccode_ex"]="&nbsp;"+item["ccode"]});
      return this.purchaseInQuantityData;
    }
  }
}
</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/ th.replacecolor {
//   background-color: #ccc;
// }
</style>
