<template>
    <div class="page-container row-container">
        <!-- 左侧区域 -->
        <section class="list-part-container" style="flex: 3">
            <!-- 搜索区 -->
            <el-form class="list-search-container" :model="queryParams" ref="queryRef" :inline="true"
                style="margin-right: 0px">
                <el-form-item class="section-title" label="包装审核申请" />
                <el-form-item label="生产计划单号：">
                    <el-input placeholder="请输入生产计划单号/批次号" v-model.trim="queryParams.keywords" @keydown.enter.prevent clearable
                        style="width: 200px" />
                </el-form-item>
                <el-form-item label="产品描述：">
                    <el-input placeholder="请输入产品描述" v-model.trim="queryParams.productDescription" @keydown.enter.prevent clearable
                        style="width: 120px" />
                </el-form-item>
                <el-form-item label="审核结果：">
                    <el-select
                    v-model="queryParams.status"
                    clearable
                    placeholder="请选择审核结果"
                    style="width: 150px"
                    >
                    <el-option
                        v-for="item in daywork_item_emamine"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="审核人：">
                    <el-input placeholder="请输入审核人名称" v-model.trim="queryParams.reviewerName" @keydown.enter.prevent clearable
                        style="width: 140px" />
                </el-form-item>
                <el-form-item c label="审核工段：">
                    <el-select-v2 v-model="queryParams.deptId" :options="deptList" placeholder="请选择审核工段"
                        style="width: 120px" />
                </el-form-item>
                <el-form-item label="提交时间：">
                    <el-date-picker v-model="queryParams.startTime" type="date" value-format="YYYY-MM-DD"
                        :editable="false" clearable placeholder="请选择" style="width: 120px" />
                    <span>~</span>
                    <el-date-picker v-model="queryParams.endTime" type="date" value-format="YYYY-MM-DD"
                        :editable="false" clearable placeholder="请选择" style="width: 120px" />
                </el-form-item>
                <el-form-item style="margin-left: 0">
                    <el-button type="info" icon="Search" @click="handleQuery">搜索
                    </el-button>
                </el-form-item>
                <el-form-item style="margin-left: 0">
                    <el-button type="primary" icon="Plus" v-hasPermi="['business:examine:add']" @click="handleAdd">新增
                    </el-button>
                </el-form-item>
                <el-form-item style="margin-left: 0">
                    <el-button type="danger" icon="Delete" v-hasPermi="['business:examine:delete']" :disabled="multiple" @click="handleDeleted">批量删除
                    </el-button>
                </el-form-item>
                <el-form-item style="margin-left: 0">
                    <el-button type="success" icon="Check" v-hasPermi="['business:examine:pass']" :disabled="multiple" @click="handleMultiply('pass')">批量通过
                    </el-button>
                </el-form-item>
                <el-form-item style="margin-left: 0">
                    <el-button type="warning" icon="Close" v-hasPermi="['business:examine:fail']" :disabled="multiple" @click="handleMultiply('fail')">批量不通过
                    </el-button>
                </el-form-item>
            </el-form>

            <div class="el-table-container">
                <div class="el-table-inner-container">
                    <el-table ref="examineTable" :data="examineList" v-loading="examineLoading" row-key="id" @selection-change="handleSelectionChange"
                        highlight-current-row height="100%">
                        <el-table-column type="selection" width="40" align="center" :reserve-selection="true" />
                        <el-table-column type="index" label="序号" width="50" align="center" />
                        <el-table-column label="计划单号" prop="productionPlanNo" width="160" align="center" />
                        <el-table-column label="批次号" prop="lotCode" width="160" align="center" />
                        <el-table-column label="工段" prop="deptName" width="160" align="center" />
                        <el-table-column label="产品描述" prop="productDescription" align="center" />
                        <el-table-column label="审核状态" prop="status" width="60" align="center" >
                            <template #default="scope">
                               <span v-if="scope.row.status == 0" style="color: darkgoldenrod;">待确认</span>
                               <span v-if="scope.row.status == 1" style="color: red;">不通过</span>
                               <span v-if="scope.row.status == 2" style="color: green;">通过</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="审核人" prop="reviewerName" width="160" align="center" />
                        <el-table-column label="提交时间" prop="createTime" width="160" align="center" />
                        <el-table-column label="审核时间" prop="updateTime" width="160" align="center" />
                        <el-table-column label="备注" prop="remark" width="160" align="center" />
                        <el-table-column label="操作" width="80px" align="center">
                <template #default="scope">
                  <el-button link type="primary" @click="handleView(scope.row)" >详细信息</el-button>
                </template>
              </el-table-column>
                    </el-table>
                </div>
            </div>
            <!-- 分页 -->
            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getDayworkItemExamine" />
        </section>
            <!-- 审核详情列表 -->
    <examine-form ref="examineFormRef" @handleSaveSuccess="handleRefreshDetail" />
    <!-- 新增审核申请 -->
    <add-examine-dialog ref="addExamineDialogRef" :multiple-selected="handleMultipleSelectedAddLot" />
    </div>
</template>

<script setup name="examine" >
import {
    listExamine,delExamine,addExamine,multiplyEdit
} from "@/api/business/dayworkItemExamine.js";
import { getDeptListByUserId } from "@/api/business/planDetailSubDetail.js";
import examineForm from "./form.vue";
import addExamineDialog from "./DialogAddExamine.vue";
import { multiply } from "lodash-es";
import { ref } from "vue";
const { proxy } = getCurrentInstance();
/** 字典数组区 */
const { daywork_item_emamine } = proxy.useDict("daywork_item_emamine");


/** 生产批次 */
const examineTable = ref(null);
const examineLoading = ref(false);
const examineList = ref([]);
const selections = []
const multiple = ref(true);
const isDispatch = ref(false)
const total = ref(0);
const ids = ref([])
/**工段 */
const deptList = ref([]);
const loading = ref(false);


const queryParams = ref({
    keywords: null,
    productDescription: null,
    status: "0",
    pageNum: 1,
    pageSize: 10,
    startTime: null,
    endTime:null,
});

/***********************  工段相关事件  ****************************/
function getDept() {
    getNowDate();
    loading.value = true;
    getDeptListByUserId().then((response) => {
        deptList.value = response.data.rows;
        isDispatch.value = response.data.others.isDispatch;
        loading.value = false;
        if (isDispatch.value) {
            deptList.value.unshift({ label: "全部", value: "0" });
        } 
        queryParams.value.deptId = deptList.value[0].value;
        getDayworkItemExamine();
    });
}
function getNowDate() {
    queryParams.value.startTime = proxy.moment().clone().subtract(7, 'days').format("YYYY-MM-DD");
    queryParams.value.endTime = proxy.moment().format("YYYY-MM-DD")
}
//新增审核申请
function handleAdd() {
    proxy.$refs.addExamineDialogRef.open();
}
function handleSelectionChange(selection) {
    selections.value = selection
    ids.value = selection.map((item) => item.id);
    multiple.value = !selections.value.length;
    console.log(multiple.value)
}
//批量通过 不通过
function handleMultiply(data) {
    console.log(data)
    if(data == 'pass'){
    proxy.$modal
      .confirm("是否确定批量审核通过")
      .then(function () {
        let passList = selections.value
        passList.forEach(item => {
          item.status = 2
        });
        multiplyEdit(passList).then(res =>{
        proxy.$refs.examineTable.clearSelection()
        getDayworkItemExamine();
        })
       
      })
    }else{
        proxy.$modal
      .confirm("是否确定批量审核不通过")
      .then(function () {
        let flag = true
        selections.value.forEach(item => {
          if(item.isNextProcess) {
            flag = false
            proxy.$modal.msgError(item.lotCode+"批次已经周转到下序，不能审核不通过");
          }
        });
        if(flag) {
            let passList = selections.value
        passList.forEach(item => {
          item.status = 1
          item.reviewComments = "批量审核不通过"
        });
        multiplyEdit(passList).then(res =>{
        proxy.$refs.examineTable.clearSelection()
        getDayworkItemExamine();
        })
    }
})
}
}

//批量删除
function handleDeleted() {
  proxy.$modal
    .confirm("是否确认删除选中的数据项？")
    .then(function () {
        let flag = true
     //判断如果选中项有已经进行到分选包装的不能删除
     for(let i = 0;i<selections.value.length;i++) {
        if(selections.value[i].isNextProcess){
            flag = false
            proxy.$modal.msgError(selections.value[i].lotCode +"已经进行到分选包装的不能删除");
            return
        }
     }
     if(flag) {
        delExamine(ids.value).then(res =>{
            proxy.$modal.msgSuccess("删除成功");
            getDayworkItemExamine();
        })
     }
    })
}
// 打开审核详情列表
function handleView(row) {
    proxy.$refs.examineFormRef.open(row);
}
function handleRefreshDetail() {
    getDayworkItemExamine();
}
/** 申请列表 */
function getDayworkItemExamine() {
    selections.value = []
    if (queryParams.value.startTime != null) {
        queryParams.value.startTime = proxy.moment(queryParams.value.startTime).format('YYYY-MM-DD 00:00:00')
    }
    if (queryParams.value.endTime != null) {
        queryParams.value.endTime = proxy.moment(queryParams.value.endTime).format('YYYY-MM-DD 23:59:59')
    }
    examineLoading.value = true;
    listExamine(queryParams.value).then((res) => {
        examineList.value = res.rows;
        total.value = res.total;
        examineLoading.value = false;
    });
}
/** 搜索按钮操作 */
function handleQuery() {
    getDayworkItemExamine();
}
//多选带回
function handleMultipleSelectedAddLot(selection) {
    addExamine(selection).then(res =>{
        if(res.code == 200) {
            proxy.$modal.msgSuccess("添加成功");
             getDayworkItemExamine();   
        }
    })
    
}

onMounted(() => {
    getDept();
});
</script>
<style scoped>
.el-form--inline .el-form-item {
    margin-right: 20px;
}
</style>