<template>
    <div style="padding: 8px">
        <div>
            <el-card style="width: 100%" header="基本信息" shadow="hover">
                <el-descriptions>
                    <el-descriptions-item label="编号">{{ mioObj.number }}</el-descriptions-item>
                    <el-descriptions-item label="出入库类型">{{ typeDict[mioObj.type] }}</el-descriptions-item>
                    <el-descriptions-item label="状态">{{ stateDict[mioObj.state] }}</el-descriptions-item>
                    <el-descriptions-item label="部门/车间">{{ mioObj.belong_dept_name }}</el-descriptions-item>
                    <el-descriptions-item label="执行人">{{ mioObj.do_user_name }}</el-descriptions-item>
                    <el-descriptions-item label="创建时间">{{ mioObj.create_time }}</el-descriptions-item>
                    <el-descriptions-item label="采购订单" v-if="mioObj.type=='pur_in'">{{ mioObj.order_number }}</el-descriptions-item>
                    <el-descriptions-item label="供应商" v-if="mioObj.type=='pur_in'">{{ mioObj.supplier_name }}</el-descriptions-item>
                    <el-descriptions-item label="销售订单" v-if="mioObj.type=='sale_out'">{{ mioObj.pu_order_number }}</el-descriptions-item>
                    <el-descriptions-item label="客户" v-if="mioObj.type=='sale_out'">{{mioObj.customer_name }}</el-descriptions-item>
                </el-descriptions>
                <el-button type="primary" @click="mioSubmit" v-auth="'mio.submit'" v-if="mioObj.state == 10">
                    提交
                </el-button>
            </el-card>
        </div>
        <div style="height:8px"></div>
        <div>
            <el-card style="width: 100%" header="物料明细" shadow="hover">
                <div>
                    <el-button type="primary" icon="el-icon-plus" @click="table_add" v-if="mioObj.state == 10"></el-button>
                </div>
                <scTable ref="table" :apiObj="apiObj" row-key="id" stripe :params="params" hidePagination hideDo>
                    <el-table-column type="index" width="50" />
                    <el-table-column label="物料" prop="material" show-overflow-tooltip>
                        <template #default="scope">
                            <span v-if="scope.row.material_"> {{ scope.row.material_.name }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="批次号" prop="batch">
                    </el-table-column>
                    <el-table-column label="仓库" prop="warehouse_name">
                    </el-table-column>
                    <el-table-column label="数量" prop="count">
                    </el-table-column>
                    <el-table-column label="创建时间" prop="create_time" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="100px">
                        <template #default="scope">
                            <el-link type="primary" @click="table_check(scope.row)" v-if="scope.row.test_user==null">
                                检验
                            </el-link>
                            <el-link type="primary" @click="table_Show(scope.row)" v-else>
                                产看
                            </el-link>
                            <el-link type="danger" @click="table_del(scope.row)" v-if="mioObj.state == 10">
                                删除
                            </el-link>
                        </template>
                    </el-table-column>
                </scTable>
            </el-card>
        </div>
    </div>
    <save-dialog v-if="dialog.save" ref="saveDialog" :mioId="mioId" :belongDeptId="belongDeptId" :belongDeptName="mioObj.belong_dept_name" :mioObj="mioObj"
        @success="handleSaveSuccess" @closed="dialog.save = false">
    </save-dialog>

    <check-dialog v-if="dialog.check" ref="checkDialog" :mioitemId="mioitemId"
        @success="handleCheckSuccess" @closed="dialog.check = false">
    </check-dialog>
</template>
<script>
import saveDialog from "./mioitem_form.vue";
import checkDialog from "./mioitem_check.vue";
export default {
    name: "mioitem",
    components: {
        saveDialog,checkDialog
    },
    data() {
        return {
            dialog: {
                check:false,
                save: false,
            },
            apiObj: null,
            params: {},
            mioId: '',
            mioObj: {},
            selection: [],
            stateDict: {
                10: '创建中',
                20: '已提交'
            },
            typeDict: {
                'do_out': '生产领料',
                'sale_out': '销售发货',
                'pur_in': '采购入库',
                'do_in': '生产入库',
                'other_in': '其他入库',
                'other_out': '其他出库',
            },
            mioitemId:''
        };
    },
    mounted() {
        this.mioId = this.$route.query.mio;
        this.params.mio = this.$route.query.mio;
        this.apiObj = this.$API.inm.mioitem.list;
        this.getMio();
    },
    methods: {
        getMio() {
            this.$API.inm.mio.item.req(this.mioId).then((res) => {
                this.mioObj = res;
                this.belongDeptId = res.belong_dept
            })
        },
        //添加
        table_add() {
            this.dialog.save = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("add", this.mioObj.type);
            });
        },
        //编辑
        table_edit(row) {
            this.dialog.save = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("edit").setData(row);
            });
        },
        //查看
        table_show(row) {
            this.dialog.save = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("show").setData(row);
            });
        },
        //删除
        table_del(row) {
            this.$confirm(`确定删除吗？`, "提示", {
                type: "warning",
            }).then(() => {
                this.$API.inm.mioitem.delete.req(row.id).then((res) => {
                    this.$message.success("删除成功");
                    this.$refs.table.refresh();
                    return res;
                }).catch((err) => {
                    return err;
                });
            }).catch(() => { });
        },
        table_check(row){
            this.dialog.check = true;
            this.mioitemId = row.id;
            this.$nextTick(() => {
                this.$refs.checkDialog.open("add");
            });
        },
        table_Show(row){
            this.dialog.check = true;
            this.mioitemId = row.id;
            this.$nextTick(() => {
                this.$refs.checkDialog.open("add");
            });
        },
        //本地更新数据
        handleSaveSuccess(data, mode) {
            if (mode == "add") {
                this.$refs.table.refresh();
            } else if (mode == "edit") {
                this.$refs.table.refresh();
            }
        },
        handleCheckSuccess(){
            this.$refs.table.refresh();
        },
        handleQuery() {
            this.$refs.table.queryData(this.query)
        },
        resetQuery() {
            this.query = {};
        },
        mioSubmit() {
            this.$API.inm.mio.submit.req(this.mioObj.id).then(res => {
                this.$message.success("提交成功");
                this.mioObj = res
            })
        }
    },
};
</script>