<template>
    <el-dialog
        :title="titleMap[mode]"
        v-model="visible"
        width="1000px"
        :size="1000"
        destroy-on-close
        @closed="$emit('closed')"
    >
        <!-- 混料工序 -->
        <el-form v-if="activeType=='混料工序'" ref="ruleForm" :model="form" :rules="rules" label-width="80px">
            <el-row>
                <el-col :md="12" :sm="24">
                    <el-form-item label="产品规格" prop="material">
                        <el-select
                            v-model="form.cate"
                            placeholder="产品规格"
                            clearable
                            style="width:100%"
                        >
                            <el-option
                                v-for="item in cateOptions"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                
                <el-col :md="12" :sm="24">
                    <el-form-item label="合格数量" width="100">
                        <el-input-number 
                        v-model="form.count_use" 
                        controls-position="right" 
                        :min="0" step="1" 
                        :step-strictly="true" 
                        style="width:100%"
                        placeholder="请输入合格数量">
                        </el-input-number>
                    </el-form-item>
                </el-col>
                
            </el-row>
        </el-form>
        <!-- 七车间 -->
        <el-form v-if="activeType=='7车间'" ref="ruleForm" :model="form" :rules="rules" label-width="120px">
            <el-row>
                <el-col :md="12" :sm="24">
                    <el-form-item label="炉号">
                        <el-select
                            v-model="form.cate"
                            placeholder="炉号"
                            clearable
                            style="width:100%"
                        >
                            <el-option
                                v-for="item in 8"
                                :key="item"
                                :label="item"
                                :value="item"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :md="12" :sm="24">
                    <el-form-item label="第几锅">
                        <el-input-number 
                        v-model="form.count_use" 
                        controls-position="right" 
                        :min="0" step="1" 
                        :step-strictly="true" 
                        style="width:100%"
                        placeholder="第几锅">
                        </el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :md="12" :sm="24">
                    <el-form-item label="产品规格" prop="material">
                        <el-select
                            v-model="form.cate"
                            placeholder="产品规格"
                            clearable
                            style="width:100%"
                        >
                            <el-option
                                v-for="item in cateOptions"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :md="12" :sm="24">
                    <el-form-item label="配粉料数(kg)">
                        <el-input-number 
                        v-model="form.count_use" 
                        controls-position="right" 
                        :min="0" 
                        step="1"  
                        :precision="2"
                        style="width:100%"
                        placeholder="请输入配粉料数（kg）">
                        </el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :md="12" :sm="24">
                    <el-form-item label="产出数量">
                        <el-input-number 
                        v-model="form.count_use" 
                        controls-position="right" 
                        :min="0" step="1" 
                        :step-strictly="true" 
                        style="width:100%"
                        placeholder="请输入产出数量">
                        </el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :md="12" :sm="24">
                    <el-form-item label="合格数量">
                        <el-input-number 
                        v-model="form.count_use" 
                        controls-position="right" 
                        :min="0" step="1" 
                        :step-strictly="true" 
                        style="width:100%"
                        placeholder="请输入合格数量">
                        </el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :md="12" :sm="24">
                    <el-form-item label="退火炉号">
                        <el-select
                            v-model="form.cate"
                            placeholder="退火炉号"
                            clearable
                            style="width:100%"
                        >
                            <el-option
                                v-for="item in 8"
                                :key="item"
                                :label="item"
                                :value="item"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                
                <el-col :md="12" :sm="24">
                    <el-form-item label="是否加氧">
                        <el-select
                            v-model="form.o2"
                            placeholder="是否加氧"
                            clearable
                            style="width:100%"
                        >
                            <el-option
                                v-for="item in yseorno"
                                :key="item"
                                :label="item"
                                :value="item"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :md="12" :sm="24">
                    <el-form-item prop="type" label="成型人">
                        <el-select
                            v-model="form.shift"
                            placeholder="成型人"
                            clearable
                            style="width:100%"
                        >
                            <el-option
                                v-for="item in userList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :md="12" :sm="24">
                    <el-form-item prop="type" label="切料人">
                        <el-select
                            v-model="form.shift"
                            placeholder="切料人"
                            clearable
                            style="width:100%"
                        >
                            <el-option
                                v-for="item in userList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <!-- 十车间 -->
        <el-form v-if="activeType=='10车间'" ref="ruleForm" :model="form" :rules="rules" label-width="80px">
            <el-row>
                <el-col :md="12" :sm="24">
                    <el-form-item label="产品规格" prop="material">
                        <el-select
                            v-model="form.cate"
                            placeholder="产品规格"
                            clearable
                            style="width:100%"
                        >
                            <el-option
                                v-for="item in cateOptions"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :md="12" :sm="24">
                    <el-form-item label="班次" prop="material">
                        <el-select
                            v-model="form.shift"
                            placeholder="班次"
                            clearable
                            style="width:100%"
                        >
                            <el-option
                                v-for="item in shiftOptions"
                                :key="item"
                                :label="item"
                                :value="item"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :md="12" :sm="24">
                    <el-form-item label="合格数量" width="100">
                        <el-input-number 
                        v-model="form.count_use" 
                        controls-position="right" 
                        :min="0" step="1" 
                        :step-strictly="true" 
                        style="width:100%"
                        placeholder="请输入合格数量">
                        </el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :md="12" :sm="24">
                    <el-form-item prop="type" label="成型人">
                        <el-select
                            v-model="form.cxr"
                            placeholder="成型人"
                            clearable
                            style="width:100%"
                        >
                            <el-option
                                v-for="item in userList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :md="12" :sm="24">
                    <el-form-item prop="type" label="带班人">
                        <el-select
                            v-model="form.shift"
                            placeholder="带班人"
                            clearable
                            style="width:100%"
                        >
                            <el-option
                                v-for="item in userList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <el-footer>
            <el-button @click="resetForm">取消</el-button>
            <el-button type="primary" @click="submitForm">提交</el-button>
        </el-footer>
    </el-dialog>
</template>
<script>
import { index } from 'd3';

    export default {
        emits: ["success", "closed"],
        props:{
            mtask: { type: String, default: '' },
            activeType:{ type: String, default: '' },
        },
        data() {
            return {
                yseorno:['是','否'],
                loading: false,
                mode: "add",
                titleMap: {
                    add: "新增记录",
                    edit: "编辑记录",
                    show: "查看记录",
                },
                form:{},
                options:[],
                userList:[],
                cateOptions:[],
                shiftOptions:['白班','夜班'],
                visible: false,
                isSaveing: false,
                setFiltersVisible: false,
                handle_name:'',
                leader_name:''
            };
        },
        mounted() {
            this.getUserList();
        },
        methods: {
            getUserList(){
                let that = this;
                this.$API.system.user.list.req({belong_dept__name:this.activeType,page:0}).then(res=>{
					that.userList = res;
				});
            },
            getReceptionist(data) {
                this.form.handle_user=data.id;
                this.handle_name=data.name
            },
            getLeader(data){
                this.form.leader_user=data.id;
                this.leader_name=data.name
            },
            //显示
            open(mode = "add") {
                this.mode = mode;
                this.visible = true;
                
                return this;
            },
            getList(){
                this.$API.mtm.material.list.req({page:0}).then(res=>{
                    this.options = res;
                })
            },
            //提交
            submit() {
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        try {
                            var res;
                            this.form.mtask = this.mtask;
                            if (this.mode == "add") {
                                res = await this.$API.pum.planitem.create.req(this.form);
                            } else if (this.mode == "edit") {
                                res = await this.$API.pum.planitem.update.req(this.form.id,this.form);
                            }
                            this.isSaveing = false;
                            this.$emit("success", this.form, this.mode);
                            this.visible = false;
                            this.$message.success("操作成功");
                        } catch (err) {
                            //可以处理校验错误
                            this.isSaveing = false;
                            return err;
                        }
                    }
                });
            },
            //表单注入数据
            setData(data) {
                Object.assign(this.form, data);
            },
            //设置过滤项
            setFilters(filters) {
                this.selectionFilters = filters;
                this.setFiltersVisible = true;
            },
        },
    };
</script>
  