<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible.sync="open"
    width="800px"
    append-to-body
    @open="dialogOpen"
  >
    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      border
      size="mini"
    >
      <el-table-column
        label="投料单号"
        align="center"
        prop="workOrderBomNo"
        width="150"
        show-overflow-tooltip
      />
      <el-table-column
        label="需求数量"
        align="center"
        prop="needQty"
      />
      <el-table-column
        label="已备数量"
        align="center"
        prop="prepareQty"
      />
      <el-table-column
        label="已发数量"
        align="center"
        prop="sendQty"
      />
      <el-table-column label="备料状态" align="center" prop="prepareStatus">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.WMS_MATERIAL_PREPARATION_STATUS" :value="scope.row.prepareStatus" />
        </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
            v-hasPermi="['wms:work-order-bom:query']"
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            :disabled="scope.row.prepareStatus === 1"
            @click="handleEnsurePreparation(scope.row)"
          >确认备料</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 对话框扫标签码 -->
    <el-dialog
      v-dialogDrag
      title="备料扫码"
      :visible.sync="visible"
      width="400px"
      append-to-body
      @open="dialogOpen"
    >
      <el-form
        v-show="showSearch"
        ref="queryForm"
        :model="queryParams"
        size="small"
        label-width="68px"
        :rules="rules"
        @submit.native.prevent
      >
        <el-form-item label="标签码" prop="qrCode">
          <el-input v-model="queryParams.qrCode" placeholder="请扫入/输入标签码" @keyup.enter.native="ensurePrepare" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="ensurePrepare" @loading="ensureLoading">确认</el-button>
        <el-button @click="visible = false">取消</el-button>
      </div>
    </el-dialog>

  </el-dialog>
</template>

<script>
import { getPdaNeedDetail, updatePdaUpdate } from '@/api/wms/inLibrary/productionPreparation'

export default {
  name: 'ProductionPreparationDetail',
  components: {
  },
  data() {
    return {

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      ensureLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售订单明细列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 扫码
      visible: false,
      prepareId: '',
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        materialNo: '',
        qrCode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        qrCode: [{ required: true, message: '标签码不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getPdaNeedDetail(this.queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    dialogOpen() {
      this.getList()
    },
    // 确认备料
    handleEnsurePreparation(row) {
      const { id } = row
      this.prepareId = id
      this.visible = true
    },
    // 确认备料
    ensurePrepare() {
      this.$refs['queryForm'].validate(valid => {
        if (!valid) {
          return
        }
        this.ensureLoading = true
        const params = {
          id: this.prepareId,
          qrCode: this.queryParams.qrCode
        }
        updatePdaUpdate(params).then(res => {
          this.$modal.msgSuccess('操作成功')
          this.visible = false
          this.open = false
          this.$emit('prepareComplete')
          this.getList()
        }).finally(() => {
          this.queryParams.qrCode = undefined
          this.visible = false
          this.open = false
          this.ensureLoading = false
        })
      })
    }
  }
}
</script>

