<template>
    <div>
         <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-row>
        <el-form-item label="退料单位" prop="backUnit">
          <el-input
            v-model="queryParams.backUnit"
            placeholder="退料单位"
            clearable
            style="width: 240px"
            disabled
          />
        </el-form-item>
        <el-form-item label="退料工程" prop="backPro">
          <el-input
            v-model="queryParams.backPro"
            placeholder="退料工程"
            clearable
            style="width: 240px"
            disabled
          />
        </el-form-item>
        <el-form-item label="维修单号" prop="repairCode">
          <el-input
            v-model="queryParams.repairCode"
            placeholder="维修单号"
            clearable
            style="width: 240px"
            disabled
          />
        </el-form-item>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-back"
          size="mini"
          @click="goBack"
          >维修管理</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="equipmentList"
    >
      <el-table-column
        label="序号"
        type="index"
        align="center"
        min-width="80"
      />
      <el-table-column
        label="类型名称"
        align="center"
        prop="typeName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="规格型号"
        align="center"
        prop="type"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="计量单位" align="center" prop="unitName"  show-overflow-tooltip/>
      <el-table-column
        label="退料数量"
        align="center"
        prop="typeRepairNum"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="已维修数量"
        align="center"
        prop="typeRepairedNum"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="报废数量"
        align="center"
        prop="typeScrapNum"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="待修数量"
        align="center"
        prop="disrepairNum"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column label="管理模式" align="center" prop="manageType">
        <template slot-scope="scope">
          <!-- 0编码1数量 -->
          <div v-if="scope.row.manageType == 0">编码管理</div>
          <div v-if="scope.row.manageType == 1">数量管理</div>
        </template>
      </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100" fixed="right">
              <template slot-scope="scope">
                  <el-button size="mini" type="primary" v-if="scope.row.manageType == 0"   @click="handleRepair(scope.row)">维修记录</el-button>
              </template>
      </el-table-column>
    </el-table>

    <!-- 维修记录弹窗 -->
    <el-dialog title="维修记录" :visible.sync="openRepair" width="46%">
        <el-table :data="repairRecord" border>
            <el-table-column label="类型名称" prop="typeName" align="center"></el-table-column>
            <el-table-column label="规格型号" prop="type" align="center"></el-table-column>
            <el-table-column label="机具编号" prop="code" align="center"></el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100" fixed="right">
              <template slot-scope="scope">
                  <el-button size="mini" type="primary"   @click="handleRecord(scope.row)">试验记录表</el-button>
              </template>
            </el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
            <el-button @click="openRepair = false">关闭</el-button>
        </span>
    </el-dialog>

    <!-- 弹框 -->
    <el-dialog title="试验记录表" :visible.sync="dialogVisible" width="46%" @close="closeDialog">
      <div  id="print-content2">
        <vue-easy-print tableShow ref="remarksPrintRef2" class="print" style="margin: 0 20px">
          <div style="text-align: center; font-weight: 600; font-size: 30px">机具（物流）分公司动力设备维修及试验记录单</div>
            <div class="info" style="margin: 10px 20px 0; display: flex; justify-content: space-between;">
                  <div
                        class="item"
                        style="flex-shrink: 0;margin-bottom: 5px;font-size: 14px;">
                        <span>退料单号：{{this.printInfo.code}}</span>
                    </div>
                    <div
                        class="item"
                        style="flex-shrink: 0;margin-bottom: 5px;font-size: 14px;">
                        <span>提交日期：{{this.printInfo.createTime}}</span>
                    </div>
                    <div class="item" style="flex-shrink: 0;margin-bottom: 5px;font-size: 14px;">
                        <span>设备编号：{{this.printInfo.maCode}}</span>
                    </div>
          </div>

          <div style="border: 3px solid #000; min-height: 900px; line-height: 3; position: relative">
            <div style="font-weight: 800; border-bottom: 1px solid #888; display: flex">
              <div style="width: 100%; padding-left: 10px;text-align: center">{{this.printInfo.typeName }}</div>
            </div>
            <div v-for="(group, groupIndex) in groupPrintData" :key="groupIndex" style="display: flex; border-bottom: 1px solid #000;">
              <!-- 遍历分组中的每个元素，每个元素生成 name 和 status 两个表格 -->
              <div v-for="(row, index) in group" :key="index" style="flex: 1; display: flex; border-right: 1px solid #000;">
                <div style="flex: 1; text-align: center; border-right: 1px solid #000; padding: 3px;">
                  <span style="display: block;">{{ row.partName || '' }}</span>
                </div>
                <div style="flex: 1; text-align: center; padding: 3px;">
                  <span style="display: block;">{{ row.status || '' }}</span>
                </div>
              </div>
            </div>
          <div style="position: absolute; bottom: 0; width: 100%;">
            <div style="font-weight: 800; border-bottom: 1px solid #888;border-top: 1px solid #888; display: flex">
              <div style="width: 20%; text-align: center;border-right: 1px solid #888;">空载试验</div>
              <div style="width: 80%; text-align: center; display: flex; justify-content: space-between; ">
                <!-- <span style="margin-left: 10px;">维修提交合格<span style="letter-spacing: 2em;">（    ）</span></span>
                <span>维修提交报废<span style="letter-spacing: 2em;">（    ）</span></span> -->
                <span style="margin-left: 10px;">维修提交合格<span :style="{ letterSpacing: printInfo && printInfo.scrapNum === 0 ? '0.9em' : '2em' }">（
                  <!-- 当 scrapNum 等于 0 时显示打勾图片 -->
                  <img v-if="printInfo && printInfo.scrapNum === 0" src="../../../../../assets/img/yes.png" alt="√" style="vertical-align: middle; height: 1em; margin: 0 1.0em 0 0; display: inline-block;"/>
                ）</span></span>
                <span>维修提交报废<span :style="{ letterSpacing: printInfo && printInfo.scrapNum > 0 ? '1.0em' : '2em' }">（
                  <!-- 当 scrapNum 大于 0 时显示打勾图片 -->
                  <img v-if="printInfo && printInfo.scrapNum > 0" src="../../../../../assets/img/yes.png" alt="√" style="vertical-align: middle; height: 1em; margin: 0 1.0em 0 0; display: inline-block;"/>
                ）</span></span>
              </div>
            </div>

            <div style="font-weight: 800; border-bottom: 1px solid #888; display: flex">
              <div style="width: 20%; text-align: center;border-right: 1px solid #888;">标识等</div>
              <div style="width: 80%; text-align: center; display: flex; justify-content: space-between;">
                <span style="margin-left: 10px;">二维码<span style="letter-spacing: 1.5em;">（<img src="../../../../../assets/img/yes.png" alt="√" style="vertical-align: middle; height: 1em; margin: 0 1.2em 0 0; display: inline-block;"/>）</span></span>
                <span>铭牌<span style="letter-spacing: 1.5em;">（<img src="../../../../../assets/img/yes.png" alt="√" style="vertical-align: middle; height: 1em; margin: 0 1.2em 0 0; display: inline-block;"/>）</span></span>
                <span>档位<span style="letter-spacing: 1.5em;">（<img src="../../../../../assets/img/yes.png" alt="√" style="vertical-align: middle; height: 1em; margin: 0 1.2em 0 0; display: inline-block;"/>）</span></span>
              </div>
            </div>

            <div style="font-weight: 800; border-bottom: 1px solid #888; display: flex">
              <div style="width: 60%;  display: flex; flex-direction: column;">
                <!-- 第一行：试验记录标题 -->
                <div style="display: flex; border-bottom: 1px solid #888;height: 40px;">
                  <div style="flex: 1; text-align: center; border-right: 1px solid #888;">额定负载</div>
                  <div style="flex: 1; text-align: center; border-right: 1px solid #888;">试验负载(T/MPa)</div>
                  <div style="flex: 1; text-align: center;">持荷时间(分/秒)</div>
                </div>
                <!-- 第二行：试验记录结果 -->
                <div style="display: flex; border-bottom: 1px solid #888;height: 40px;">
                  <div style="flex: 1; text-align: center; border-right: 1px solid #888;">{{ testResult1 || '' }}</div>
                  <div style="flex: 1; text-align: center; border-right: 1px solid #888;">{{ testResult2 || '' }}</div>
                  <div style="flex: 1; text-align: center;">{{ testResult3 || '' }}</div>
                </div>
                <!-- 第三行：试验日期、检验人员、提交人员标题 -->
                <div style="display: flex; border-bottom: 1px solid #888;height: 40px;">
                  <div style="flex: 1; text-align: center; border-right: 1px solid #888;">试验日期</div>
                  <div style="flex: 1; text-align: center; border-right: 1px solid #888;">检修人员</div>
                  <div style="flex: 1; text-align: center;">试验人员</div>
                </div>
                <!-- 第四行：对应结果 -->
                <div style="display: flex;height: 40px;">
                  <div style="flex: 1; text-align: center; border-right: 1px solid #888;">{{ formatDate(printInfo.createTime) }}</div>
                  <div style="flex: 1; text-align: center; border-right: 1px solid #888;">{{ this.printInfo.repairer }}</div>
                  <div style="flex: 1; text-align: center;">{{  this.printInfo.userName }}</div>
                </div>
              </div>
              <!-- 右边部分，占三分之一 -->
              <div style="width: 40%; display: flex; border-left: 1px solid #888;">
                <!-- 第一列：试验结论 -->
                <div style="width: 25%;text-align: center; display: flex; flex-direction: column; justify-content: center; border-right: 1px solid #888;">
                  <span>试验结论</span>
                </div>
                <!-- 第二列：合格 -->
                <div style="width: 75%; padding: 0;">
                  <input 
                    type="text"
                    v-model="testConclusion"
                    style="
                      width: 100%;
                      height: 100%;
                      font-size: 64px;
                      color: red;
                      font-weight: bold;
                      border: none;
                      outline: none;
                      box-sizing: border-box;
                      text-align: center;
                      background-color: white;
                      font-family: SimSun;
                    "
                  />
                </div>
              </div>
            </div>
          </div>
          </div>

          <div class="fillIn" style="margin-top: 10px; display: flex; justify-content: space-between">
            <div class="item" style="width: 33%">
              <div>
                <span>审核：</span>
              </div>
            </div>

            <div class="item" style="width: 33%">
              <div>
                <span>检验员：{{ '高民' }}</span>
              </div>
            </div>
          </div>
        </vue-easy-print>
      </div>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="print">打 印</el-button>
        <el-button @click="closeDialog">关 闭</el-button>
      </div>
    </el-dialog>



    </div>
</template>

<script>
import {
  getAppRepairMaTypeList,getRepairCodeList,getRepairRecord
} from "@/api/repair/repair.js";
import { mapState } from 'vuex'
import { getToken } from "@/utils/auth";
import vueEasyPrint from "vue-easy-print";
import printJS from 'print-js';

export default {
    name: 'ReturnApplyAdd',
    dicts: ["ma_machine_status"],
    components: { vueEasyPrint },
    props: {
        repairRow: {
        type: [Object],
        },
    },
    data() {
        return {
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 弹出层标题
            title: '',
            // 查询参数
            queryParams: {
                backUnit: "",
                backPro: "",
                repairCode: "",
                equipmentList: [],
            },
            equipmentList: [],

            // 选中的设备类型
            deviceType: null,
            companyId: '',
            createBy: '',
            rowId: '',

            dialogVisible: false, // 弹框显示
            dialogForm: {
              proName: '', // 项目名称
              code: '' // 业务单号
            },
            dialogColumns: [
              { label: '名称', prop: 'maTypeName', width: '150px' },
              { label: '规格', prop: 'typeName', width: '150px' },
              { label: '单位', prop: 'unitName', width: '60px' },
              { label: '数量', prop: 'preNum', width: '60px' },
              { label: '备注', prop: 'remark', width: '' }
            ],
            dialogList: [],
            openRepair: false,
            repairRecord: [],
            printTableData: [], 
            printInfo:{},
            testResult1: '',
            testResult2: '',
            testResult3: '',
            testConclusion: '合格',
        }
    },
    mounted() {
         this.GetViewByApply();
    },
    computed: {
        groupPrintData() {
            const result = [];
            console.log("xxxxxxxxxxxxx",this.printTableData.length)
            for (let i = 0; i < this.printTableData.length; i += 2) {
                const group = this.printTableData.slice(i, i + 2);
                // 若分组不足 2 个元素，补充空对象
                while (group.length < 2) {
                    group.push({ partName: '', status: '' });
                }
                result.push(group);
            }
            return result;
        },
        ...mapState(["user"]),
    },
    methods: {
    goBack() {
      this.$emit("goBackPage");
    },
    // 获取 任务详情数据
    async GetViewByApply() {
      console.log(this.repairRow);
      this.queryParams.backUnit = this.repairRow.backUnit;
      this.queryParams.backPro = this.repairRow.backPro;
      this.queryParams.repairCode = this.repairRow.repairCode;
      let param = {
        taskId: this.repairRow.taskId,
        keyword: "",
      };
      const res = await getAppRepairMaTypeList(param);
      this.equipmentList = res.data;
      this.equipmentList.forEach((e) => {
        e.disrepairNum = Number(e.typeRepairNum) - Number(e.typeRepairedNum) - Number(e.typeScrapNum);
      });
    },
    
    async handleRepair(row){
      this.openRepair = true;
      console.log("222222",row)
      let param = {
        taskId: this.repairRow.taskId,
        typeId: row.typeId
      }
      const res = await getRepairCodeList(param)
      this.repairRecord = res.data
    },

    async handleRecord(row){
      this.dialogVisible = true;
      this.printTableData = [];
      let param  = {
        id:row.id,
      }
      const res = await getRepairRecord(param)
      this.printInfo = res.data[0]
      if(this.printInfo && this.printInfo.ratedLoad) {
          this.testResult1 = this.printInfo.ratedLoad + '(T/MPa)'
      } else {
          this.testResult1 = ''
      }
      if(this.printInfo && this.printInfo.testLoad) {
          this.testResult2 = this.printInfo.testLoad + '(T/MPa)'
      } else {
          this.testResult2 = ''
      }
      if(this.printInfo && this.printInfo.holdingTime) {
          this.testResult3 = this.printInfo.holdingTime + '(分/秒)'
      } else {
          this.testResult3 = ''
      }
      if(res.data[0].partTypeList&& res.data[0].partTypeList.length != 0){
        res.data[0].partTypeList.forEach((e) => {
          this.printTableData.push({'partName':e.partName,'status':'不合格更换'})
        })
      }
    },

    // 定义格式化日期的方法
    formatDate(dateStr) {
        if (!dateStr) return '';
        const date = new Date(dateStr);
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        return `${year}-${month}-${day}`;
    },


    // 打印
    print() {
      printJS({
        printable: 'print-content2',
        type: 'html', //
        // targetStyles: ['*'], // 打印的元素样式
        scanStyles: false, // 是否扫描页面样式
        // css: [
        //   'https://unpkg.com/element-ui/lib/theme-chalk/index.css' // Element UI 的样式表
        // ],
        maxWidth: '1400'
        // 其他配置选项
      })
    },

    // 关闭弹框并清除数据
    closeDialog() {
      this.dialogVisible = false; // 关闭弹框
      this.printInfo = {}; // 清空数据
      this.printTableData = []; // 清空表格数据
      this.testResult1 = '';
      this.testResult2 = '';
      this.testResult3 = '';
      this.testConclusion = '合格';
    },
    }
}
</script>

