<template>
  <el-dialog
    append-to-body
    title="合同在途"
    :visible.sync="dialogVisible"
    v-loading.fullscreen.lock="fullscreenLoading"
    destroy-on-close
    :show-close="false"
    fullscreen
    @close="close"
    @open="open"
  >
    <el-row :gutter="20">
      <el-col :span="20" style="border: 1px solid white">
        物料:<el-select
        v-model="queryrow.material_id"
        clearable
        size="mini"
        placeholder="请选择物料"
        style="width: 200px"
        class="filter-item"
        @change="MyQuery"
      >
        <el-option
          v-for="item in XLList"
          :key="item.material_id"
          :label="item.material_name"
          :value="item.material_id"
        />
        </el-select>
      </el-col>
      <el-col :span="4">
        <span>
          <!--左侧插槽-->
          <slot name="left" />
          <el-button slot="left" type="success" @click="MyQuery2">查询</el-button>
          <el-button slot="left" type="info" @click="dialogVisible = false">关闭</el-button>
        </span>
      </el-col>
    </el-row>
    <!--表格渲染-->
    <el-table
      ref="table"
      :data="tableDtl"
      style="width: 100%;background: transparent;overflow:auto;"
      :max-height="590"
      border
      show-summary
      :summary-method="getSummaries"
      :highlight-current-row="true"
      :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
    >
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column prop="receive_code" label="合同号" min-width="100" />
      <el-table-column prop="material_code" label="物料编码" min-width="150" />
      <el-table-column prop="material_name" label="物料名称" min-width="150" />
      <el-table-column prop="sumqty" label="合同重量" min-width="100" :formatter="crud.formatNum2" />
      <el-table-column prop="notqty" label="在途重量" min-width="100" :formatter="crud.formatNum2" />
    </el-table>
  </el-dialog>
</template>

<script>

import { header } from '@crud/crud'
import report from '@/api/wms/statistics/report'

export default {
  name: 'StructIvt3',
  mixins: [header()],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    rowmst: {
      type: Object
    }
  },
  data() {
    return {
      dialogVisible: false,
      tableDtl: [],
      cxjList: [],
      XLList: [],
      fullscreenLoading: false,
      queryrow: { material_id: '' },
      sortable: null,
      rows: []
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    },
    rowmst: {
      handler(newValue, oldValue) {
        this.queryrow = newValue
      }
    }
  },
  methods: {
    open() {
      report.query4().then(res => {
        this.XLList = res
      })
      this.MyQuery2()
    },
    /**
     * 接受父组件传值
     * @param msg
     */
    getMsg(msg) {
      this.queryrow = msg
    },
    queryStruct() {
      this.fullscreenLoading = true
      report.query3(this.queryrow).then(res => {
        this.tableDtl = res
        this.fullscreenLoading = false
      }).catch(() => {
        this.fullscreenLoading = false
      })
    },
    close() {
      this.queryrow.material_id = ''
      this.tableDtl = []
      this.$emit('update:dialogShow', false)
    },
    MyQuery(value) {
      this.queryrow.material_id = value
      this.fullscreenLoading = true
      report.query3(this.queryrow).then(res => {
        this.tableDtl = res
        this.fullscreenLoading = false
      }).catch(() => {
        this.fullscreenLoading = false
      })
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        const values = data.map(item => Number(item[column.property]))
        if (column.property === 'sumqty') {
          const total = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] = parseFloat(total).toFixed(3)
          sums[index]
        }

        if (column.property === 'notqty') {
          const total = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] = parseFloat(total).toFixed(3)
          sums[index]
        }
      })
      return sums
    },
    MyQuery2() {
      this.fullscreenLoading = true
      report.query3(this.queryrow).then(res => {
        this.tableDtl = res
        this.fullscreenLoading = false
      }).catch(() => {
        this.fullscreenLoading = false
      })
    }
  }
}
</script>
