<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item :label="tLang('pms','维修单编号')" prop="repairCode">
                <el-input v-model="queryParams.repairCode" :placeholder="tLang('common','请输入') + tLang('pms','维修单编号')" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item :label="tLang('pms','维修单名称')" prop="repairName">
                <el-input v-model="queryParams.repairName" :placeholder="tLang('common','请输入') + tLang('pms','维修单名称')" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>

            <el-form-item :label="tLang('device','设备编码')" prop="deviceId">
                <el-input v-model="queryParams.deviceId" :placeholder="tLang('common','请输入') + tLang('device','设备编码')" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item :label="tLang('device','设备名称')" prop="machineryName">
                <el-input v-model="queryParams.machineryName" :placeholder="tLang('common','请输入') + tLang('device','设备名称')" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item :label="tLang('pms','维修结果')" prop="repairResult">
                <el-select v-model="queryParams.repairResult" :placeholder="tLang('common','请选择') + tLang('pms','维修结果')" clearable>
                    <el-option v-for="dict in pms_repair_result" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item :label="tLang('pms','单据状态')" prop="status">
                <el-select v-model="queryParams.status" :placeholder="tLang('common','请选择') + tLang('pms','单据状态')" clearable>
                    <el-option v-for="dict in pms_order_status" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="search" size="mini" @click="handleQuery">{{ tLang('common', '搜索') }}</el-button>
                <el-button icon="refresh" size="mini" @click="resetQuery">{{ tLang('common', '重置') }}</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="plus" size="mini" @click="handleAdd"
                    v-hasPermi="['pms:repair:add']">{{ $t('common.新增') }}</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['pms:repair:edit']">{{ $t('common.修改') }}</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['pms:repair:remove']">{{ $t('common.删除') }}</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="repairList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column :label="tLang('pms','维修单编号')" width="120px" align="center" prop="repairCode" />
            <el-table-column :label="tLang('pms','维修单名称')" width="150px" align="center" prop="repairName"
                :show-overflow-tooltip="true" />
            <el-table-column :label="tLang('device','设备编码')" align="center" prop="deviceCode" />
            <el-table-column :label="tLang('device','设备名称')" align="center" prop="deviceName" />
            <el-table-column :label="tLang('pms','报修日期')" align="center" prop="requireDate" width="140">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.requireDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column :label="tLang('pms','维修完成日期')" align="center" prop="finishDate" width="140">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.finishDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column :label="tLang('pms','验收日期')" align="center" prop="confirmDate" width="140">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.confirmDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column :label="tLang('pms','维修结果')" align="center" prop="repairResult">
                <template #default="scope">
                    <dict-tag :options="pms_repair_result" :value="scope.row.repairResult" />
                </template>
            </el-table-column>
            <el-table-column :label="tLang('pms','维修人员')" align="center" prop="acceptedName" />
            <el-table-column :label="tLang('pms','验收人员')" align="center" prop="confirmName" />
            <el-table-column :label="tLang('common','状态')" align="center" prop="dataStatus" />
            <!-- <el-table-column :label="tLang('pms','')单据状态" align="center" prop="status">
                <template #default="scope">
                    <dict-tag :options="pms_order_status" :value="scope.row.status" />
                </template>
            </el-table-column> -->
            <el-table-column :label="tLang('common','操作')" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button size="mini" type="text" icon="grid" @click="handleOpenLog(scope.row)"
                        v-hasPermi="['pms:repair:log']">{{tLang('pms','记录')}}</el-button>
                    <el-button size="mini" type="text" icon="edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['pms:repair:edit']">{{tLang('common','修改')}}</el-button>
                    <el-button size="mini" type="text" icon="delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['pms:repair:remove']">{{tLang('common','删除')}}</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" v-model="open" width="960px" append-to-body destroy-on-close>
            <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
                <el-row>
                    <el-col :span="8">
                        <el-form-item :label="tLang('pms','维修单编号')" prop="repairCode">
                            <el-input v-model="form.repairCode" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <!-- <el-form-item label-width="10">
                            <el-switch v-model="autoGenFlag" active-color="#13ce66" active-text="自动生成"
                                @change="handleAutoGenChange(autoGenFlag)" v-if="optType != 'view'"
                                :disabled="form.status != 'PREPARE'">
                            </el-switch>
                        </el-form-item> -->
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="tLang('pms','维修单名称')" prop="repairName">
                            <el-input v-model="form.repairName" :placeholder="tLang('common','请输入') + tLang('pms','维修单名称')"
                                :disabled="form.status != 'PREPARE'&&form.status != 'SENDBACK'" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item :label="tLang('pms','选择设备')" prop="deviceId">
                            <el-select v-model="form.deviceId"
                                :placeholder="tLang('common', '请选择') + tLang('device', '设备')" @change="changeDevice"
                                :disabled="form.status != 'PREPARE'&&form.status != 'SENDBACK'">
                                <el-option v-for="item in deviceList" :key="item.value" :label="item.label"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8" v-if="form.status !== 'PREPARE'">
                        <el-form-item :label="tLang('pms','报修人员')" prop="createBy">
                            <el-input v-model="form.createName" :disabled="true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item :label="tLang('pms','报修日期')" prop="requireDate">
                            <el-date-picker clearable v-model="form.requireDate" style="width: 187px" type="date"
                                value-format="YYYY-MM-DD" :placeholder="tLang('common','请选择') + tLang('pms','报修日期')" :disabled="form.status != 'PREPARE'&&form.status != 'SENDBACK'">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item :label="tLang('pms','最晚维修日期')" prop="">
                            <el-date-picker clearable v-model="form.deadline" style="width: 187px" type="date"
                                value-format="YYYY-MM-DD" :placeholder="tLang('common','请选择') + tLang('pms','最晚维修日期')" :disabled="form.status != 'PREPARE'">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                </el-row>

                <el-row>
                    <el-col :span="24">
                        <el-form-item :label="tLang('pms','报修备注')" prop="remark">
                            <el-input v-model="form.remark" type="textarea" :placeholder="tLang('common','请输入') + tLang('pms','报修备注')"
                                :disabled="form.status !== 'PREPARE'&&form.status != 'SENDBACK'" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item :label="tLang('pms','审核备注')" prop="reviewerRemark"                        
                        v-if="form.status != 'PREPARE'&&form.status != 'SENDBACK'">
                            <el-input v-model="form.reviewerRemark" type="textarea" :placeholder="tLang('common','请输入') + tLang('pms','审核备注')"
                                :disabled="!is_reviewer || form.status !== 'APPROVING'" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item :label="tLang('pms','维修人员')" prop="acceptedBy"
                            v-if="form.status == 'APPROVED' || form.status == 'FINISHED' || form.status == 'CONFIRMED' || form.status == 'APPROVING'||form.status == 'NOTPASS'">

                            <el-select v-model="form.acceptedBy" placeholder=""
                                :disabled="!is_reviewer || form.status !== 'APPROVING'">
                                <el-option v-for="item in acceptedByOptions" :key="item.value" :label="item.label"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item :label="tLang('pms','维修完成日期')"
                            v-if="form.status == 'APPROVED' || form.status == 'FINISHED' || form.status == 'CONFIRMED'||form.status == 'NOTPASS'"
                            prop="finishDate">
                            <el-date-picker clearable v-model="form.finishDate" style="width: 187px" type="date"
                                value-format="YYYY-MM-DD" :placeholder="tLang('common','请选择') + tLang('pms','维修完成日期')"
                                :disabled="!is_repairman || (form.status !== 'APPROVED'&&form.status !== 'NOTPASS')">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item :label="tLang('pms','维修结果')" prop="repairResult"
                            v-if="form.status == 'APPROVED' || form.status == 'FINISHED' || form.status == 'CONFIRMED'||form.status == 'NOTPASS'">
                            <el-radio-group v-model="form.repairResult"
                                :disabled="!is_repairman || (form.status !== 'APPROVED'&&form.status !== 'NOTPASS')">
                                <el-radio v-for="dict in pms_repair_result" :key="dict.value" :label="tLang('pms',dict.value)">
                                    {{ tLang('pms',dict.value) }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item :label="tLang('pms','维修备注')" prop="repairmanRemark"
                            v-if="form.status == 'APPROVED' || form.status == 'FINISHED' || form.status == 'CONFIRMED'||form.status == 'NOTPASS'">
                            <el-input v-model="form.repairmanRemark" type="textarea" :placeholder="tLang('common','请输入') + tLang('pms','维修备注')"
                                :disabled="!is_repairman || (form.status !== 'APPROVED'&&form.status !== 'NOTPASS')" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item :label="tLang('pms','验收人员')" prop="confirmBy"
                            v-if="form.status == 'APPROVED' || form.status == 'FINISHED' || form.status == 'CONFIRMED' || form.status == 'APPROVING'||form.status == 'NOTPASS'">
                            <el-select v-model="form.confirmBy" placeholder=""
                                :disabled="!is_reviewer || form.status !== 'APPROVING'">
                                <el-option v-for="item in confirmByOptions" :key="item.value" :label="item.label"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item :label="tLang('pms','验收日期')" v-if="form.status == 'FINISHED' || form.status == 'CONFIRMED'"
                            prop="confirmDate">
                            <el-date-picker clearable v-model="form.confirmDate" style="width: 187px" type="date"
                                value-format="YYYY-MM-DD" :placeholder="tLang('common','请选择') + tLang('pms','验收日期')"
                                :disabled="!is_inspector || form.status != 'CONFIRMED'">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item :label="tLang('pms','验收备注')" prop="inspectorRemark"
                            v-if="form.status == 'FINISHED' || form.status == 'CONFIRMED'">
                            <el-input v-model="form.inspectorRemark" type="textarea" :placeholder="tLang('common','请输入') + tLang('pms','验收备注')"
                                :disabled="!is_inspector || form.status != 'CONFIRMED'" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <el-divider content-position="center">{{tLang('pms','维修内容')}}</el-divider>
            <el-card shadow="always" class="box-card">
                <Repairline ref="line" :repairlineList="form.line" :status="form.status"></Repairline>
            </el-card>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="warning" v-if="(form.status == 'PREPARE'||form.status == 'SENDBACK') && optType != 'view' && (userStore.id == form.createBy||form.createBy == null)"
                        @click="submitForm('PREPARE')">{{tLang('common','暂存')}}</el-button>
                    <el-button type="primary" @click="submitForm('APPROVING')"
                        v-if="(form.status == 'PREPARE'||form.status == 'SENDBACK') && optType != 'view' && (userStore.id == form.createBy||form.createBy == null)">{{ tLang('common','提 交') }}</el-button>

<!-- 
                        
//判断当前用户是否有审核权限
let is_reviewer = ref(false)
//判断当前用户是否有维修权限
let is_repairman = ref(false)
//判断当前用户是否有验收权限
let is_inspector = ref(false)
-->
                    <el-button type="primary" @click="submitForm('PREPARE')"
                        v-if="form.status == 'APPROVING' && optType != 'view' && userStore.id == form.createBy">{{tLang('common','撤回')}}</el-button>

                    <el-button type="danger" @click="submitForm('SENDBACK')"
                        v-if="form.status == 'APPROVING' && optType != 'view'&&is_reviewer">{{tLang('common','退回')}}</el-button>

                    <el-button type="warning" @click="submitForm('STOP')"
                        v-if="form.status == 'APPROVING' && optType != 'view'&&is_reviewer">{{tLang('common','终止')}}</el-button>

                    <el-button type="success" @click="submitForm('APPROVED')"
                        v-if="form.status == 'APPROVING' && optType != 'view'&&is_reviewer">{{tLang('common','审批通过')}}</el-button>

                    <el-button type="success" @click="submitForm('CONFIRMED')"
                        v-if="(form.status == 'APPROVED'||form.status == 'NOTPASS') && optType != 'view'&&is_repairman">{{tLang('common','完成维修')}}</el-button>

                    <el-button type="success" @click="submitForm('FINISHED')"
                        v-if="form.status == 'CONFIRMED' && optType != 'view'&&is_inspector">{{tLang('common','完成验收')}}</el-button>
                    <el-button type="danger" @click="submitForm('NOTPASS')"
                        v-if="form.status == 'CONFIRMED' && optType != 'view'&&is_inspector">{{tLang('common','验收未通过')}}</el-button>
                    <el-button @click="cancel">{{tLang('common','取 消')}}</el-button>
                </div>
            </template>

        </el-dialog>
        <RepairLog v-model="logOpen" :repairCode="logRepairCode"></RepairLog>
    </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch, toRefs, getCurrentInstance } from 'vue';
import Repairline from './line.vue'
import RepairLog from './log.vue'
import { listRepair, getRepair, delRepair, addRepair, updateRepair } from "@/api/pms/repair";
import { genCode } from "@/api/system/autocode/rule"
import { listDevice } from "@/api/pms/device.js";
import { selectRepairUser } from "@/api/system/repair";

import useUserStore from '@/store/modules/user'

const { proxy } = getCurrentInstance();

const { pms_repair_result, pms_order_status } = proxy.useDict("pms_repair_result", "pms_order_status");
let userStore = useUserStore();

let machinerySelectRef = ref(null)
let autoGenFlag = ref(false)
let optType = ref(undefined)
// 遮罩层
let loading = ref(true)
// 选中数组
let ids = ref([])
// 非单个禁用
let single = ref(true)
// 非多个禁用
let multiple = ref(true)
// 显示搜索条件
let showSearch = ref(false)
// 总条数
let total = ref(0)
// 设备维修单表格数据
let repairList = ref([])
// 弹出层标题
let title = ref("")
// 是否显示弹出层
let open = ref(false)
//记录日志弹出层
let logOpen = ref(false)
//记录日志维修单编号
let logRepairCode = ref("")


// 查询参数
let queryParams = reactive({
    pageNum: 1,
    pageSize: 10,
    repairCode: null,
    repairName: null,
    machineryId: null,
    deviceId: null,
    machineryName: null,
    machineryBrand: null,
    machinerySpec: null,
    machineryTypeId: null,
    requireDate: null,
    finishDate: null,
    confirmDate: null,
    repairResult: null,
    acceptedBy: null,
    confirmBy: null,
    status: null,
})
// 表单参数
let form = ref({})
let formRef = ref(null)
let deviceList = ref([])
// 表单校验
let rules = {
    repairCode: [
        { required: true, message: proxy.tLang('pms','维修单编号')  + proxy.tLang('validate','不能为空'), trigger: "blur" }
    ],
    deviceId: [
        { required: true, message: proxy.tLang('device','设备')  + proxy.tLang('validate','不能为空'), trigger: "blur" }
    ],
    requireDate: [
        { required: true, message: proxy.tLang('common','请选择') + proxy.tLang('pms','报修日期'), trigger: "blur" },
        //开始时间不能大于结束时间
        { validator: validateStarttime, trigger: 'change' }
    ],
    deadline: [
        { required: true, message: proxy.tLang('common','请选择') + proxy.tLang('pms','最晚维修日期'), trigger: "blur" },
        //开始时间不能大于结束时间
        { validator: validateStarttime, trigger: 'change' }
    ],
    finishDate: [
        { required: true, message: proxy.tLang('common','请选择') + proxy.tLang('pms','维修完成日期'), trigger: "blur" },
        //开始时间不能大于结束时间
        { validator: validateStarttime, trigger: 'change' }
    ],
    confirmDate: [
        { required: true, message: proxy.tLang('common','请选择') + proxy.tLang('pms','验收日期'), trigger: "blur" },
        //开始时间不能大于结束时间
        { validator: validateStarttime, trigger: 'change' }
    ],
    repairResult: [
        { required: true, message: proxy.tLang('common','请选择') + proxy.tLang('pms','维修结果'), trigger: "blur" }
    ],
    // acceptedBy: [
    //     { required: true, message: '维修人员不能为空', trigger: "blur" }
    // ],
    // confirmBy: [
    //     { required: true, message: '验收人员不能为空', trigger: "blur" }
    // ],
}


//时间验证
function validateStarttime(rule, value, callback) {
    let requireDate = new Date(form.value.requireDate).getTime();
    let finishDate = new Date(form.value.finishDate).getTime();
    let confirmDate = new Date(form.value.confirmDate).getTime();
    if (requireDate > 0 && finishDate > 0 && requireDate > finishDate) {
        callback(new Error('报修日期不能大于维修完成日期'));
    } else if (finishDate > 0 && confirmDate > 0 && finishDate > confirmDate) {
        callback(new Error('维修完成日期不能大于验收日期'));
    } else {
        callback();
    }
    
}




//查看维修记录
function handleOpenLog(row) {
    logRepairCode.value = row.repairCode
    logOpen.value = true
}


//获取设备列表
function getDeviceList() {
    listDevice({ pageSize: 100 }).then(response => {
        deviceList.value = response.rows.map(item => {
            return {
                value: +item.id,
                label: item.name,
                brand: item.brand,
            }
        })
    })
}
//设备选择
function changeDevice(value) {
    let device = deviceList.value.find(item => item.value === value);
    form.value.machineryId = device.value;
    form.value.machineryName = device.label;
    form.value.machineryBrand = device.brand;
}


/** 查询设备维修单列表 */
function getList() {
    loading.value = true;
    listRepair(queryParams).then(response => {
        repairList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

let acceptedByOptions = ref([])
let confirmByOptions = ref([])
let reviewerByOptions = ref([])

//判断当前用户是否有审核权限
let is_reviewer = ref(false)
//判断当前用户是否有维修权限
let is_repairman = ref(false)
//判断当前用户是否有验收权限
let is_inspector = ref(false)
//获取维修人员列表
function acceptedByChange() {
    selectRepairUser({ flow: 'repairman', pageSize: 100 }).then(response => {
        acceptedByOptions.value = response.rows.map(item => {
            return {
                value: item.userId,
                label: item.nickName,
            }
        })
        is_repairman.value = acceptedByOptions.value.some(item => item.value == userStore.id)
    })
    
}

//获取验收人员列表
function confirmByChange() {
    selectRepairUser({ flow: 'Inspector', pageSize: 100 }).then(response => {
        confirmByOptions.value = response.rows.map(item => {
            return {
                value: item.userId,
                label: item.nickName,
            }
        })
        is_inspector.value = confirmByOptions.value.some(item => item.value == userStore.id)
    })
}

//获取审核人员列表
function reviewerByChange() {
    selectRepairUser({ flow: 'reviewer', pageSize: 100 }).then(response => {
        reviewerByOptions.value = response.rows.map(item => {
            return {
                value: item.userId,
                label: item.nickName,
            }
        })
        is_reviewer.value = reviewerByOptions.value.some(item => item.value == userStore.id)
    })
}







// 取消按钮
function cancel() {
    open.value = false;
    // reset();
}

// 表单重置
function reset() {
    form.value = {
        id: null,
        repairCode: null,
        repairName: null,
        machineryId: null,
        deviceId: null,
        machineryName: null,
        machineryBrand: null,
        machinerySpec: null,
        machineryTypeId: null,
        requireDate: null,
        finishDate: null,
        confirmDate: null,
        repairResult: "",
        acceptedBy: null,
        confirmBy: null,
        status: 'PREPARE',
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        line: [],
        reviewerRemark: null,
        repairmanRemark: null,
        inspectorRemark: null,
        deadline: null
    }
    autoGenFlag.value = false;
    proxy.resetForm("formRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryForm");
    handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id)
    single.value = selection.length !== 1
    multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = proxy.tLang('common','添加') + proxy.tLang('pms','设备维修单');
    handleAutoGenChange(true)
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const repairId = row.id || ids.value
    getRepair(repairId).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = proxy.tLang('common','修改') + proxy.tLang('pms','设备维修单');
    });
}

/** 提交按钮 */
function submitForm(formStatus) {
    formRef.value.validate(valid => {
        if (valid) {
            // form.value.status = formStatus;

            let requestForm = JSON.parse(JSON.stringify(form.value));
            requestForm.status = formStatus;
            if (form.value.id != null) {
                updateRepair(requestForm).then(response => {
                    proxy.$modal.msgSuccess(proxy.tLang('common', '修改成功'));
                    open.value = false;
                    getList();
                });
            } else {
                addRepair(requestForm).then(response => {
                    proxy.$modal.msgSuccess(proxy.tLang('common', '新增成功'));
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const repairIds = row.id || ids.value;
    proxy.$modal.confirm(proxy.tLang('tip', '确定删除选中记录？') + repairIds).then(function () {
        return delRepair(repairIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess(proxy.tLang('common', '删除成功'));
    }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('dv/repair/export', {
        ...queryParams
    }, `repair_${new Date().getTime()}.xlsx`)
}
//设备资源选择弹出
function handleMachineryAdd() {
    machinerySelectRef.value.showFlag = true;
}
//自动生成编码
function handleAutoGenChange(autoGenFlag) {
    if (autoGenFlag) {
        genCode('REPAIR_CODE').then(response => {
            form.value.repairCode = response;
        });
    } else {
        form.value.repairCode = null;
    }
}
onMounted(() => {
    getList();
    getDeviceList();
    acceptedByChange();
    confirmByChange();
    reviewerByChange();
})
</script>

<style lang="scss" scoped></style>
