<template>
    <basic-container>
        <avue-crud :option="option" v-model:search="search" :table-loading="loading" :data="data" v-model="form" v-model:page="page" ref="crud"
            @search-change="searchChange" @search-reset="searchReset"
            @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
            @refresh-change="refreshChange" @on-load="onLoad">
            <template #menu-left>
                <el-button type="primary" @click="addInfo()">新增
                </el-button>
                <el-button type="danger" @click="handleDelete">删 除
                </el-button>
                
                <el-button type="primary" @click="handleSetting">批量设置
                </el-button>
            </template>
            <template #jobType="scope">
                <span v-if="scope.row.jobType == 1">{{scope.row.jobTypeWeekValue.split(',').length + '次/周'}}</span>
                <span v-if="scope.row.jobType == 2">{{scope.row.jobTypeMonthValue.split(',').length + '次/月'}}</span>
                <span v-if="scope.row.jobType == 3">{{scope.row.jobTypeYearValue.split(',').length + '次/年'}}</span>
            </template>
            <template #temperatureType="scope">
                <span v-if="scope.row.temperatureType == 2">{{scope.row.temperatureInRoom}}</span>
                <span v-if="scope.row.temperatureType == 3">{{scope.row.temperatureEnv}}</span>
                <span v-if="scope.row.temperatureType == 4">{{scope.row.temperatureRangeLeft}} ~ {{scope.row.temperatureRangeRight}}</span>
            </template>
            <template #phType="scope">
                <span v-if="scope.row.phType == 2">{{scope.row.phRangeLeft}} ~ {{scope.row.phRangeRight}}</span>
            </template>
            <template #menu-right>
                <el-button type="primary" @click="handleImport">导入
                </el-button>
            </template>
            <template #menu="{ row }">
                <el-button type="text" @click="handleDetail(row)">详情</el-button>
                <el-button type="text" @click="handleEdit(row)">修改</el-button>
                <el-button type="text" @click="rowDel(row)">删除</el-button>
                <el-button type="primary" text plain style="border: 0; background-color: transparent !important"
                    size="medium" @click.stop="handleIssue(row)">下发</el-button>
            </template>
        </avue-crud>

        <!-- <el-dialog title="导入" append-to-body v-model="excelBox" width="555px">
            <avue-form :option="excelOption" v-model="excelForm" :upload-after="uploadAfter">
                <template #excelTemplate>
                    <el-button type="primary" @click="handleTemplate">
                        点击下载<i class="el-icon-download el-icon--right"></i>
                    </el-button>
                </template>
            </avue-form>
        </el-dialog> -->
        <el-dialog title="批量设置" append-to-body v-model="setBox" width="555px">
            <el-form v-model="settingForm" label-width="80px">
                <el-form-item label="主管工艺">
                    <el-select v-model="settingForm.processUserId">
                        <el-option v-for="item in managerList" :key="item.id" :label="item.realName" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="化验人">
                    <el-select v-model="settingForm.testUserId">
                        <el-option v-for="item in personList" :key="item.id" :label="item.realName" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="班组">
                    <el-select v-model="settingForm.teamId">
                        <el-option v-for="item in teamList" :key="item.id" :label="item.tsName" :value="item.id" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="setBox = false">取 消</el-button>
                    <el-button type="primary" @click="submitSeeting">确 定</el-button>
                </span>
            </template>
        </el-dialog>

        <!-- 下发选择人员 -->
        <dispatcherDialog v-if="isOpen" :row-id="rowId" :showDialog="isOpen" @closeDialog="closeDialog"></dispatcherDialog>

        <!-- 新增  编辑  -->
         <addEditInfoDialog v-if="isInfoOpen" :type="infoType" :row-id="rowId" :show-dialog="isInfoOpen" @closeDialog="closeDialog"></addEditInfoDialog>

        <!-- 导入 -->
        <basic-import v-if="isShowImport" title="导入" :isShow="isShowImport"
            templateUrl="/blade-desk/QA/LiquidTank/downloadExcelTemplate"
            templateName="槽液信息模板.xlsx"
            importUrl="/blade-desk/QA/LiquidTank/importExcel"
            @closeDialog="closeDialog">
        </basic-import>
    </basic-container>
</template>

<script>
import dispatcherDialog from './components/dispatcherDialog.vue'
import addEditInfoDialog from './components/addEditInfoDialog.vue'
import basicImport from '@/components/basic-import/main.vue'
// import calculateNeedValuesByFormula fro/m '@/utils/calculateNeedValuesByFormula'
import {getList,getDetail,deleteInfo,getManager,getPerson,getTeam,setBatch} from '@/api/tankSolutionSystem/info'
export default {
    components: { dispatcherDialog, addEditInfoDialog,basicImport },
    data() {
        return {
            months: [
                '一月', '二月', '三月', '四月', '五月', '六月',
                '七月', '八月', '九月', '十月', '十一月', '十二月'
            ],
            daysDatas: [
                '1', '2', '3', '4', '5', '6',
                '7', '8', '9', '10', '11', '12', , '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'
            ],
            isShowImport:false,
            excelBox: false,
            loading: false,
            setBox: false,
            data: [],
            settingForm: {},
            rowId:'',
            infoType:'',
            checkList: [1, 2, 3, 4, 5, 6, 7],
            option: {
                height: 'auto',
                calcHeight: 32,
                tip: false,
                // size: 'medium',
                simplePage: true,
                searchShow: true,
                searchMenuSpan: 6,
                searchIcon: true,
                searchIndex: 3,
                tree: false,
                border: true,
                index: true,
                selection: true,
                viewBtn: false,
                delBtn: false,
                editBtn:false,
                addBtn: false,
                editBtnText: '修改',
                addBtnIcon: ' ',
                viewBtnIcon: ' ',
                delBtnIcon: ' ',
                editBtnIcon: ' ',
                viewBtnText: '详情',
                labelWidth: 140,
                menuWidth: 180,
                dialogWidth: 1200,
                dialogClickModal: false,
                searchEnter: true,
                excelBtn: false,
                filterBtn: true,
                searchShowBtn: false,
                columnSort: true,
                excelBtn: true,
                columnSort: true,
                index: false,
                showOverflowTooltip: true,
                searchLabelPosition: 'left',
                searchLabelPosition: 'left',
                searchGutter: 24,
                searchSpan: 6,
                menuAlign: 'center',
                gridBtn: false,
                searchMenuPosition: 'right',
                align: 'center',
                column: [
                    {
                        label: '作业中心',
                        prop: 'wcName',
                        type: 'select',
                        sortable: true,
                        width: 150,
                        filter: true,
                        span: 8,
                        search: true,
                        rules: [
                            {
                                required: true,
                                message: '请输入作业中心',
                                trigger: 'blur',
                            },
                        ],
                        // dicUrl: '/blade-desk/bsWorkCenter/getList',
                        dicUrl:'/blade-desk/BA/WorkCenter/listForSelect',
                        props: {
                            label: 'wcName',
                            value: 'id',
                        },
                    },
                    {
                        label: '作业槽',
                        prop: 'workTankName',
                        search: true,
                        type: 'select',
                        sortable: true,
                        width: 120,
                        filter: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入槽号',
                                trigger: 'blur',
                            },
                        ],
                        dicUrl: '/blade-desk/BA/WorkTank/listForSelect',
                        props: {
                            label: 'name',
                            value: 'id',
                        },
                    },
                    {
                        label: '药品名称',
                        prop: 'drugName',
                        search: false,
                        type: 'select',
                        sortable: true,
                        width: 120,
                        filter: true,
                        span: 8,
                        dicData: [
                            {
                                label: '药品一',
                                value: 0
                            },
                            {
                                label: '药品二',
                                value: 1
                            }
                        ],
                    },
                    {
                        label: '药品物料号',
                        prop: 'drugMaterialCode',
                        search: false,
                        type: 'select',
                        sortable: true,
                        width: 120,
                        filter: true,
                        span: 8,
                        hide: true,
                        rules: [
                            {
                                required: true,
                                message: '请输入药品物料号',
                                trigger: 'blur',
                            },
                        ],
                        dicData: [
                            {
                                label: '物料一',
                                value: 0
                            },
                            {
                                label: '物料二',
                                value: 1
                            }
                        ],
                    },
                    {
                        label: '药品物料名称',
                        prop: 'drugMaterialName',
                        disabled: true,
                        search: false,
                        sortable: true,
                        width: 150,
                        filter: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入药品物料名称',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '分析项目',
                        prop: 'testElement',
                        search: true,
                        sortable: true,
                        width: 120,
                        filter: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入分析项目',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '规范上下限',
                        prop: 'proContent',
                        search: false,
                        sortable: true,
                        width: 120,
                        filter: true,
                        hide: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入规范上下限',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '规范上限',
                        prop: 'normValueMax',
                        search: false,
                        sortable: true,
                        width: 120,
                        filter: true,
                        display: false,
                        // hide:true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入目标上限',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '规范下限',
                        prop: 'normValueMin',
                        search: false,
                        sortable: true,
                        width: 120,
                        filter: true,
                        display: false,
                        // hide:true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入目标上限',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '目标上下限',
                        prop: 'targetLimit',
                        search: false,
                        sortable: true,
                        width: 120,
                        filter: true,
                        hide: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入目标上限',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '目标上限',
                        prop: 'targetValueMax',
                        search: false,
                        sortable: true,
                        width: 120,
                        filter: true,
                        addDisplay: false,
                        viewDisplay: false,
                        editDisplay: false,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入目标上限',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '目标下限',
                        prop: 'targetValueMin',
                        search: false,
                        sortable: true,
                        addDisplay: false,
                        viewDisplay: false,
                        editDisplay: false,
                        width: 120,
                        filter: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入目标下限',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '目标值',
                        prop: 'targetValue',
                        search: false,
                        sortable: true,
                        width: 120,
                        filter: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入目标值',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '体积',
                        prop: 'volume',
                        search: false,
                        sortable: true,
                        width: 120,
                        filter: true,
                        span: 8,

                    },
                    {
                        label: '测量值单位',
                        prop: 'testUnit',
                        search: false,
                        sortable: true,
                        width: 120,
                        filter: true,
                        span: 8,

                    },
                    {
                        label: '加药量单位',
                        prop: 'addUnit',
                        search: false,
                        sortable: true,
                        width: 120,
                        filter: true,
                        span: 8,

                    },
                    {
                        label: '添加点',
                        prop: 'fillingLocation',
                        search: false,
                        sortable: true,
                        width: 120,
                        filter: true,
                        span: 8,

                    },
                    {
                        label: '加药量计算公式',
                        prop: 'addDrugFormulaName',
                        search: false,
                        type: 'select',
                        sortable: true,
                        width: 160,
                        filter: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请选择计算公式',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '添加后理论值公式',
                        prop: 'afterTheoryFormulaName',
                        span: 13,
                        width: 150,
                        span: 8,
                    },
                    {
                        label: '温度',
                        prop: 'temperatureType',
                        span: 12,
                    },
                    {
                        label: 'PH值',
                        prop: 'phType',
                        span: 8,
                        width: 120,
                        span: 12,
                    },
                    {
                        label: '化验频率',
                        prop: 'jobType',
                        search: false,
                        sortable: true,
                        addDisplay: false,
                        viewDisplay: false,
                        editDisplay: false,
                        width: 120,
                        filter: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入分析周期',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '测试周期(天)',
                        prop: 'analysisCycle',
                        search: false,
                        sortable: true,
                        type: 'date',
                        width: 140,
                        filter: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入有效期',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '有效期',
                        prop: 'validDate',
                        search: false,
                        sortable: true,
                        type: 'date',
                        width: 120,
                        filter: true,
                        span: 8,
                        display: false,
                        rules: [
                            {
                                required: true,
                                message: '请输入有效期',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '上次化验日期',
                        prop: 'lastTest',
                        search: false,
                        sortable: true,
                        addDisplay: false,
                        viewDisplay: false,
                        editDisplay: false,
                        width: 150,
                        filter: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入上次化验日期',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '化验人',
                        prop: 'testUserRealName',
                        type: 'select',
                        search: false,
                        sortable: true,
                        width: 120,
                        filter: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入化验人',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '主管工艺',
                        prop: 'processUserRealName',
                        type: 'select',
                        search: false,
                        sortable: true,
                        width: 120,
                        filter: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入主管工艺',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '班组',
                        prop: 'tsName',
                        type: 'select',
                        search: false,
                        sortable: true,
                        width: 120,
                        filter: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入班组',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '更新时间',
                        prop: 'updateTime',
                        search: false,
                        sortable: true,
                        width: 160,
                        addDisplay: false,
                        viewDisplay: false,
                        editDisplay: false,
                        filter: true,
                        span: 8,
                        rules: [
                            {
                                required: true,
                                message: '请输入更新时间',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '分析周期',
                        prop: 'assayCycle1',
                        search: false,
                        sortable: true,
                        hide: true,
                        width: 120,
                        filter: true,
                        span: 16,
                        rules: [
                            {
                                required: true,
                                message: '请输入分析周期',
                                trigger: 'blur',
                            },
                        ],
                    },
                ]
            },
            search:{},
            form: {

            },
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0,
            },
            excelOption: {
                submitBtn: false,
                emptyBtn: false,
                column: [
                    {
                        label: '模板上传',
                        prop: 'excelFile',
                        type: 'upload',
                        drag: true,
                        loadText: '模板上传中，请稍等',
                        span: 24,
                        propsHttp: {
                            res: 'data',
                        },
                        tip: '请上传 .xls,.xlsx 标准格式文件',
                        action: '/blade-system/user/import-user',
                    },
                    {
                        label: '模板下载',
                        prop: 'excelTemplate',
                        formslot: true,
                        span: 24,
                    },
                ],
            },

            isOpen: false,
            isInfoOpen:false,
            selectionList:[],

            managerList:[],
            teamList:[],
            personList:[],

            // data1:[
            //     {addPoint:1,testValue:15,volume:10,name:"CUCN",actualValue:20,needValue:30},
            //     {addPoint:3,testValue:2,volume:5,name:"氰化钠",actualValue:10,needValue:''},
            // ]
        }
    },
    mounted() {
        this.getManagerList()
        this.getTeamList()
        this.getPersonList()
    },
    methods: {
        // 主管工艺下拉
        getManagerList(){
            getManager().then(res =>{
                this.managerList = res.data.data
            })
        },
        // 化验人下拉
        getPersonList(){
            getPerson().then(res =>{
                this.personList = res.data.data
            })
        },
        // 班组下拉
        getTeamList(){
            getTeam().then(res =>{
                this.teamList = res.data.data
            })
        },
        addInfo(){
            this.infoType = 'add'
            this.rowId = null
            this.isInfoOpen = true
        },
        closeDialog(val) {
            this.isOpen = false
            this.isInfoOpen = false
            this.isShowImport = false
            if(val){
                this.onLoad()
            }
        },
        getName(val) {
            return val == 1 ? '周一' : val == 2 ? '周二' : val == 3 ? '周三' : val == 4 ? '周四' : val == 5 ? '周五' : val == 6 ? '周六' : '周日'
        },
        handleImport() {
            this.isShowImport = true;
        },
        // 批量设置
        submitSeeting(){
            setBatch({ids:this.selectionList.map(item => item.id).join(','), ...this.settingForm}).then(res =>{
                if(res.data.code == 200){
                    this.$message.success('设置成功')
                    this.setBox = false
                    this.onLoad()
                }
            })
        },
        // 多选
        selectionChange(list) {
            this.selectionList = list;
        },
        selectionClear() {
            this.selectionList = [];
            this.$refs.crud.toggleSelection();
        },
        searchReset() {
            this.query = {};
            this.onLoad(this.page);
        },
        searchChange(params, done) {
            this.query = params;
            this.page.currentPage = 1;
            this.onLoad(this.page, params);
            done();
        },
        handleDetail(row){
            this.rowId = row.id
            this.infoType = 'view'
            this.isInfoOpen = true
        },
        handleEdit(row){
            this.rowId = row.id
            this.infoType = 'edit'
            this.isInfoOpen = true
        },
        rowDel(row) {
            this.$confirm('确定将选择数据删除?', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                deleteInfo({ids:row.id}).then(res =>{
                    if(res.data.code == 200){
                        this.$message.success('删除成功')
                        this.onLoad()
                    }
                })
            })
        },
        currentChange(currentPage) {
            this.page.currentPage = currentPage;
        },
        sizeChange(pageSize) {
            this.page.pageSize = pageSize;
        },
        refreshChange() {
            this.onLoad(this.page, this.query);
        },
        // 批量删除
        handleDelete() {
            if (this.selectionList.length === 0) {
                this.$message.warning('请选择至少一条数据');
                return;
            }
            this.$confirm('确定将选择数据删除?', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                deleteInfo({ids:this.selectionList.map(item => item.id).join(',')}).then(res =>{
                    if(res.data.code == 200){
                        this.$message.success('删除成功')
                        this.onLoad()
                    }
                })
            })
        },
        // 批量设置
        handleSetting() {
            if (this.selectionList.length === 0) {
                this.$message.warning('请选择至少一条数据');
                return;
            }
            this.setBox = true
        },
        // 下发
        handleIssue(row) {
            this.rowId = row.id
            this.isOpen = true
        },
        onLoad() {
            this.loading = true
            getList({
                current:this.page.currentPage,
                size:this.page.pageSize,
                workCenterId:this.search.wcName ? this.search.wcName : '',
                workTankId:this.search.workTankName ? this.search.workTankName : '',
                testElement:this.search.testElement ? this.search.testElement : ''
            }).then(res =>{
                this.data = res.data.data.records
                this.loading = false
                this.page.total = res.data.data.total
            })
            // this.data = [
            //     {
            //         id: '1',
            //         wcName: '化学镀镍作业中心',
            //         slotNo: '11#氰化镀铜槽',
            //         testEle: '碳酸钠',
            //         proContent: '0',
            //         targetUpperLimit: '10',
            //         targetLowerLimit: '5',
            //         targetValue: '3',
            //         drugName: '111',
            //         addPoints: '0',
            //         assayCycle: 'N次/周',
            //         reallyDate: '2025-10-09',
            //         lastTest: '2023-06-13',
            //         dueDate: '2023-06-20',
            //         userName: '崔殿龙',
            //         chargeCraft: '崔殿龙',
            //         updateTime: '2024-01-12'
            //     },
            //     {
            //         id: '2',
            //         wcName: '化学镀镍作业中心',
            //         slotNo: '22#氰化镀铜槽',
            //         testEle: '碳酸钠',
            //         proContent: '0',
            //         targetUpperLimit: '10',
            //         targetLowerLimit: '5',
            //         targetValue: '3',
            //         drugName: '111',
            //         addPoints: '0',
            //         assayCycle: 'N次/月',
            //         reallyDate: '2025-10-09',
            //         lastTest: '2023-06-13',
            //         dueDate: '2023-06-20',
            //         userName: '崔殿龙',
            //         chargeCraft: '崔殿龙',
            //         updateTime: '2024-01-12'
            //     },
            //     {
            //         id: '2',
            //         wcName: '化学镀镍作业中心',
            //         slotNo: '22#氰化镀铜槽',
            //         testEle: '碳酸钠',
            //         proContent: '0',
            //         targetUpperLimit: '10',
            //         targetLowerLimit: '5',
            //         targetValue: '3',
            //         drugName: '111',
            //         addPoints: '0',
            //         assayCycle: 'N次/年',
            //         reallyDate: '2025-10-09',
            //         lastTest: '2023-06-13',
            //         dueDate: '2023-06-20',
            //         userName: '崔殿龙',
            //         chargeCraft: '崔殿龙',
            //         updateTime: '2024-01-12'
            //     }
            // ]
            // this.page.total = this.data.length
            // this.loading = false
            // setTimeout(() => {
            //     this.selectionClear()
            // }, 500)
        }
    }
}
</script>

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