<template>
    <div>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">   
            <el-form-item label="创建时间" prop="time">
                <el-date-picker
                    v-model="queryParams.time"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    type="daterange"
                    value-format="yyyy-MM-dd"
                    style="width: 240px"
                ></el-date-picker>
            </el-form-item>
            <el-form-item label="关键字" prop="keyWord">
                <el-input
                    maxlength="50"
                    v-model="queryParams.keyWord"
                    placeholder="请输入关键字"
                    clearable
                    style="width: 240px"
                />
            </el-form-item>
            <el-form-item label="状态" prop="taskStatus">
                <el-select
                    v-model="queryParams.taskStatus"
                    clearable
                    filterable
                    style="width: 240px"
                    placeholder="请选择"
                >   
                    <el-option
                        v-for="dict in dict.type.repair_task_status"
                        v-if="dict.value == 0 || dict.value == 1 || dict.value == 2"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                </el-select>
            </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 size="mini" @click="handleAdd">新增</el-button>
            </el-col> -->
            <el-col :span="1.5">
                <el-button type="warning" plain size="mini"  @click="handleSend">提交</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain size="mini" @click="handleReject">退回</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange" >
            <el-table-column type="selection" width="55" align="center" :selectable="selectable"/>
            <el-table-column label="序号" align="center" width="80" type="index">
                <template slot-scope="scope">
                    <span>{{(queryParams.pageNum - 1) * 10 + scope.$index + 1}}</span>
                </template>
            </el-table-column>
            <!-- <el-table-column align="center" label="序号" type="index"/> -->
            <el-table-column label="退料单位" align="center" prop="backUnit" :show-overflow-tooltip="true"/>
            <el-table-column label="退料工程" align="center" prop="backPro" :show-overflow-tooltip="true"/>
            <el-table-column label="维修单号" align="center" prop="repairCode" :show-overflow-tooltip="true"/>
            <el-table-column label="物资类型" align="center" prop="type" :show-overflow-tooltip="true"/>
            <el-table-column label="退料单号" align="center" prop="backCode" :show-overflow-tooltip="true"/>
            <el-table-column label="维修班组" align="center" prop="repairer" :show-overflow-tooltip="true"/>
            <!-- <el-table-column label="退料人电话" align="center" prop="phone" :show-overflow-tooltip="true"/> -->
            <el-table-column label="创建人" align="center" prop="createName" :show-overflow-tooltip="true"/>
            <el-table-column label="创建时间" align="center" prop="createTime" :show-overflow-tooltip="true"/>
            <el-table-column label="状态" align="center" :show-overflow-tooltip="true" prop="repairStatusCode">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.repair_task_status" :value="scope.row.repairStatusCode"/>
                </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
                <template slot-scope="scope">
                    <el-button size="mini" plain icon="el-icon-zoom-in" @click="handleSee(scope.row)" >查看</el-button>
                    <el-button size="mini" type="primary" plain icon="el-icon-edit" v-if="scope.row.repairStatusCode!='1'" @click="handleUpdate(scope.row)">维修</el-button>
                    <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleReject(scope.row)">驳回</el-button> 
                    <el-button size="mini" type="success"   @click="handlePrint(scope.row)">维修单</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="openPrint"
      width="1100px"
      append-to-body
    >
      <div style="height: 600px; overflow-y: scroll">
        <vue-easy-print tableShow ref="remarksPrintRef" class="print">
          <div
            class="title"
            style="text-align: center; font-weight: 600; font-size: 16px"
          >
            机具设备维修任务单
          </div>
          <div
            class="info"
            style="margin-top: 10px; display: flex; flex-wrap: wrap"
          >
            <div
              class="item"
              style="
                width: 50%;
                flex-shrink: 0;
                margin-bottom: 5px;
                font-size: 14px;
              "
            >
              <span>日期：</span>
               {{ repairTicketlLevelOne.createTime }}
            </div>
             <div
              class="item"
              style="
                width: 50%;
                flex-shrink: 0;
                margin-bottom: 5px;
                font-size: 14px;
              "
            >
              <span>单号：</span>
               {{ repairTicketlLevelOne.repairCode }}
            </div>
            <div
              class="item"
              style="
                width: 50%;
                flex-shrink: 0;
                margin-bottom: 5px;
                font-size: 14px;
              "
            >
              <span>承修单位：</span>
               {{ repairTicketlLevelOne.backUnit }}
            </div>

            <div
              class="item"
              style="
                width: 50%;
                flex-shrink: 0;
                margin-bottom: 5px;
                font-size: 14px;
              "
            >
              <span>操作人：</span>
               {{ repairTicketlLevelOne.repairer }}
            </div>

            <div
              class="item"
              style="
                width: 100%;
                flex-shrink: 0;
                margin-bottom: 5px;
                font-size: 14px;
              "
            >
              <span>项目名称：</span>
                {{ repairTicketlLevelOne.backPro }}
            </div>

            <div
              class="item"
              style="
                width: 50%;
                flex-shrink: 0;
                margin-bottom: 5px;
                font-size: 14px;
              "
            >
              <span>委托单位：</span>
                {{ repairTicketlLevelOne.backUnit }}
            </div>

            <div class="item" style="width: 50%; flex-shrink: 0; margin-bottom: 5px; font-size: 14px;">
              <span>验收人：</span>
            </div>
          </div>
          <el-table
            :data="repairTicketDevice"
            class="table"
            style="margin-top: 20px; width: 1000px; padding-bottom: 1px"
            border
          >
            <!--          <el-table-column type="selection" width="55" align="center" />-->
            <!-- <el-table-column label="序号" align="center" type="index" row="2" /> -->
            <el-table-column label="名称" align="center" prop="typeName" />
            <el-table-column label="型号" align="center" prop="type"/>
            <el-table-column label="编号" align="center" prop="code" >
              <template slot-scope="scope">
                  <div v-if="scope.row.manageType==1" >计数</div>
                  <div v-if="scope.row.manageType==0" style="color: blue;" @click="handleView(scope.row)">详见附件</div>
              </template>
            </el-table-column>
            <el-table-column label="单位" align="center" prop="unitName" />
            <el-table-column label="报修数量" align="center" prop="repairNum" />
            <el-table-column label="修复数量" align="center" prop="repairedNum" />
            <el-table-column label="报废数量" align="center" prop="scrapNum" />
            <el-table-column label="维修费小计" align="center" prop="totalCost" >
              <template slot-scope="scope">
                  <div >{{"¥"+" "+scope.row.totalCost}}</div>
              </template>
            </el-table-column>
            <el-table-column label="报废图片" align="center" >
              <template slot-scope="scope">
                  <div v-if="scope.row.fileList.length==0" ></div>
                  <div v-if="scope.row.fileList.length!=0" style="color: blue;" @click="picturesPreview(scope.row)">{{scope.row.fileList.length}}</div>
              </template>
            </el-table-column>
          </el-table>
            <div              
            class="tabelAll"
            >
                <div class="columnAllNum">总计</div>
                <div class="columnAll">
                {{ repairNum1 }}
                </div>
                <div class="columnAll">
                {{ repairNum2 }}
                </div>
                <div class="columnAll">
                {{ repairNum3 }}
                </div>
                <div class="columnAll">
                {{ "¥"+" "+repairNum4 }}
                </div>
                <div class="columnAll">
               
                </div>
            </div>
          <div
            class="fillIn"
            style="
              margin-top: 20px;
              display: flex;
              justify-content: space-between;
            "
          >
            <div class="item" style="text-align: center;width: 100% ;font-weight: 600; font-size: 14px" >
              <div>
                <span>维修内容</span>
              </div>
            </div>
          </div>

          <el-table
            :data="repairTicketPart"
            class="table"
            style="margin-top: 20px; width: 1000px; padding-bottom: 1px"
            border
          >
            <el-table-column label="修理项目" align="center" prop="repairName" >
              <template slot-scope="scope">
                <div>{{ scope.row.maTypeName }}-{{ scope.row.typeName }}</div>
              </template> 
            </el-table-column>
            <el-table-column label="配件名称 * 数量 / 单价" align="center" prop="specificationType" >
              <template slot-scope="scope">
                <div>{{ scope.row.partName + " " }}*{{ " "+scope.row.partNum + " " }}/{{" ¥"+" "}}{{ scope.row.partPrice }}</div>
              </template> 
            </el-table-column>
          </el-table>
            <div              
            class="tabelAll"
            >
                <div class="columnAllNumTwo">合计</div>
                <div class="columnAllNumThree">
                   {{ "¥"+" "+acountAll }}
                </div>
            </div>

            <div class="fillIn" style=" margin-top: 20px; display: flex; justify-content: space-between;">
            <div class="item" style="width: 50%">
              <div>
                <span>负责人：</span>
              </div>
            </div>

            <div class="item" style="width: 50%">
              <div>
                <span>完成日期：</span>
                 {{ repairTicketlLevelOne.finishTime }}
              </div>
            </div>

          </div>

          <div class="fillIn" style="margin-top: 20px; 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 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="openPrint = false">关 闭</el-button>
      </div>
    </el-dialog>

     <!-- 查看弹窗 -->
    <el-dialog
      :title="titleView"
      :visible.sync="showView"
      width="1000px"
      append-to-body
    >
    <div style="overflow-y: scroll">
      <vue-easy-print tableShow ref="remarksPrintRefView" class="print">
        <div
            class="title"
            style="text-align: center; font-weight: 600; font-size: 16px; margin-bottom:20px"
          >
            维修任务单编号明细
        </div>
        <el-table :data="getListViewInfo" width="600px" height="450">
          <el-table-column align="center" label="序号" type="index" width="55" />
          <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="code"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="状态"
            align="center"
            prop="status"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.status=='1'" >已完成</div>
              <div v-if="scope.row.status=='0'">未完成</div>
            </template>
          </el-table-column>
        </el-table>
      </vue-easy-print>
      </div>
        <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="printView">打 印</el-button>
        <el-button @click="showView = false">关 闭</el-button>
        </div>
    </el-dialog>

     <!--多图片查看弹窗 -->
    <el-dialog :visible.sync="dialogVisibles">
      <div style="width: 100%;height: 600px;display: flex;justify-content: space-between;align-items: center;">
        <div style="width: 5%;height: 100%;display: flex;align-items: center;justify-content: center;" >
          <i class="el-icon-caret-left" style="font-size: 32px;" @click="imgIndex=imgIndex-1" v-show="imgIndex>0"></i>
        </div>
        <div style="width: 90%;height: 100%;overflow: hidden;">
          <img v-for="(item,index) of dialogImageUrls" :key="index" v-show="imgIndex==index" width="100%" height="500px" :src="item.url" alt />
        </div>
        <div style="width: 5%;height: 100%;display: flex;align-items: center;" >
          <i class="el-icon-caret-right" style="font-size: 32px;" @click="imgIndex=imgIndex+1" v-show="(imgIndex+1)<(dialogImageUrls.length)"></i>
        </div>
      </div>
      
    </el-dialog>
    </div>
</template>

<script>
  
import {
    getRepairTaskList,
    getRepaiDocumentInfo,
    rejectRepair,
    sendList,
    getRepairDocumentInfo,
} from '@/api/repair/repair.js' 

 import { getInfo, h } from '@/api/login'

import vueEasyPrint from "vue-easy-print";
export default {
    name: '',
    dicts: ['repair_task_status'], 
    data() {
        return { 
            type: '',
            isShowOneFlag: false,
            priKey: '',
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 字典表格数据
            tableList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                keyWord: '', //关键字
                unitId: '', //单位id
                proId: '', //工程id
                taskStatus: '', //状态
                typeId: '', //工机具类型
                time: [],// 日期范围
                agreementCode: '', //协议
                startTime: '',
                endTime: '', 
            },
            companyId: '', 
            unitList: [],
            proList: [],
            //维修单显示
            openPrint:false,
            printData: {},
            //选择的taskId数组
            ids:[],
            taskList:[],
            //维修任务单上部分数据
            repairTicketlLevelOne:{},
            //中间维修设备
            repairTicketDevice:[],
            //下方维修内容
            repairTicketPart:[],
            //报修数量计和
            repairNum1:0,
            //修复数量计和
            repairNum2:0,
            //报废数量计和
            repairNum3:0,
            //维修费小计计和
            repairNum4:0,
            //维修任务单详见附件
            showView:false,
            //维修任务单详见附件标题
            titleView:'',
            //维修任务单详见附件列表
            getListViewInfo:[],
            //维修内容列表合计金额
            acountAll:0,
            //多图片查看弹窗
            dialogImageUrls: [],
            dialogVisibles: false,
            imgIndex:0,
        }
    },
    created() {
        this.getList();
        // this.initSelectData()
        // this.InitIGetInfo()
    },
    components: { vueEasyPrint },
    methods: {
        //人员信息
        InitIGetInfo() {
            getInfo().then((res) => {
                this.companyId = res.user.companyId
                this.getList()
            })
        },
        /** 查询列表 startTime,结束日期endTime  */
        async getList() {
            this.loading = true
            if (this.queryParams.time && this.queryParams.time.length > 0) {
                this.queryParams.startTime = this.queryParams.time[0]
                this.queryParams.endTime = this.queryParams.time[1]
            }
            try {
                let params = {
                    companyId: this.companyId,
                    ...this.queryParams,
                }
                const res = await getRepairTaskList(params)
                this.tableList = res.rows
                this.total = res.total
                this.loading = false
            } catch (error) {}
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams.time = []
            // this.resetForm("queryForm");
            this.$refs.queryForm.resetFields() 
            this.handleQuery()
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.$emit('handelAdd')
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.$emit("handelEdit",row);
        },
         /** 查看按钮操作 */
        handleSee(row) {
            this.$emit("handelView",row);
        }, 
        /** 删除按钮操作 */
        handleReject(row) {
            const taskIds = row.taskId || this.ids;
            this.$modal.confirm('是否确认驳回该退料？').then(function() {
                return rejectRepair(taskIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {});
            
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                'material/repair/export',
                {    
                    ...this.queryParams,
                    startTime:this.queryParams.time[0],
                    endTime:this.queryParams.time[1]
                },
                `维修任务单_${new Date().getTime()}.xlsx`,
            )
        },
        //查看维修任务单
       async handlePrint(row) {
            const res = await getRepairDocumentInfo(row.taskId);
            this.repairTicketlLevelOne = res.data.repairTaskInfo;
            this.repairTicketDevice = res.data.repairDeviceSummaryList;
            //维修设备列表计算
            if(this.repairTicketDevice.length>0){
              let num1 = 0;  //报废数量
              let num2 = 0;  //修复数量
              let num3 = 0;  //报废数量
              let num4 = 0.0;  //维修费小计
            this.repairTicketDevice.forEach(item=>{
              num1 = num1 + Number(item.repairNum)
              num2 = num2 + Number(item.repairedNum)
              num3 = num3 + Number(item.scrapNum)
              num4 = num4 + item.totalCost
            })
            this.repairNum1 = num1
            this.repairNum2 = num2
            this.repairNum3 = num3
            this.repairNum4 = num4
            }

            //维修内容列表
            this.repairTicketPart = res.data.repairPartArray;

            //维修费小计 计算
            if(this.repairTicketPart.length>0){
              let acount = 0 //维修费用
              this.repairTicketPart.forEach(item=>{
                acount = acount + item.partCost
              })
              this.acountAll = acount;
            }

            this.openPrint = true;
            this.title = "机具设备维修任务单";
        },
        //提交
        handleSend(row){
        if (this.ids.length == 0) {
          this.$alert("请至少勾选一条要提交的数据", "提示", {
            type: "warning",
            confirmButtonText: "确定",
          });
          return;
        } else {
          sendList(this.taskList).then((response) => {
            if (response.code == 200) {
              this.$modal.msgSuccess("通过成功");
            }
            this.getList()
          });
          }
        },

        // 多选框选中数据
        handleSelectionChange(selection) {
          this.taskList = [];
          this.ids = selection.map((item) => item.taskId);
          selection.forEach((item) => {
            this.taskList.push({ taskId: item.taskId });
          });
          this.single = selection.length != 1;
          this.multiple = !selection.length;
          },

        //维修任务单打印
        print() {
          this.$refs.remarksPrintRef.print();
        },

        //详见附件打印
        printView() {
          this.$refs.remarksPrintRefView.print();
        },

        handleView(row){
          this.getListViewInfo = row.repairDeviceList
          console.log('this.getListViewInfo',this.getListViewInfo)
          this.titleView = "查看"
          this.showView = true;
        },

        //多图片查看
        picturesPreview(row) {
          this.dialogImageUrls = [];
            row.fileList.forEach(item => {
                this.dialogImageUrls.push({
                  name: item.name,
                  url: item.url
                })
            });
            this.imgIndex=0
            this.dialogVisibles = true;
        },

        //是否可用勾选框
        selectable(row) {
          if (row.repairStatusCode == "1") {
            return false;
          } else {
            return true;
          }
        },
        },
}
</script>
<style lang="scss" scoped>
.submit_box {
    display: flex;
    justify-content: flex-start;
    align-items: center;

    .submit_box_title {
        display: flex;
        flex-direction: column;
        margin-left: 15px;

        :first-child {
            font-size: 14px;
            font-weight: 600;
        }

        :last-child {
            margin-top: 6px;
            font-size: 12px;
        }
    }
}

.submit_box_two {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    margin-left: 30%;

    .submit_box_title {
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        font-size: 18px;
        font-weight: 600;
    }
}

::v-deep.el-table .fixed-width .el-button--mini {
    width: 60px !important;
    margin-bottom: 10px;
}
.dialog-footer {
    display: flex;
    justify-content: flex-end;
    align-items: center;
}
.tabelAll {
  display: flex;
  border: 1px solid #dfe6ec;
  margin-bottom: 1px;
  width:1000px;
  border-bottom: 1px solid #dfe6ec;
  border-top: none;
}
.columnAll {
  flex: 1;
  padding: 5px;
  border-left: 1px solid #dfe6ec;
  text-align: center;
  width:11.1%;
}
.columnAllNum {
  padding: 5px;
  text-align: center;
  width:44.4%;
  border-left: none;
}
.columnAllNumTwo {
  flex: 1;
  padding: 5px;
  border-left: 1px solid #dfe6ec;
  text-align: center;
  width:50%;
  border-left: none;
}
.columnAllNumThree {
  flex: 1;
  padding: 5px;
  border-left: 1px solid #dfe6ec;
  text-align: center;
  width:50%;
}
/* 最后一列无右边框 */
.tabelAll .column:last-child {
  border-right: none;
}
</style>
