<template>
  <div>
    <!-- 列表 -->
    <AutoResizer :given-height="220">
      <template #default="{height}">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :height="height"
          :data="list"
          border
        >
          <!-- <el-table-column label="发料单号" align="center" prop="outStockNo" /> -->
          <el-table-column
            v-if="columnsList[0].visible"
            :key="Math.random()"
            label="领料单号"
            align="center"
            prop="workOrderBomNo"
            width="140"
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="生产订单"
            align="center"
            prop="workOrderNo"
            width="120"
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="订单数量"
            align="center"
            prop="productAmount"
          />
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="产品编码"
            align="center"
            prop="productCode"
            width="140"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[4].visible"
            :key="Math.random()"
            label="产品名称"
            align="center"
            prop="productName"
            width="160"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[5].visible"
            :key="Math.random()"
            label="BOM版本"
            align="center"
            prop="bomVersion"
            width="80"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[9].visible"
            :key="Math.random()"
            label="发料类型"
            align="center"
            prop="type"
          />
          <el-table-column
            v-if="columnsList[6].visible"
            :key="Math.random()"
            label="发料状态"
            align="center"
            prop="sendStatus"
            width="140"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.MES_ISSUE_MATERIALS_STATUS" :value="scope.row.sendStatus" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[7].visible"
            :key="Math.random()"
            label="领料状态"
            align="center"
            prop="receiveStatus"
            width="100"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_RECEIVE_MATERIALS_STATUS" :value="scope.row.receiveStatus" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[10].visible"
            :key="Math.random()"
            label="进度"
            align="center"
            prop="progress"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_SEND_ISSUE_PROGRESS" :value="scope.row.progress" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[11].visible"
            :key="Math.random()"
            label="发料次数"
            align="center"
            prop="sendCount"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleViewIssue(scope.row)"
              >{{ scope.row.sendCount || 0 }}</el-button>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[8].visible"
            :key="Math.random()"
            label="创建时间"
            align="center"
            prop="createTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="退料详细" align="center" prop="progress">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleReturnDetails(scope.row)"
              >查看</el-button>
            </template>
          </el-table-column>
          <el-table-column label="发退明细" align="center" prop="progress">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleSendDetail(scope.row)"
              >查看</el-button>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="120"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                icon="el-icon-thumb"
                size="mini"
                @click="continueIssueInSome(scope.row)"
              >继续发料-部分</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </AutoResizer>

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

    <!-- 对话框(发退明细) -->
    <ReturnDetails ref="returnDetails" />
    <!-- 继续发料-部分 -->
    <IssueInSome ref="continueIssue" @complete="getList" @codeComplete="getList" />
  </div>
</template>

<script>
import { getOutStockHaveSendPage } from '@/api/wms/output/productionIssue'
import ReturnDetails from './ReturnDetails.vue'
import IssueInSome from './IssueInSome.vue'
import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'OutStock',
  components: {
    ReturnDetails,
    IssueInSome
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // WMS 生产发料 主列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 高级查询参数
      advParams: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wmsProductionWorkOrderBomDetailId: [{ required: true, message: 'ERP用料清单明细表ID不能为空', trigger: 'blur' }],
        outStockNo: [{ required: true, message: '发料单号不能为空', trigger: 'blur' }]
      },
      // 显示隐藏列参数
      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: 'BOM版本', 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 }
      ],
      // 显示隐藏持久化参数
      columnsList: []
    }
  },
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList()
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = {
        ...this.paramsQuery,
        ...this.queryParams,
        ...this.advParams
      }
      getOutStockHaveSendPage(params).then(response => {
        this.list = response.data.list
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    // 退料明细
    handleReturnDetails(row) {
      const { workOrderBomNo } = row
      this.$router.push({ path: '/entry-exit/purchase-out-in/incoming-reject', query: { receiveOrderNo: workOrderBomNo }})
    },
    // 发退明细
    handleSendDetail(row) {
      const { id } = row
      this.$refs.returnDetails.open = true
      this.$refs.returnDetails.queryParams.masterId = id
    },
    // 继续发料部分
    continueIssueInSome(row) {
      const { id } = row
      this.$refs.continueIssue.queryParams.masterId = id
      this.$refs.continueIssue.open = true
    },
    // 发料次数
    handleViewIssue(row) {
      const { workOrderBomNo } = row
      // this.$router.push({ path: '/entry-exit/product-out-in/out-stock-detail', query: { id }})
      this.$router.push({ path: '/entry-exit/product-out-in/production-material-order', query: { workOrderBomNo }})
    },
    // 获取显示隐藏列
    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
        } else {
          this.columnsList = this.columns
        }
      }).catch((err) => {
        if (err) {
          this.columnsList = this.columns
        }
      }).finally(() => {
        this.$emit('columns', this.columnsList)
      })
    }
  }
}
</script>

