<template>
  <el-dialog
    v-dialogDrag
    title="实测结果"
    :visible.sync="visible"
    append-to-body
    width="800px"
    @open="baseDialogOpen"
  >

    <!-- 搜索工作栏 -->
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="55px"
      size="mini"
    >
      <el-row :gutter="10">
        <el-col :span="6" :offset="0">
          <el-form-item label="下限值">
            <el-input v-model="form.minValue" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6" :offset="0">
          <el-form-item label="标准值">
            <el-input v-model="form.standValue" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6" :offset="0">
          <el-form-item label="上限值">
            <el-input v-model="form.maxValue" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6" :offset="0">
          <el-form-item label="版本">
            <el-input v-model="form.version" disabled />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 列表 -->
    <el-table
      ref="table"
      :data="list"
      style="width: 100%"
      border
      size="mini"
    >
      <el-table-column prop="actual" align="center" label="实测值">
        <template slot-scope="scope">
          <el-input
            :ref="`auto-${scope.$index}`"
            v-model="scope.row.actual"
            :class="`auto-${scope.$index}`"
            oninput="value=value.replace(/^\D*(\d*(?:.\d{0,7})?).*$/g, '$1')"
            @keyup.enter.native="getQualified(scope.row,scope.$index)"
            @change="handleActualChange"
          />
        </template>
      </el-table-column>
      <el-table-column prop="qualified" align="center" label="是否合格">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.MES_QUALITY_IPQC_SINGLE_RESULT" :value="scope.row.qualified" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="120">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['wms:material-item-actual:delete']"
            type="text"
            size="mini"
            class="text-danger"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :loading="ensureLoading" @click="handleEnsure">确认</el-button>
    </span>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </el-dialog>
</template>

<script>
import { createMaterialItemActual, deleteMaterialItemActual, getMaterialItemActualPage, exportMaterialItemActualExcel } from '@/api/wms/quality/iqcInspectionExecute'

export default {
  name: 'MaterialItemActual',
  components: {
  },
  data() {
    return {
      visible: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检验项实测值列表
      list: [],
      ensureLoading: false,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      itemForm: {},
      itemLoading: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        materialItemResultId: null,
        qualified: null,
        actual: null,
        createTime: []
      },
      // 表单参数
      form: {
        version: ''
      },
      // 表单校验
      rules: {
      }
    }
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getMaterialItemActualPage(this.queryParams).then(response => {
        this.list = response.data.list
        this.list.push({ actual: '', qualified: '' })
        this.total = response.data.total
        // 光标自动聚焦
        const len = this.list.length
        this.$refs.table.$nextTick(() => {
          this.$refs['auto-' + (len - 1) ].focus()
        })
      }).finally(() => {
        this.loading = false
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    resetItemForm() {
      this.itemForm = {
        actual: undefined,
        qualified: '1'
      }
      this.resetForm('itemForm')
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除检验项实测值编号为"' + id + '"的数据项?').then(function() {
        return deleteMaterialItemActual(id)
      }).then(() => {
        this.getList()
        this.$emit('completeMeasure')
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      const params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      this.$modal.confirm('是否确认导出所有检验项实测值数据项?').then(() => {
        this.exportLoading = true
        return exportMaterialItemActualExcel(params)
      }).then(response => {
        this.$download.excel(response, '检验项实测值.xls')
        this.exportLoading = false
      }).catch(() => {})
    },
    baseDialogOpen() {
      this.$nextTick(() => {
        this.resetQuery()
      })
    },
    // 小数点问题处理
    // getHandleNumber(val) {
    //   return Math.round((parseFloat(val) * 10000000)) / 10000000
    // },
    // 实测值
    handleActualChange(v) {
      // this.getHandleNumber(v)
    },
    // 判断实测是否合格
    getQualified(row, index) {
      const { id: materialItemResultId } = this.form
      const { actual } = row
      const params = {
        actual,
        materialItemResultId
      }
      createMaterialItemActual(params).then(res => {
        row.qualified = res.data === 'true' ? '合格' : '不合格'
        this.getList()
        this.$emit('completeMeasure')
      }).finally(() => {
      })
    },
    //  确定
    handleEnsure() {
      const { id: materialItemResultId } = this.form
      const items = this.list.find(item => item.qualified === '' && item.actual !== '')
      if (items !== undefined) {
        const params = {
          actual: items.actual,
          materialItemResultId
        }
        this.ensureLoading = true
        createMaterialItemActual(params).then(res => {
          items.qualified = res.data === 'true' ? '合格' : '不合格'
          this.getList()
          this.visible = false
          this.$emit('completeMeasure')
          this.ensureLoading = false
        }).finally(res => {
          this.ensureLoading = false
        })
      } else {
        this.visible = false
      }
    }
  }

}
</script>
