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

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="98px"
    >
      <QueryFormWrapper>
        <el-col :span="8">
          <el-form-item label="批次号" prop="batchNumber">
            <el-input
              v-model="queryParams.batchNumber"
              placeholder="请扫入批次号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="物料批次" prop="materialLots">
            <el-input
              v-model="queryParams.materialLots"
              placeholder="请输入物料批次"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="采购订单" prop="purchaseOrderNo">
            <el-input
              v-model="queryParams.purchaseOrderNo"
              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="materialNo">
                      <el-input
                        v-model="model.materialNo"
                        placeholder="请输入物料编码"
                        clearable
                        @keyup.enter.native="handleQuery"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="物料名称" prop="materialName">
                      <el-input
                        v-model="model.materialName"
                        placeholder="请输入物料名称"
                        clearable
                        @keyup.enter.native="handleQuery"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="是否已打印" prop="testBar">
                      <DictSelect v-model="model.testBar" placeholder="请选择是否已打印" dict-type="YES_NO" />
                    </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">
      <right-toolbar
        :columns="columnsList"
        :show-search.sync="showSearch"
        @saveSetting="getColumnsList"
        @queryTable="getList"
      />
    </el-row>

    <!-- 列表 -->
    <AutoResizer :given-height="166">
      <template #default="{height}">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :data="list"
          :height="height"
          border
        >
          <el-table-column
            v-if="columnsList[0].visible"
            :key="Math.random()"
            label="来料批次"
            align="center"
            prop="materialLots"
            width="150"
            fixed="left"
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="采购单号"
            align="center"
            prop="purchaseOrderNo"
            width="130"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="批次数量"
            align="center"
            prop="receiptQty"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="物料编码"
            align="center"
            prop="materialNo"
            min-width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[4].visible"
            :key="Math.random()"
            label="物料名称"
            align="center"
            prop="materialName"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[5].visible"
            :key="Math.random()"
            label="单位"
            align="center"
            prop="unitCode"
            width="80"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[6].visible"
            :key="Math.random()"
            label="供应商"
            align="center"
            prop="supplierName"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[7].visible"
            :key="Math.random()"
            label="长(m)"
            align="center"
            prop="length"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[8].visible"
            :key="Math.random()"
            label="宽(mm)"
            align="center"
            prop="width"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[9].visible"
            :key="Math.random()"
            label="是否已打印"
            align="center"
            prop="testBar"
            width="130"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.YES_NO" :value="scope.row.testBar" />
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="80"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-printer"
                @click="handlePrint(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"
    />
    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      v-dialogDrag
      title="录入测试条打印信息"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="88px"
      >
        <el-form-item label="标题" prop="title">
          <DictSelect v-model="form.title" placeholder="请选择标题" dict-type="PULL_FORCE_TITLE" />
        </el-form-item>
        <el-form-item label="原胶批号" prop="materialLots">
          <el-input v-model="form.materialLots" disabled placeholder="请输入原胶批号" />
        </el-form-item>
        <el-form-item label="原胶型号" prop="glueModel">
          <el-input v-model="form.glueModel" placeholder="请输入原胶型号" />
        </el-form-item>
        <el-form-item label="原胶拉力" prop="pullingForce">
          <DictSelect v-model="form.pullingForce" placeholder="请选择原胶拉力" dict-type="ORIGINAL_GLUE_PULL_FORCE" />
        </el-form-item>
        <el-form-item label="相对剥离力" prop="strippingForce">
          <DictSelect v-model="form.strippingForce" placeholder="请选择相对剥离力" dict-type="RELATIVE_STRIPPING_FORCE" />
        </el-form-item>
        <el-form-item label="数量" prop="qty">
          <el-input-number v-model="form.qty" :min="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 单据打印 -->
    <IssuePrinit ref="issuePrinit" />
  </div>
</template>

<script>
import { getTestBarPage, saveGluePullForce } from '@/api/wms/quality/testStripPrint'
// 打印参数
const AccessTokenKey = 'ACCESS_TOKEN'
import { mapState } from 'vuex'
import IssuePrinit from './iqcInspection/components/IssuePrinit.vue'
import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'TestStripPrint',
  components: {
    IssuePrinit
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 不良代码	列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 高级查询参数
      advParams: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        badNo: null,
        badName: null,
        badDescription: null,
        badType: null,
        badClass: null,
        remark: null,
        createTime: []
      },
      // 单据打印参数
      tokens: '',
      baseUrl: '',
      reportId: '',
      printId: null,
      // 表单参数
      form: {
        qty: 10
      },
      // 表单校验
      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: '长(m)', visible: true },
        { key: 8, label: '宽(mm)', visible: true },
        { key: 9, label: '是否已打印', visible: true }
      ],
      // 显示隐藏持久化参数
      columnsList: []
    }
  },
  computed: {
    ...mapGetters([
      'userId'
    ]),
    ...mapState({
      routeLists: state => state.permission.addRoutes
    })
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList()
    this.getList()
    this.tokens = localStorage.getItem(AccessTokenKey)
    this.baseUrl = process.env.VUE_APP_REPORT_API
    this.reportId = this.$route.meta.reportId
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = { ...this.queryParams, ...this.advParams }
      getTestBarPage(params).then(response => {
        this.list = response.data.list
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        badNo: undefined,
        badName: undefined,
        badDescription: undefined,
        badType: undefined,
        badClass: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.advParams = {}
      this.handleQuery()
    },
    // 高级查询
    advQuery(advQueryParams) {
      this.advParams = advQueryParams
      this.handleQuery()
    },
    // 打印
    handlePrint(row) {
      const { materialName, materialLots, id } = row
      this.printId = id
      this.form.guleModel = materialName
      this.form.materialLots = materialLots
      this.open = true
    },
    // 保存打印信息
    submitForm() {
      saveGluePullForce(this.form).then(res => {
        this.$modal.msgSuccess('保存成功，打印中...')
        this.getList()
        this.open = false
        const src = `${this.baseUrl}/jmreport/view/${this.reportId}?token=${this.tokens}&id=${res.data}`
        this.$refs.issuePrinit.dialogVisible = true
        this.$refs.issuePrinit.src = src
      })
    },
    // 获取显示隐藏列
    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
        }
      })
    }
  }
}
</script>

