<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="测试总结果" prop="result">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.result"-->
      <!--          placeholder="请输入测试总结果"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="产品条码" prop="barcode">
        <el-input
          v-model="queryParams.barcode"
          placeholder="请输入产品条码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录时间">
        <el-date-picker
          v-model="daterangeRecordTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['report:electricalInspection:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['report:electricalInspection:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['report:electricalInspection:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['report:electricalInspection:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="electricalInspectionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="唯一标识" align="center" prop="uuid" v-if="columns[0].visible"/>
      <el-table-column label="产品条码" align="center" prop="barcode" v-if="columns[2].visible"/>
      <el-table-column label="测试总结果" align="center" prop="result" v-if="columns[1].visible"/>
      <el-table-column label="测试时间" align="center" prop="testTime" v-if="columns[3].visible"/>
      <el-table-column label="测试数据" align="center" prop="testData" v-if="columns[4].visible"/>
      <el-table-column label="记录时间" align="center" prop="recordTime" width="180" v-if="columns[5].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recordTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['report:electricalInspection:edit']"
          >查看明细
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['report:electricalInspection:remove']"-->
          <!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改电检数据记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!--        <el-form-item label="唯一标识" prop="uuid">-->
        <!--          <el-input v-model="form.uuid" placeholder="请输入唯一标识" />-->
        <!--        </el-form-item>-->
        <el-row gutter="15">
          <el-col :span="11">
            <el-form-item label="测试总结果" prop="result">
              <el-input v-model="form.result" placeholder="请输入测试总结果" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="产品条码" prop="barcode">
              <el-input v-model="form.barcode" placeholder="请输入产品条码" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-form-item label="测试时间" prop="testTime">-->
<!--          <el-input v-model="form.testTime" placeholder="请输入测试时间"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="测试数据" prop="testData">-->
<!--          <el-input v-model="form.testData" placeholder="请输入测试数据"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="记录时间" prop="recordTime">-->
<!--          <el-date-picker clearable-->
<!--                          v-model="form.recordTime"-->
<!--                          type="date"-->
<!--                          value-format="yyyy-MM-dd"-->
<!--                          placeholder="请选择记录时间"-->
<!--          >-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-divider content-position="center">电检数据明细信息</el-divider>
        <!--        <el-row :gutter="10" class="mb8">-->
        <!--          <el-col :span="1.5">-->
        <!--            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddDetailElectricalinspection">添加</el-button>-->
        <!--          </el-col>-->
        <!--          <el-col :span="1.5">-->
        <!--            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteDetailElectricalinspection">删除</el-button>-->
        <!--          </el-col>-->
        <!--        </el-row>-->
        <el-table :data="detailElectricalinspectionList" :row-class-name="rowDetailElectricalinspectionIndex"
                  @selection-change="handleDetailElectricalinspectionSelectionChange" ref="detailElectricalinspection"
        >
          <el-table-column type="selection" width="50" align="center"/>
          <!--          <el-table-column label="序号" align="center" prop="index" width="50"/>-->
          <el-table-column label="序号" prop="serialnumber" width="150">
            <!--            <template slot-scope="scope">-->
            <!--              <el-input v-model="scope.row.serialnumber" placeholder="请输入序号" />-->
            <!--            </template>-->
          </el-table-column>
          <el-table-column label="项目名称" prop="projectname" width="150">
            <!--            <template slot-scope="scope">-->
            <!--              <el-input v-model="scope.row.projectname" placeholder="请输入项目名称" />-->
            <!--            </template>-->
          </el-table-column>
          <el-table-column label="测试条件" prop="testcondition" width="150">
            <!--            <template slot-scope="scope">-->
            <!--              <el-input v-model="scope.row.testcondition" placeholder="请输入测试条件" />-->
            <!--            </template>-->
          </el-table-column>
          <el-table-column label="测试值" prop="testvalue" width="150">
            <!--            <template slot-scope="scope">-->
            <!--              <el-input v-model="scope.row.testvalue" placeholder="请输入测试值" />-->
            <!--            </template>-->
          </el-table-column>
          <el-table-column label="测试结果" prop="testresult" width="150">
            <!--            <template slot-scope="scope">-->
            <!--              <el-input v-model="scope.row.testresult" placeholder="请输入测试结果" />-->
            <!--            </template>-->
          </el-table-column>
          <el-table-column label="记录时间" prop="recordtime" width="240">
            <!--            <template slot-scope="scope">-->
            <!--              <el-date-picker clearable v-model="scope.row.recordtime" type="date" value-format="yyyy-MM-dd" placeholder="请选择记录时间" />-->
            <!--            </template>-->
          </el-table-column>
        </el-table>
      </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>
  </div>
</template>

<script>
import {
  listElectricalInspection,
  getElectricalInspection,
  delElectricalInspection,
  addElectricalInspection,
  updateElectricalInspection
} from '@/api/report/electricalInspection'

export default {
  name: 'ElectricalInspection',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedDetailElectricalinspection: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电检数据记录表格数据
      electricalInspectionList: [],
      // 电检数据明细表格数据
      detailElectricalinspectionList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 记录时间时间范围
      daterangeRecordTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        uuid: null,
        result: null,
        barcode: null,
        testTime: null,
        testData: null,
        recordTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      columns: [
        { key: 0, label: `唯一标识`, visible: false },
        { key: 1, label: `测试总结果`, visible: true },
        { key: 2, label: `产品条码`, visible: true },
        { key: 3, label: `测试时间`, visible: true },
        { key: 4, label: `测试数据`, visible: false },
        { key: 5, label: `记录时间`, visible: false }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询电检数据记录列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeRecordTime && '' != this.daterangeRecordTime) {
        this.queryParams.params['beginRecordTime'] = this.daterangeRecordTime[0]
        this.queryParams.params['endRecordTime'] = this.daterangeRecordTime[1]
      }
      listElectricalInspection(this.queryParams).then(response => {
        this.electricalInspectionList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        uuid: null,
        result: null,
        barcode: null,
        testTime: null,
        testData: null,
        recordTime: null
      }
      this.detailElectricalinspectionList = []
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRecordTime = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.uuid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加电检数据记录'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const uuid = row.uuid || this.ids
      getElectricalInspection(uuid).then(response => {
        this.form = response.data
        this.detailElectricalinspectionList = response.data.detailElectricalinspectionList
        this.open = true
        this.title = '查看电检数据记录明细'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.detailElectricalinspectionList = this.detailElectricalinspectionList
          if (this.form.uuid != null) {
            updateElectricalInspection(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addElectricalInspection(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const uuids = row.uuid || this.ids
      this.$modal.confirm('是否确认删除电检数据记录编号为"' + uuids + '"的数据项？').then(function() {
        return delElectricalInspection(uuids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 电检数据明细序号 */
    rowDetailElectricalinspectionIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 电检数据明细添加按钮操作 */
    handleAddDetailElectricalinspection() {
      let obj = {}
      obj.serialnumber = ''
      obj.projectname = ''
      obj.testcondition = ''
      obj.testvalue = ''
      obj.testresult = ''
      obj.recordtime = ''
      this.detailElectricalinspectionList.push(obj)
    },
    /** 电检数据明细删除按钮操作 */
    handleDeleteDetailElectricalinspection() {
      if (this.checkedDetailElectricalinspection.length == 0) {
        this.$modal.msgError('请先选择要删除的电检数据明细数据')
      } else {
        const detailElectricalinspectionList = this.detailElectricalinspectionList
        const checkedDetailElectricalinspection = this.checkedDetailElectricalinspection
        this.detailElectricalinspectionList = detailElectricalinspectionList.filter(function(item) {
          return checkedDetailElectricalinspection.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handleDetailElectricalinspectionSelectionChange(selection) {
      this.checkedDetailElectricalinspection = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('report/electricalInspection/export', {
        ...this.queryParams
      }, `electricalInspection_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
