<template>
    <el-dialog
        :title="titleMap[mode]"
        v-model="visible"
        width="1000px"
        :size="1000"
        destroy-on-close
        @closed="$emit('closed')"
    >
        <el-form ref="saveDialog" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="产品编号" prop="material">
                <el-select
                    v-model="form.batch"
                    placeholder="产品编号"
                    clearable
                    style="width:100%"
                >
                    <el-option
                        v-for="item in options"
                        :key="item"
                        :label="item"
                        :value="item"
                    ></el-option>
                </el-select>
            </el-form-item>
            <sc-form-table ref="table" v-model="formList" :addTemplate="addTemplate" drag-sort placeholder="暂无数据">
                <el-table-column prop="type" label="工序" min-width="100">
                    <template #default="scope">
                        <span v-if="!scope.row.change">{{ scope.row.mgroup_name }}</span>
                        <el-select v-else v-model="scope.row.batch" placeholder="请选择">
                            <el-option v-for="item in options" :key="item" :label="item" :value="item"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column prop="val" label="加工数量" width="100">
                    <template #default="scope">
                        <el-input v-model="scope.row.count_use" placeholder="请输入内容"></el-input>
                    </template>
                </el-table-column>
                <el-table-column prop="val" label="合格数量" width="100">
                    <template #default="scope">
                        <el-input v-model="scope.row.count_ok" placeholder="请输入内容"></el-input>
                    </template>
                </el-table-column>
                <el-table-column label="不合格数量" align="center">
                    <el-table-column prop="val" label="原因1" width="100">
                        <template #default="scope">
                            <el-input v-model="scope.row.count_notok" placeholder="不合格数量1"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="val" label="原因2" width="100">
                        <template #default="scope">
                            <el-input v-model="scope.row.count_no2" placeholder="不合格数量2"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="val" label="原因3" width="100">
                        <template #default="scope">
                            <el-input v-model="scope.row.count_no3" placeholder="不合格数量3"></el-input>
                        </template>
                    </el-table-column>
                </el-table-column>
                <el-table-column prop="type" label="操作人" min-width="120" align="center">
                    <template #default="scope">
                        <el-select v-model="scope.row.handle_user" filterable style="width: 100%" :disabled="bindType == 20">
                            <el-option
                                v-for="item in userList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            />
                        </el-select>
                    </template>
                </el-table-column>
                <!-- <el-table-column prop="type" label="操作" min-width="90" align="center">
                    <template #default="scope">
                        <el-link type="primary" @click="submitRecord(scope.row)" >确定 </el-link>
                    </template>
                </el-table-column> -->
            </sc-form-table>
        </el-form>
        <el-footer>
            <el-button type="primary" @click="submitForm">保存</el-button>
            <el-button @click="resetForm">取消</el-button>
        </el-footer>
    </el-dialog>
</template>
<script>
    export default {
        emits: ["success", "closed"],
        props:{
            mtask: { type: String, default: '' },
            date: { type: String, default: '' },
        },
        data() {
            return {
                loading: false,
                mode: "add",
                titleMap: {
                    add: "新增记录",
                    edit: "编辑记录",
                    show: "查看记录",
                },
                obj:{},
                form:{batch:''},
                addTemplate: {
					batch: '',
                    mgroup_name: '',
                    count_use:1,
                    count_ok: 1,
                    count_notok: 1,
                    count_notok1: 1,
                    count_notok2: 1,
                    count_notok3: 1,
                    handle_user:'',
                    handle_date:'',
                    mtask:'',
                    change:true
				},
                handleDate:'',
                related:[],
                userList:[],
                formList: [],
                recordId:'',
                visible: false,
                isSaveing: false,
                setFiltersVisible: false,
                options: [],
                materialOptions:[],
                rules: {
                    batch: [{required: true, message: "请选选择批次", trigger: "blur"}]
                },
            };
        },
        mounted() {
            console.log('date',this.date)
            this.getBatchs();
            this.getFormList();
            this.getUserList();
        },
        methods: {
             //显示
             open(mode = "add") {
                this.mode = mode;
                this.visible = true;
                return this;
            },
            getUserList(){
                let that = this;
                this.$API.system.user.list.req({belong_dept:'3423857962907983872',page:0}).then(res=>{
					that.userList = res;
				});
            },
            getFormList(){
                this.$API.pm.mtask.related.req(this.mtask).then(res=>{
                    this.related = res;
                    res.forEach(item=>{
                        let obj = {
                            mtask:item.id,
                            batch: '',
                            mgroup_name: item.mgroup_name,
                            count_use:1,
                            count_ok: 1,
                            count_notok: 1,
                            count_notok1: 1,
                            count_notok2: 1,
                            count_notok3: 1,
                            handle_user:'',
                            change:false
                        }
                        this.formList.push(obj)
                    })
                })
            },
            
            //获取车间物料批次号
			getBatchs(){
                let that = this;
				this.$API.wpm.wmaterial.batchs.req({belong_dept_name:'6车间'}).then(res=>{
					that.options = res;
                    console.log(res)
				});
			},
            pushRow(){
				let obj = {
					batch: '',
                    mgroup_name:"",
                    count_use:1,
                    count_ok: 1,
                    count_notok: 1,
                    count_notok1: 1,
                    count_notok2: 1,
                    count_notok3: 1,
                    handle_user:'',
                    mtask:'',
                    change:true
				}
				this.formList.push(obj);
			},
			deleteRow(){
				this.$refs.table.deleteRow(0)
			},
           
            getList(){
                this.$API.mtm.material.list.req({page:0}).then(res=>{
                    this.options = res;
                })
            },
            getReceptionist(data) {
                // 子组件调用父组件的方法并传参
                console.log(data);
                console.log(data.listIndex);
                debugger;
            },
            //保存
            submitForm() {
                this.$refs.saveDialog.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        try {
                            var res;
                            for(let i=0;i<this.formList.length;i++){
                                this.formList[i].batch = this.form.batch;
                                this.formList[i].handle_date = this.date;
                            }
                            this.form.pu_plan = this.puPlan;
                            if (this.mode == "add") {
                                res = await this.$API.wpm.mlog.create.req(this.formList);
                            } else if (this.mode == "edit") {
                                res = await this.$API.wpm.mlog.update.req('bulk',this.formList);
                            }
                            this.$emit('success')
                            this.isSaveing = false;
                            this.visible = false;
                            this.$message.success("操作成功");
                        } catch (err) {
                            //可以处理校验错误
                            this.isSaveing = false;
                            return err;
                        }
                    }
                });
            },
            //表单注入数据
            setData(data) {
                console.log('data:',data)
                this.form.batch = data.batch;
                this.recordId = data.id;
                let obj =  {};
                obj.batch = data.batch;
                obj.handle_date = data.handle_date;
                obj.mtask__mgroup__belong_dept__name = '6车间';
                this.$API.wpm.mlog.list.req(obj).then(res=>{
                    let data = res.results;
                    for(let i=0;i<this.formList.length;i++){
                        data.forEach(item=>{
                            if(item.mgroup_name==this.formList[i].mgroup_name){
                                Object.assign(this.formList[i],item)
                            }
                        })
                    }
                    console.log('formList:',this.formList)
                    console.log('data:',data)
                })
            },
            //设置过滤项
            setFilters(filters) {
                this.selectionFilters = filters;
                this.setFiltersVisible = true;
            },
        },
    };
</script>
  