<template>
  <a-modal
    title="材料出库数量"
    v-model="materialsOutQuantityModVis"
    :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="4" :sm="8">
              <a-form-item label="批号">
                <a-input placeholder="请输入批号" v-model="queryParam.cbatchOut"></a-input>
              </a-form-item>
            </a-col>

            <a-col :md="4" :sm="8">
              <a-form-item label="供应商">
                <a-input placeholder="请输入供应商" v-model="queryParam.cvenAbbName"></a-input>
              </a-form-item>
            </a-col>

            <a-col :md="4" :sm="8">
              <a-form-item label="物料编码">
                <a-input placeholder="请输入物料编码" v-model="queryParam.cinvCodeOut"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="8">
              <a-form-item label="颜色">
                <a-input placeholder="请输入颜色" v-model="queryParam.colour"></a-input>
              </a-form-item>
            </a-col>
            <template v-if="toggleSearchStatus">
              <a-col :md="4" :sm="8">
                <a-form-item label="物料名称">
                  <a-input placeholder="请输入物料名称" v-model="queryParam.cinvNameOut"></a-input>
                </a-form-item>
              </a-col>
                <a-col :md="4" :sm="8">
                <a-form-item label="订单号">
                  <a-input placeholder="请输入订单号" v-model="queryParam.cplanCode"></a-input>
                </a-form-item>
             
              </a-col>
            </template>
            <a-col :md="8" :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-row :gutter="24" v-if="type!=='1'">
            <a-col :md="4" :sm="8">
              <a-form-item label="">
                 <a-input placeholder="请输入" v-model="editPricre"></a-input>
              </a-form-item>
             
            </a-col>
            <a-col :md="4" :sm="8">
              <a-form-item label="">
                <a-button type="primary" @click="allEditPrice">批量修改</a-button>
              </a-form-item>
             
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!-- table -->
      <div>
        <a-table
          ref="tableRef"
          :loading="loading"
          bordered
          :columns="materialsOutQuantityColumns"
          :data-source="materialsOutQuantityData"
          :pagination="false"
          :scroll="{ y: 500,x:1500 }"
          :rowClassName="setRowClassName"
          :footer="showTotal"
        >
        <template slot="iprice" slot-scope="text, record, index">
              <a-input placeholder="请输入物料名称" v-model="record.iprice"  @blur="changePrice(record)" @pressEnter="changePrice(record)" v-if="type!=='1'"></a-input>
              <span v-else>{{ record.iprice }}</span>
        </template>
        </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: 'MaterialsOutQuantityModal', // 材料出库数量 弹框
  mixins: [JeecgListMixin],
  components: { JEllipsis,JsonExcel },
  data() {
    return {
      exportTitle:"材料出库数量",
      editPricre:'',
      allowModification:'no',
      // 表头
      materialsOutQuantityColumns: [
        {
          title: '序号',
          width:80,
           customRender: (text, record, index) => {
            if (record.cvenAbbName == "合计")
              return "";
            else
              return index + 1;
           }
        },
        {
          title: '供应商',
          dataIndex: 'cvenAbbName',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '订单号',
          dataIndex: 'cplanCode',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '入库单号',
          dataIndex: 'ccode',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '物料编码',
          dataIndex: 'cinvCodeOut',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '物料名称',
          dataIndex: 'cinvNameOut',
          width: 300,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '颜色',
          dataIndex: 'ccolorOut',
          width: 100,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '批号',
          dataIndex: 'cbatchOut',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        
        {
          title: '单价',
          dataIndex: 'iprice',
          width: 120,
          key: '',
          className: 'replacecolor',
          scopedSlots: { customRender: 'iprice' },
        },
        {
          title: '数量',
          dataIndex: 'iquantity',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        {
          title: '金额',
          dataIndex: 'imoney',
          width: 120,
          key: '',
          className: 'replacecolor'
        },
        
        
      ],
      materialsOutQuantityData: [],
      allDataList:[],
      loading: false, // 表格加载
      // orderDataform: this.$form.createForm(this),
      confirmLoading: false,
      materialsOutQuantityModVis: false,
     type:'',
      // 查询条件
      queryParam: {
        cbatchOut: '', // 批号
        cvenAbbName: '', // 供应商
        cinvCodeOut: '', // 物料编码
        colour:'',
        cinvNameOut: '' // 物料名称
      }
    }
  },
  computed: {
    // 合计展示
    totalDataSource(){
      // 开票成本-衬衣 合计
      var item = {
        "cvenAbbName":"合计"
      };
      var iquantity = 0,imoney = 0;
      for (let row of this.materialsOutQuantityData){
        iquantity += row.iquantity*1;
        imoney += row.imoney*1;
      }
      
      item.iquantity= parseFloat(iquantity.toFixed(4));
      item.imoney= parseFloat(imoney.toFixed(2));
      return [item];

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

      });
      return ret;
    }
  },
  // 接收父组件 方法
  props: {
    father: {
      type: Function,
      default: null
    }
  },
  created() {},
  watch:{
    materialsOutQuantityData(){
      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.materialsOutQuantityModVis = false
      this.close()
    },
    allEditPrice(){
       if(this.queryParam.cinvCodeOut=='' || !this.queryParam.cinvCodeOut){
        this.$message.error('物料编码不允许为空！' );
      }else if(this.queryParam.cvenAbbName !=='' && this.queryParam.cvenAbbName){
        this.$message.error('供应商必须为空！');
      } else if(this.editPricre ==''){
        this.$message.error('请输入修改的价格！');
      }else{           
        this.allDataList.map(item=>{
          if(item.cinvCodeOut == this.queryParam.cinvCodeOut && item.ccolorOut==this.queryParam.colour && (item.cvenAbbName == ''|| !item.cvenAbbName)){
              var length =(this.queryParam.cbatchOut)? this.queryParam.cbatchOut.length:0
              this.queryParam.cbatchOut = this.queryParam.cbatchOut?this.queryParam.cbatchOut:''
              if(item.cbatchOut.substring(0,length)==this.queryParam.cbatchOut){
                item.iprice = this.editPricre
              }

          }
        })
        this.$emit('Bulk-price',this.queryParam.cinvCodeOut,this.queryParam.colour,this.queryParam.cbatchOut,this.editPricre)
      }
    },
    // 弹框查询按钮
    searchQuery() {
      this.materialsOutQuantityData = this.allDataList.filter(e=>
        (!this.queryParam.cbatchOut  || e.cbatchOut.toLowerCase().indexOf(this.queryParam.cbatchOut.toLowerCase())>-1) && 
        (!this.queryParam.cvenAbbName  || e.cvenAbbName.toLowerCase().indexOf(this.queryParam.cvenAbbName.toLowerCase())>-1) &&
        (!this.queryParam.cinvCodeOut || e.cinvCodeOut.toLowerCase().indexOf(this.queryParam.cinvCodeOut.toLowerCase())>-1) &&
        (!this.queryParam.cinvNameOut  || e.cvenAbbName.toLowerCase().indexOf(this.queryParam.cinvNameOut.toLowerCase())>-1)&&
        (!this.queryParam.colour || e.ccolorOut.indexOf(this.queryParam.colour)>-1)&&
        (!this.queryParam.cplanCode || e.cplanCode.indexOf(this.queryParam.cplanCode)>-1)
      );
      this.allowModification = 'yes'
    },
    // 重置
    searchReset() {
      this.queryParam={
        cbatchOut: '', // 批号
        cvenAbbName: '', // 供应商
        cinvCodeOut: '', // 物料编码
        colour:'',
        cinvNameOut: '' // 物料名称
      }
      this.materialsOutQuantityData = this.allDataList
      // this.getShipmentList()
    },
    close() {
      this.$emit('close')
      this.materialsOutQuantityModVis = false
      this.allowModification = 'no'
      this.type =''
    },
    showTotal(data) {
      return (
        <a-table
          ref="tableInfo"
          rowKey={Math.random}
          bordered={false}
          pagination={false}
          columns={this.materialsOutQuantityColumns}
          dataSource={this.totalDataSource || []}
          showHeader={false}
          scroll={{x:1500 }}
        ></a-table>
      )
    },
    // 导出excel
    exportExcel(){
      this.$refs.realExportExcel.$el.click();
    },
    // 生成导出数据
    getExportDataList(){
      this.materialsOutQuantityData.forEach((record,index)=>record["ccode_ex"]="&nbsp;"+record["ccode"]);
      return this.materialsOutQuantityData;
    },
    //改变单价
    changePrice(record){
      
    },
     // 行样式设置
      setRowClassName(record) {
        return record.iprice == '0' ? "inputRowStyl" : "inputRowSty2";//赋予点击行样式
      }
  }
}
</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/.ant-table-fixed-header .ant-table-scroll .ant-table-header{
            width: calc(100% + 9px);//减去滚动条的宽度
}
// /deep/ th.replacecolor {
//   background-color: #ccc;
// }
</style>
