<template>
	<lj-win-common :title="title" :show-back="true">
        <template slot="header_right">
            <mu-menu placement="bottom-end" :open.sync="openTitle">
                <mu-button icon>
                    <i class="iconfont icon-more"></i>
                </mu-button>
                <mu-list slot="content">
                    <mu-list-item button @click="handleCreateCpCheck">
                        <mu-list-item-title>自动生成质检单</mu-list-item-title>
                    </mu-list-item>
                </mu-list>
            </mu-menu>
        </template>
		<div slot="body">
			<div class="lj-card-item">
				<mu-row>
					<mu-col span="3" class="lj-card-secondary-title">质检单号：</mu-col>
					<mu-col span="7" class="lj-card-secondary-content">{{mainObject.billcode}}</mu-col>
					<mu-col span="2">
					</mu-col>
					<mu-col span="3" class="lj-card-secondary-title">相关号：</mu-col>
					<mu-col span="9" class="lj-card-secondary-content">{{mainObject.relcode}}</mu-col>
					<mu-col span="3" class="lj-card-secondary-title">供应商号：</mu-col>
					<mu-col span="9" class="lj-card-secondary-content">{{mainObject.sptcode}}</mu-col>
					<mu-col span="3" class="lj-card-secondary-title">供应商名：</mu-col>
					<mu-col span="9" class="lj-card-secondary-content">{{mainObject.sptname}}</mu-col>
					<mu-col span="3" class="lj-card-secondary-title">来料日期：</mu-col>
					<mu-col span="3" class="lj-card-secondary-content">{{mainObject.indate|dateFilter}}</mu-col>
					<mu-col span="3" class="lj-card-secondary-title">质检日期：</mu-col>
					<mu-col span="3" class="lj-card-secondary-content">{{mainObject.billdate|dateFilter}}</mu-col>
					<mu-col span="3" class="lj-card-secondary-title">质检员：</mu-col>
					<mu-col span="9" class="lj-card-secondary-content">{{mainObject.billemp}}</mu-col>
				</mu-row>
			</div>
			<template>
				<lj-grid :table-data="mxList" :columns="gridColumns"></lj-grid>
			</template>
		</div>
		<template slot="footer">
                <mu-row>
                    <template v-if="option420 == 1">
                        <mu-col tag="mu-button" color="primary" v-if="mainObject.flag == 0" @click="handleAudit(1)">初审</mu-col>
                        <mu-col tag="mu-button" color="primary" v-if="mainObject.flag == 4" @click="handleAudit(2)">终审 </mu-col>
                    </template>
                    <template v-else>
                        <mu-col tag="mu-button" color="primary" v-if="mainObject.flag == 0" @click="handleAudit(1)">审核</mu-col>
                    </template>

                    <template v-if="option420 == 1">
                        <mu-col tag="mu-button" color="error" v-if="mainObject.flag == 4" @click="handleCAudit(1)">初撤</mu-col>
                        <mu-col tag="mu-button" color="error" v-if="mainObject.flag == 1" @click="handleCAudit(2)">终撤 </mu-col>
                    </template>
                    <template v-else>
                        <mu-col tag="mu-button" color="error" v-if="mainObject.flag == 1" @click="handleCAudit(1)">撤审</mu-col>
                    </template>
                </mu-row>
            </template>
	</lj-win-common>
</template>

<script>
	import LjWinCommon from 'components/lj-win-common';
	import LjGrid from 'components/lj-grid';

	export default {
		components: {
			LjWinCommon,
			LjGrid,
		},
		data() {
			return {
				title: "自制送检单-详情",
				mainObject: {},
				mxList: [],
				gridColumns: [
					{label: "指令单号", property: "reltaskcode", width: screen.width * 0.3},
					{label: "物料编码", property: "mtrlcode", width: screen.width * 0.25},
					{label: "物料名称", property: "mtrlname", width: screen.width * 0.25},
					{label: "检验类型", property: "checktype", width: screen.width * 0.2,
						selectOptions: {
                            options: [
                                {checktype: 0, typestr: "全检"},
                                {checktype: 1, typestr: "抽检"},
                            ],
                            valueMap: ["checktype", "typestr"]
                        }
                    },
					{label: "来单质检数", property: "checkqty", width: screen.width * 0.2},
					{label: "合格数", property: "goodqty", width: screen.width * 0.2},
					{label: "不合格数", property: "badqty", width: screen.width * 0.2},
					{label: "入库数", property: "goodqty_ware", width: screen.width * 0.2},
				],
				option420: Number($lj.getOptionValue(420)),
                openTitle: false
			}
		},
		created() {
            this.mainObject = $lj.getPageParam();
            this.getSptcheckMx();
		},
		methods: {
			getSptcheckMx() {
				let self = this;
				$lj.postLJRequest('GetL1SptcheckMx', {
					token: $lj.getStorage("token"),
					billid: this.mainObject.billid
				}, function (ret) {
					if (ret) {
						self.mxList = ret.sptcheckmxList;
					}
				})
			},
			handleAudit(type) {
                let self = this;
                let functionName = type === 1 ? "first_audit" : "audit";
                let requestBody = {
                    token: $lj.getStorage("token"),
                    CList: [{
                        uoName: "uo_sptcheck",
                        functionName: functionName,
                        SParms: {
                            arg_billid: this.mainObject.billid,
                            arg_opemp: $lj.getStorage('user').username,
                            arg_msg: "",
                            arg_ifcommit: true
                        },
                        ifcommit: true
                    }]
                };
                $lj.postLJRequest('CommonBill', requestBody, function (ret) {
                    if (ret) {
                        if(self.option420 === 1) {
                            if(type === 1) {
                                $lj.toast("初审成功!");
                                self.mainObject.flag = 4;
                            } else {
                                $lj.toast("终审成功!");
								self.mainObject.flag = 1;
                            }
                        } else {
                            $lj.toast("审核成功!");
                            self.mainObject.flag = 1;
                        }
                    }
                })
            },
            handleCAudit(type) {
                let self = this;
                let functionName = type === 1 ? "first_caudit" : "caudit";
                let requestBody = {
                    token: $lj.getStorage("token"),
                    CList: [{
                        uoName: "uo_sptcheck",
                        functionName: functionName,
                        SParms: {
                            arg_billid: this.mainObject.billid,
                            arg_msg: "",
                            arg_ifcommit: true
                        },
                        ifcommit: true
                    }]
                };
                $lj.postLJRequest('CommonBill', requestBody, function (ret) {
                    if (ret) {
                        if(self.option420 === 1) {
                            if(type === 1) {
                                $lj.toast("初撤成功!");
                                self.mainObject.flag = 0;
                            } else {
                                $lj.toast("终撤成功!");
                                self.mainObject.flag = 4;
                            }
                        } else {
                            $lj.toast("撤审成功!");
                            self.mainObject.flag = 0;
                        }
                    }
                })
            },
            handleCreateCpCheck() {
                let self = this;
                $lj.confirm("确认", "是否自动建立自制质检单", function () {
                    let functionName = "uof_create_cpcheck_order";
                    let requestBody = {
                        token: $lj.getStorage("token"),
                        CList: [{
                            uoName: "uo_sptcheck",
                            functionName: functionName,
                            SParms: {
                                arg_billid: self.mainObject.billid,
                                arg_msg: "",
                                arg_ifcommit: true
                            },
                            ifcommit: true
                        }]
                    };
                    $lj.postLJRequest('CommonBill', requestBody, function (ret) {
                        if (ret) {
                            $lj.toast("成功建立质检单!");
                        }
                    })
                })
                
            }
		}
	}
</script>