<template>
    <el-container>
        <el-header style="height: 50%;padding: 0">
            <el-container>
                <el-header>
                    <div class="left-panel">
                        <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'mlog.create'">新增</el-button>
                    </div>
                </el-header>
                <el-main class="nopadding">
                    <scTable ref="table" :apiObj="apiObj" row-key="id" stripe :params="params">
                        <el-table-column type="index" width="50" />
                        <el-table-column label="物料名" prop="material_out_name">
                            <template #default="scope">
                                <span v-if="scope.row.material_out_">
                                    {{ scope.row.material_out_.full_name }}
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column label="设备名" prop="equipment_name">
                        </el-table-column>
                        <el-table-column label="批次号" prop="batch">
                        </el-table-column>
                        <el-table-column label="操作日期" prop="handle_date">
                        </el-table-column>
                        <el-table-column label="退火数" prop="count_real">
                        </el-table-column>
                        <el-table-column label="合格数" prop="count_ok">
                        </el-table-column>
                        <el-table-column label="不合格数" prop="count_notok">
                        </el-table-column>
                        <el-table-column label="操作" fixed="right" align="center" width="180">
                            <template #default="scope">
                                <el-button link type="primary" :disabled="scope.row.submit_user != null"
                                    @click="table_edit(scope.row)" v-auth="'mlog.update'">
                                    编辑
                                </el-button>
                                <el-button link type="success" v-if="scope.row.submit_user == null"
                                    @click="table_submit(scope.row)" v-auth="'mlog.submit'">
                                    提交
                                </el-button>
                                <el-button link type="warning" v-else @click="table_revert(scope.row)"
                                    v-auth="'mlog.submit'">
                                    撤回
                                </el-button>
                                <el-button link type="danger" @click="table_del(scope.row)" v-auth="'mlog.delete'">
                                    删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </scTable>
                </el-main>
            </el-container>

        </el-header>
        <el-main class="nopadding">
            <el-container>
                <el-aside style="width:50%">
                    <el-container>
                        <el-header>
                            <div class="left-panel">
                                <span style="font-size: 14px">交接记录</span>
                            </div>
                            <div class="right-panel">
                                <el-button type="primary" @click="addHandover" v-auth="'handover.create'">新增</el-button>
                            </div>
                        </el-header>
                        <el-main>
                            <scTable ref="table2" :apiObj="apiObjHandover" row-key="id" stripe :params="paramsHandover">
                                <el-table-column type="index" width="50" />
                                <el-table-column label="送料日期" prop="send_date">
                                </el-table-column>
                                <el-table-column label="产物" prop="material" show-overflow-tooltip>
                                    <template #default="scope">
                                        <span v-if="scope.row.material_out_">
                                            {{ scope.row.material_out_.full_name }}
                                        </span>
                                    </template>
                                </el-table-column>
                                <el-table-column label="批次" prop="batch"> </el-table-column>
                                <el-table-column label="数量" prop="count">
                                </el-table-column>
                                <el-table-column label="交送人" prop="send_user_name">
                                </el-table-column>
                                <el-table-column label="接收人" prop="recive_user_name">
                                </el-table-column>
                                <el-table-column label="是否确认" prop="submit_time">
                                    <template #default="scope">
                                        <span v-if="scope.row.submit_time">是</span>
                                        <span v-else>否</span>
                                    </template>
                                </el-table-column>
                                <el-table-column label="操作" fixed="right" align="left" width="100">
                                    <template #default="scope">
                                        <el-button link type="primary" @click="handover_edit(scope.row)"
                                            v-auth="'handover.update'" v-if="scope.row.submit_time == null">
                                            编辑
                                        </el-button>
                                        <el-button link type="danger" @click="handover_delete(scope.row)"
                                            v-auth="'handover.delete'" v-if="scope.row.submit_time == null">
                                            删除
                                        </el-button>
                                    </template>
                                </el-table-column>
                            </scTable>
                        </el-main>
                    </el-container>
                </el-aside>
                <el-main class="nopadding">
                    <el-container>
                        <el-header>
                            <div class="left-panel">
                                <span style="font-size: 14px">车间库存</span>
                            </div>
                            <div class="right-panel">
                                <el-button type="primary" @click="tomio" v-auth="'mio.do'">领料</el-button>
                                <el-button type="primary" @click="tomio" v-auth="'mio.do'">入库</el-button>
                            </div>
                        </el-header>
                        <el-main>
                            <scTable ref="table_wm" :apiObj="apiObjWm" row-key="id" :params="paramsWm" stripe
                                hidePagination>
                                <el-table-column label="物料名" prop="material">
                                    <template #default="scope">{{
                                        scope.row.material_.name
                                    }}</template>
                                </el-table-column>
                                <el-table-column label="规格" prop="material">
                                    <template #default="scope">{{
                                        scope.row.material_.specification
                                    }}</template>
                                </el-table-column>
                                <el-table-column label="型号" prop="material">
                                    <template #default="scope">{{
                                        scope.row.material_.model
                                    }}</template>
                                </el-table-column>
                                <el-table-column label="已到工序" prop="material">
                                    <template #default="scope">{{
                                        scope.row.material_.process_name
                                    }}</template>
                                </el-table-column>
                                <el-table-column label="批次" prop="batch"> </el-table-column>
                                <el-table-column label="数量" prop="count" width="80">
                                </el-table-column>
                            </scTable>
                        </el-main>
                    </el-container>
                </el-main>
            </el-container>
        </el-main>
    </el-container>
    <el-dialog :title="titleOptions[handoverType]" ref="handoverDialog" v-model="handoverVisible" :size="1000"
        destroy-on-close @closed="handoverVisible = false">
        <el-form ref="dialogForm" :model="handoverForm" :rules="rules" label-width="120px">
            <el-row>
                <el-col :md="12" :sm="24">
                    <el-form-item label="送料日期" prop="send_date">
                        <el-date-picker v-model="handoverForm.send_date" type="date" value-format="YYYY-MM-DD"
                            style="width: 100%" :disabled="handoverType == 'edit'" />
                    </el-form-item>
                </el-col>
                <el-col :md="12" :sm="24">
                    <el-form-item label="交送物料" prop="wm">
                        <el-select v-model="handoverForm.wm" placeholder="交送物料" clearable style="width: 100%"
                            :disabled="handoverType == 'edit'">
                            <el-option v-for="item in wmaterialList" :key="item.id" :label="item.material_.name"
                                :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :md="12" :sm="24">
                    <el-form-item label="交送数量" prop="count">
                        <el-input-number v-model="handoverForm.count" 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="send_user">
                        <el-select v-model="handoverForm.send_user" placeholder="交送人" clearable style="width: 100%"
                            :disabled="handoverType == 'edit'">
                            <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 label="接收部门" prop="recive_dept">
                        <el-select v-model="handoverForm.recive_dept" placeholder="接收部门" clearable style="width: 100%"
                            @change="getUserList2" :disabled="handoverType == 'edit'">
                            <el-option v-for="item in deptOptions" :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="recive_user">
                        <el-select v-model="handoverForm.recive_user" placeholder="接收人" clearable style="width: 100%"
                            :disabled="handoverType == 'edit'">
                            <el-option v-for="item in userList2" :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="handoverVisible = false">取消</el-button>
            <el-button type="primary" @click="submitHandover">提交</el-button>
        </el-footer>
    </el-dialog>
    <save-dialog v-if="dialogSave" ref="saveDialog" :belongdept="currentMgroup.belong_dept" :mgroup="currentMgroup.id"
        :mgroupShiftRule="currentMgroup.shift_rule" :activeType="activeName" @success="handleSaveSuccess"
        @closed="dialogSave = false">
    </save-dialog>
</template>
<script>
import saveDialog from "./worktask_form.vue";
export default {
    components: {
        saveDialog,
    },
    data() {
        return {
            activeName: '退火',
            handoverType: 'add',
            titleOptions: {
                'add': '新增',
                'edit': '编辑',
            },
            currentMgroup: {},
            apiObj: null,
            params: {},
            apiObjHandover: null,
            paramsHandover: {},
            apiObjWm: null,
            paramsWm: {},
            handoverForm: {},
            dialogSave: false,
            handoverVisible: false,
            deptOptions: [],
            userList: [],
            userList2: []
        }
    },
    mounted() {
        this.initMgroup()
        this.getDepts();
        this.getWmaterial();
    },
    methods: {
        add() {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("add");
            });
        },
        //编辑
        table_edit(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("edit").setData(row);
            });
        },
        tomio() {
            this.$router.push({ name: "halfgood_mio" });
        },
        table_submit(row) {
            this.$API.wpm.mlog.submit.req(row.id).then(res => {
                this.$message.success("提交成功");
                this.$refs.table.refresh();
                this.$refs.table_wm.refresh();
            })
        },
        table_revert(row) {
            this.$API.wpm.mlog.revert.req(row.id).then(res => {
                this.$message.success("撤回成功");
                this.$refs.table.refresh();
                this.$refs.table_wm.refresh();
            }).catch(e => { })
        },
        async table_del(row) {
            this.$confirm(`确定删除吗？`, "提示", {
                type: "warning",
            }).then(() => {
                this.$API.wpm.mlog.delete.req(row.id).then((res) => {
                    this.$message.success("删除成功");
                    this.$refs.table.refresh();
                    return res;
                })
                    .catch((err) => {
                        return err;
                    });
            })
                .catch(() => { });
        },
        handleSaveSuccess() {
            this.$refs.table.refresh();
        },
        getDepts() {
            this.$API.system.dept.list.req({ page: 0, name__in: '6车间,7车间,10车间' }).then(res => {
                this.deptOptions = res
            })
        },
        getUserList2() {
            this.$API.system.user.list.req({ belong_dept: this.handoverForm.recive_dept, page: 0 }).then(res => {
                this.userList2 = res
            })
        },
        initMgroup() {
            this.$API.mtm.mgroup.list.req({ page: 0, name: '管料退火' }).then(res => {
                if (res.length == 1) {
                    this.currentMgroup = res[0]
                    this.params.mgroup = this.currentMgroup.id
                    this.apiObj = this.$API.wpm.mlog.list

                    this.paramsHandover.send_mgroup = this.currentMgroup.id
                    this.apiObjHandover = this.$API.wpm.handover.list

                    this.paramsWm.belong_dept = this.currentMgroup.belong_dept
                    this.paramsWm.count__gte = 1
                    this.apiObjWm = this.$API.wpm.wmaterial.list

                    this.$API.system.user.list.req({ page: 0, belong_dept: this.currentMgroup.belong_dept }).then(res => {
                        this.userList = res
                    })
                } else {
                    this.$message.error("未找到工段");
                }
            })
        },
        //从生产日志生成交接记录
        addHandover(row) {
            console.log('currentMgroup', this.currentMgroup.id)
            this.handoverForm.wm = '';
            this.handoverForm.send_date = "";
            this.handoverForm.send_user = "";
            this.handoverForm.count = 1;

            this.handoverForm.recive_user = '';//接收人
            this.handoverForm.recive_dept = '';//接收车间
            this.handoverForm.send_mgroup = this.currentMgroup.id;
            this.handoverType = 'add';
            this.handoverVisible = true;
        },
        getWmaterial() {
            let obj = {};
            obj.belong_dept__name = "8车间";
            obj.page = 0;
            obj.count__gte = 1;
            this.$API.wpm.wmaterial.list.req(obj).then(res => {
                debugger;
                console.log(res);
                this.wmaterialList = res;
            })
        },
        submitHandover() {
            this.$API.wpm.handover.genByWm.req(this.handoverForm).then((res) => {
                this.$message.success("创建成功");
                this.handoverVisible = false;
                this.$refs.table2.refresh();
            });
        },
        handover_edit(row) {
            Object.assign(this.handoverForm, row);
            this.getUserList2(row.recive_dept);
            this.handoverType = 'edit';
            this.handoverVisible = true;
        },
        handover_delete(row) {
            this.$confirm(`确定删除吗？`, "提示", {
                type: "warning",
            })
                .then(() => {
                    this.$API.wpm.handover.delete
                        .req(row.id)
                        .then((res) => {
                            this.$message.success("删除成功");
                            this.$refs.table2.refresh();
                            return res;
                        })
                        .catch((err) => {
                            return err;
                        });
                })
                .catch(() => { });
        },
    }
}
</script>