<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <QueryFormWrapper>
        <el-col :span="8">
          <el-form-item label="成品编码" prop="productNo">
            <el-input
              v-model="queryParams.productNo"
              placeholder="请输入成品编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="成品名称" prop="productName">
            <el-input
              v-model="queryParams.productName"
              placeholder="请输入成品名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="生产单号" prop="workOrderNo">
            <el-input
              v-model="queryParams.workOrderNo"
              placeholder="请输入生产单号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <div slot="right">
          <el-form-item label-width="0" class="text-right" style="word">
            <EasyAdvancedQuery class="mr10" label-width="98px" @advQuery="advQuery">
              <template #default="{model}">
                <el-row :gutter="10" type="flex">
                  <el-col :span="12">
                    <el-form-item label="工单号" prop="erpWorkOrderNo">
                      <el-input
                        v-model="queryParams.erpWorkOrderNo"
                        placeholder="请输入工单号"
                        clearable
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="下单日期" prop="erpMdTime">
                      <el-date-picker
                        v-model="queryParams.erpMdTime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
              </template>
            </EasyAdvancedQuery>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
      </QueryFormWrapper>

    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- <el-button
          v-hasPermi="['wms:inventory-general:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button> -->
      </el-col>
      <right-toolbar
        :columns="columnsList"
        :show-search.sync="showSearch"
        @saveSetting="getColumnsList"
        @queryTable="getList"
      />
    </el-row>

    <!-- 列表 -->
    <AutoResizer :given-height="177">
      <template #default="{height}">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :data="list"
          :height="height"
          border
          :summary-method="getSummaries"
          show-summary
        >
          <el-table-column
            v-if="columnsList[33].visible"
            :key="Math.random()"
            label="工单部门名称"
            align="center"
            prop="workShopName"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[0].visible"
            :key="Math.random()"
            label="生产单号"
            align="center"
            prop="workOrderNo"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="下单日期"
            align="center"
            prop="erpMdTime"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="{ row }">
              <span>{{ parseTime(row.erpMdTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="工单号"
            align="center"
            prop="erpWorkOrderNo"
            min-width="140"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="成品编码"
            align="center"
            prop="productNo"
            fixed="left"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[4].visible"
            :key="Math.random()"
            label="成品名称"
            align="center"
            prop="productName"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[5].visible"
            :key="Math.random()"
            label="成品分类"
            align="center"
            prop="productType"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[6].visible"
            :key="Math.random()"
            label="下单数量"
            align="center"
            prop="productAmount"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[7].visible"
            :key="Math.random()"
            label="单位"
            align="center"
            prop="unitName"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[8].visible"
            :key="Math.random()"
            label="材料编码"
            align="center"
            prop="materialNo"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[9].visible"
            :key="Math.random()"
            label="替换前材料编码"
            align="center"
            prop="oldMaterialNo"
            min-width="140"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[10].visible"
            :key="Math.random()"
            label="是否主材"
            align="center"
            prop="mainMaterial"
            min-width="140"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[11].visible"
            :key="Math.random()"
            label="材料名称"
            align="center"
            prop="materialName"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[12].visible"
            :key="Math.random()"
            label="材料需求数量"
            align="center"
            prop="needQty"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[13].visible"
            :key="Math.random()"
            label="出库数量"
            align="center"
            prop="sendQty"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[14].visible"
            :key="Math.random()"
            label="退回数量"
            align="center"
            prop="returnQty"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[15].visible"
            :key="Math.random()"
            label="领料部门编号"
            align="center"
            prop="departmentNo"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[16].visible"
            :key="Math.random()"
            label="领料部门名称"
            align="center"
            prop="departmentName"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[17].visible"
            :key="Math.random()"
            label="未出库数量"
            align="center"
            prop="notSendQty"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[18].visible"
            :key="Math.random()"
            label="待退回数"
            align="center"
            prop="needReturnQty"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[19].visible"
            :key="Math.random()"
            label="未退回数"
            align="center"
            prop="notReturnQty"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[20].visible"
            :key="Math.random()"
            label="工单制单人"
            align="center"
            prop="workOrderMakePerson"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column label="工单状态" align="center" prop="workStatus">
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.MES_PRODUCTION_ORDER_STATUS" :value="scope.row.workStatus" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[21].visible"
            :key="Math.random()"
            label="本材料单位用量"
            align="center"
            prop="materialUnitDosage"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[22].visible"
            :key="Math.random()"
            label="出库米数"
            align="center"
            prop="outMetre"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[23].visible"
            :key="Math.random()"
            label="退回米数"
            align="center"
            prop="returnMetre"
            min-width="140"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[24].visible"
            :key="Math.random()"
            label="长度"
            align="center"
            prop="length"
            min-width="140"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[25].visible"
            :key="Math.random()"
            label="宽度"
            align="center"
            prop="width"
            min-width="140"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[26].visible"
            :key="Math.random()"
            label="单位用量"
            align="center"
            prop="unitDosage"
            min-width="140"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[27].visible"
            :key="Math.random()"
            label="货位"
            align="center"
            prop="location"
            min-width="140"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[28].visible"
            :key="Math.random()"
            label="bom备注"
            align="center"
            prop="bomRemark"
            min-width="140"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[29].visible"
            :key="Math.random()"
            label="不含税单价"
            align="center"
            prop="unitPrice"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[30].visible"
            :key="Math.random()"
            label="最后发料日期"
            align="center"
            prop="lastSendDate"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="{ row }">
              <span>{{ parseTime(row.lastSendDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[31].visible"
            :key="Math.random()"
            label="领料仓库"
            align="center"
            prop="pickStockName"
            min-width="110"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[32].visible"
            :key="Math.random()"
            label="工单部门编号"
            align="center"
            prop="workShopCode"
            min-width="110"
            show-overflow-tooltip
          />
        </el-table>
      </template>
    </AutoResizer>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getWorkOrderBomExecutePage } from '@/api/wms/inquirePurchase/documentList'

import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'MaterialFeedingList',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存总账列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 高级查询参数
      advParams: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        productNo: null,
        productName: null,
        workOrderNo: null,
        erpWorkOrderNo: null,
        deliverBatch: [],
        wmsStockId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 显示隐藏列参数
      columns: [
        { key: 0, label: '生产单号', visible: true },
        { key: 1, label: '下单日期', visible: true },
        { key: 2, label: '工单号', visible: true },
        { key: 3, label: '成品编码', visible: true },
        { key: 4, label: '成品名称', visible: true },
        { key: 5, label: '成品分类', visible: true },
        { key: 6, label: '下单数量', visible: true },
        { key: 7, label: '单位', visible: true },
        { key: 8, label: '材料编码', visible: true },
        { key: 9, label: '替换前材料编码', visible: true },
        { key: 10, label: '是否主材', visible: true },
        { key: 11, label: '材料名称', visible: true },
        { key: 12, label: '材料需求数量', visible: true },
        { key: 13, label: '出库数量', visible: true },
        { key: 14, label: '退回数量', visible: true },
        { key: 15, label: '领料部门编号', visible: true },
        { key: 16, label: '领料部门名称', visible: true },
        { key: 17, label: '未出库数量', visible: true },
        { key: 18, label: '待退回数', visible: true },
        { key: 19, label: '未退回数', visible: true },
        { key: 20, label: '工单制单人', visible: true },
        { key: 21, label: '本材料单位用量', visible: true },
        { key: 22, label: '出库米数', visible: true },
        { key: 23, label: '退回米数', visible: true },
        { key: 24, label: '长度', visible: true },
        { key: 25, label: '宽度', visible: true },
        { key: 26, label: '单位用量', visible: true },
        { key: 27, label: '货位', visible: true },
        { key: 28, label: 'bom备注', visible: true },
        { key: 29, label: '不含税单价', visible: true },
        { key: 30, label: '最后发料日期', visible: true },
        { key: 31, label: '领料仓库', visible: true },
        { key: 32, label: '工单部门编号', visible: true },
        { key: 33, label: '工单部门名称', visible: true }
      ],
      // 显示隐藏持久化参数
      columnsList: []
    }
  },
  // 获取当前登录用户id
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList()
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = { ...this.queryParams, ...this.advParams }
      getWorkOrderBomExecutePage(params).then(response => {
        this.list = response.data.list
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    /** 表单重置 */
    reset() {
      this.form = {
        pageNo: 1,
        pageSize: 10,
        productNo: null,
        productName: null,
        workOrderNo: null,
        erpWorkOrderNo: null,
        deliverBatch: null,
        wmsStockId: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.advParams = {}
      this.handleQuery()
    },
    // 小数处理
    getHandleNumber(val) {
      return Math.round((parseFloat(val) * 100000000)) / 100000000
    },
    // 表尾计算
    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 === 'inQty') {
          sums[index] = values.reduce((acc, cur) => {
            const value = Number(cur)
            if (!isNaN(value)) {
              return this.getHandleNumber(acc + cur)
            } else {
              return acc
            }
          }, 0)
          sums[index]
        }
      })
      return sums
    },
    // 高级查询
    advQuery(advQueryParams) {
      this.advParams = advQueryParams
      this.handleQuery()
    },
    // 获取显示隐藏列
    getColumnsList() {
      const { componentName } = this.$route.meta || null
      const params = {
        userId: this.userId,
        vueForm: componentName
      }
      getFormColumnsList(params).then(res => {
        if (isArray(res.data)) {
          this.columnsList = res.data.length === 0 ? this.columns : res.data
          // 对比默认数据和接口数据，将缺失的列添加到接口数据中
          this.columnsList = this.columns.reduce((acc, defaultColumn) => {
            const found = res.data.find(apiColumn => apiColumn.key === defaultColumn.key)
            if (!found) {
              acc.push({ ...defaultColumn, vueForm: componentName, userId: this.userId, id: null })
            }
            return acc
          }, res.data)
        } else {
          this.columnsList = this.columns
        }
      }).catch((err) => {
        if (err) {
          this.columnsList = this.columns
        }
      })
    }
  }
}
</script>
