<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      @submit.native.prevent
      label-width="100px"
    >
      <el-form-item label="LPN扫码/输入" prop="lpnNo">
        <el-input
          v-model="queryParams.lpnNo"
          placeholder="LPN扫码/输入回车立马打印"
          clearable
          @keyup.enter.native="handleprint"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="handleprint"
          >打印</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['pro:scheduleSetupRule:export']"
          >导出</el-button>
        </el-col> -->
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table
      v-loading="loading"
      v-if="scheduleSetupRuleList.length > 0"
      :data="scheduleSetupRuleList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        label="工单号"
        align="center"
        width="120"
        prop="workorderCode"
      />
      <el-table-column
        label="销售编号"
        align="center"
        width="160"
        prop="salesVoucher"
      />
      <el-table-column
        label="销售编号项次"
        align="center"
        width="160"
        prop="salesVoucherItem"
      />
      <el-table-column label="LPN码" align="center" prop="lpnNo" />
      <el-table-column label="on" align="center" prop="是否打印">
        <template slot-scope="scope">
          {{ scope.row.on ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
    </el-table>


  </div>
</template>
  
  <script>
import { listlpnrecord } from '@/api/mes/lpn/lpnLabel'
import { postBatchPrintPdf } from '@/utils/ruoyi'

export default {
  name: 'lpnLabel',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户产品标签模板对照表表格数据
      scheduleSetupRuleList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lpnNo: null,
      },
      customerOptions: [],
      brandOptions: [],
      templateOptions: [],
      selectedRows: [],
      timer: null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        templateId: [
          {
            required: true,
            message: '请选择模板',
            trigger: ['blur', 'change'],
          },
        ],
      },
    }
  },
  created() {
    // this.getList()
  },
  methods: {
    handleprint() {
      if (this.timer !== null) return
      this.timer = setTimeout(async () => {
        this.timer = null
        await this.getList()
        if (
          !this.scheduleSetupRuleList ||
          this.scheduleSetupRuleList.length === 0
        )
          return this.$modal.msgError('没有数据')
        const valueList = this.scheduleSetupRuleList.map((v) => {
          return {
            packagePrint: {
              lpnNo: v.lpnNo,
            }
          }
        })
        await postBatchPrintPdf('package-lpnV2', valueList).then(() => {
          this.visible = false
          this.getPackageList()
        })
      }, 100)
    },
    onItemSelected(obj) {
      if (obj != undefined && obj != null) {
        this.form.itemId = obj.itemId
        this.form.itemCode = obj.itemCode
        this.form.itemName = obj.itemName
      }
    },
    /** 查询客户产品标签模板对照表列表 */
    async getList() {
      this.loading = true
      await listlpnrecord(this.queryParams).then((response) => {
        this.scheduleSetupRuleList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        itemCode: null,
        customerName: null,
        brandName: null,
        brandCode: null,
        brandId: null,
        itemId: null,
        itemName: null,
        itemTypeCode: null,
        itemTypeId: null,
        itemTypeName: null,
        sapItemCode: null,
        templateName: null,
        templateType: null,
        templateCode: null,
        templateId: null,
        createBy: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleSelectProduct() {
      this.$refs.itemSelect.showFlag = true
    },
    handleitemType() {
      this.$refs.brSelect.showFlag = true
    },
    onitemTypeed(row) {
      if (Object.values(row).length > 0) {
        this.form.itemTypeId = row.itemTypeId
        this.form.sapTypeCode = row.sapTypeCode
        this.form.itemTypeCode = row.itemTypeCode
        this.form.itemTypeName = row.itemTypeName
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.selectedRows = selection
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加客户产品标签模板对照表'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if (!row || !row.id) row = this.selectedRows[0]
      this.reset()
      this.form = row
      this.open = true
      this.title = '修改客户产品标签模板对照表'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.customerCode) {
            this.form.customerName = this.customerOptions.find(
              (v) => this.form.customerCode === v.KUNNR
            ).NAME
          }
          if (this.form.brandCode) {
            this.form.brandName = this.brandOptions.find(
              (v) => this.form.brandCode === v.brandCode
            ).brandName
          }
          if (this.form.templateId) {
            const template = this.templateOptions.find(
              (v) => this.form.templateId === v.id
            )
            this.form.templateName = template.name
            this.form.templateType = template.reportType
            this.form.templateCode = template.id + ''
          }
          if (this.form.id != null) {
            updateCustomerLabel(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCustomerLabel(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.id || this.ids
      this.$modal
        .confirm(
          '是否确认删除客户产品标签模板对照表编号为"' + Ids + '"的数据项？'
        )
        .then(function () {
          return delcustomerLabel(Ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'pro/scheduleSetupRule/export',
        {
          ...this.queryParams,
        },
        `scheduleSetupRule_${new Date().getTime()}.xlsx`
      )
    },
  },
}
</script>
  