<template>
    <basic-container>
        <avue-crud :option="option" :table-loading="loading" :data="data" v-model="form" v-model:page="page" ref="crud"
            @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset"
            @row-update="rowUpdate"
            :before-open="beforeOpen"
            @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
            @refresh-change="refreshChange" @on-load="onLoad">
            <template #deptName="{ row }">{{ row.dept && row.dept.deptName }}</template>
            <template #materialGoods="{ row }">{{ row.materialGoods.goodsCode }}</template>
            <template #menu-left>
                <div style="display: flex;">
                    <el-input v-model="goodsCode" placeholder="请输入物料编码"></el-input>
                    <el-button type="primary" style="margin-left: 10px;" @click="handleAdd">新增</el-button>
                </div>
            </template>
        </avue-crud>
    </basic-container>
</template>

<script>
import {getGoodsList,addGoods,editGoods} from "@/api/storeManagement/materialMaintenance"
// import { edit } from '@/api/orderManagement/exceptionOrder';
export default {
    data() {
        return {
            goodsCode: '',
            option: {
                columnSort: true,
                tip: false,
                height: 'auto',
                align: 'center',
                calcHeight: 32,
                simplePage: false,
                searchShow: true,
                searchMenuSpan: 6,
                searchIcon: true,
                searchIndex: 3,
                tree: false,
                border: true,
                index: false,
                selection: true,
                viewBtn: false,
                delBtn: false,
                editBtn: true,
                addBtnIcon: ' ',
                viewBtnIcon: ' ',
                delBtnIcon: ' ',
                editBtnIcon: ' ',
                editBtnText: '修改',
                addBtn: false,
                labelWidth: 120,
                searchLabelWidth: 120,
                menu: true,
                menuWidth: 80,
                dialogWidth: '60%',
                dialogClickModal: false,
                searchEnter: true,
                excelBtn: true,
                gridBtn: false,
                searchShowBtn: false,
                showOverflowTooltip: true,
                searchLabelPosition: 'left',
                searchLabelPosition: 'left',
                searchGutter: 24,
                searchSpan: 6,
                menuAlign: 'center',
                gridBtn: false,
                searchMenuPosition: 'right',
                column: [
                    {
                        label: '物料编码',
                        prop: 'goodsCode',
                        span: 12,
                        editDisabled: true,
                        overflow: true,
                        search: true,
                        width: 200,
                        // rules: [
                        //     {
                        //         required: true,
                        //         message: '请输入物料编码',
                        //         trigger: 'blur',
                        //     },
                        // ],
                    },
                    {
                        label: '物料名称',
                        prop: 'goodsName',
                        editDisabled: true,
                        span: 12,
                        overflow: true,
                        search: true,
                        width: 200,
                        // rules: [
                        //     {
                        //         required: true,
                        //         message: '请输入物料名称',
                        //         trigger: 'blur',
                        //     },
                        // ],
                    },
                    {
                        label: '采购部门',
                        prop: 'deptName',
                        span: 12,
                        editDisplay: false,
                        overflow: true,
                        search: false,
                        width: 200,
                        rules: [
                            {
                                required: true,
                                message: '请输入采购部门',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '型号/牌号',
                        prop: 'specifications',
                        span: 12,
                        overflow: true,
                        editDisplay: false,
                        search: true,
                        width: 200,
                        rules: [
                            {
                                required: true,
                                message: '请输入型号/牌号',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '计量单位',
                        prop: 'unitName',
                        span: 12,
                        // editDisplay: false,
                        overflow: true,
                        search: false,
                        width: 200,
                        rules: [
                            {
                                required: true,
                                message: '请输入计量单位',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: 'PDM单位',
                        prop: 'pdmUnit',
                        span: 12,
                        editDisplay: false,
                        overflow: true,
                        search: false,
                        width: 200,
                        rules: [
                            {
                                required: true,
                                message: '请输入PDM单位',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '物料类别',
                        prop: 'gcId',
                        span: 12,
                        type:"select",
                        // editDisplay: false,
                        overflow: true,
                        search: false,
                        width: 200,
                        rules: [
                            {
                                required: false,
                                message: '请输入物料类别',
                                trigger: 'blur',
                            },
                        ],
                        dicUrl:"/api/blade-wms/stGoodsClass/findGoodsClass",
                        props:{
                            label: 'gcName',
                            value: 'id'
                        }
                    },
                    {
                        label: '是否工艺审批',
                        prop: 'processApproval',
                        type: 'select',
                        dicData: [
                            { label: '是', value: true },
                            { label: '否', value: false },
                        ],
                        span: 12,
                        overflow: true,
                        search: false,
                        width: 200,
                        // rules: [
                        //     {
                        //         required: true,
                        //         message: '请输入是否工艺审批',
                        //         trigger: 'blur',
                        //     },
                        // ],
                    },
                    {
                        label: '补制类型',
                        prop: 'reissueType',
                        type: 'select',
                        dicData: [
                            // /**
                            // * 补制类型 - 无
                            // */
                            // public static Short REISSUE_TYPE_NONE = 0;

                            // /**
                            //  * 补制类型 - 物料出库
                            //  */
                            // public static Short REISSUE_TYPE_GOODS_OUT = 1;

                            // /**
                            //  * 补制类型 - 最低库存
                            //  */
                            // public static Short REISSUE_TYPE_MIN_STOCK = 2;
                            {label: '无', value: 0},
                            {label: '物料出库', value: 1},
                            {label: '最低库存', value: 2},
                        ],
                        span: 12,
                        overflow: true,
                        search: false,
                        width: 200,
                        rules: [
                            {
                                required: true,
                                message: '请选择补制类型',
                                trigger: 'blur',
                            },
                        ],
                        change:val =>{
                            if(val.value != 0){
                               let tmp = this.option.column.find(item => item.prop == 'gcId')
                               tmp.rules[0].required = true
                               if(val.value == 2){
                                let tmp = this.option.column.find(item => item.prop == 'minNum')
                                tmp.rules[0].required = true
                               }else{
                                let tmp = this.option.column.find(item => item.prop == 'minNum')
                                tmp.rules[0].required = false
                               }
                            }else{
                                let tmp = this.option.column.find(item => item.prop == 'gcId')
                                tmp.rules[0].required = false
                                let tmp1 = this.option.column.find(item => item.prop == 'minNum')
                                tmp1.rules[0].required = false
                            }
                        }
                    },
                    {
                        label: '等级',
                        prop: 'grade',
                        editDisplay: false,
                        span: 12,
                        overflow: true,
                        search: false,
                        width: 200,
                        rules: [
                            {
                                required: true,
                                message: '请输入等级',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '最高库存',
                        prop: 'maxNum',
                        span: 12,
                        overflow: true,
                        search: false,
                        width: 200,
                        // rules: [
                        //     {
                        //         required: true,
                        //         message: '请输入最高库存',
                        //         trigger: 'blur',
                        //     },
                        // ],
                    },
                    {
                        label: '最低库存',
                        prop: 'minNum',
                        span: 12,
                        overflow: true,
                        search: false,
                        width: 200,
                        rules: [
                            {
                                required: false,
                                message: '请输入最低库存',
                                trigger: 'blur',
                            },
                        ],
                    },
                    
                    {
                        label: '采购数量',
                        prop: 'purchaseQuantity',
                        span: 12,
                        overflow: true,
                        search: false,
                        width: 200,
                        // rules: [
                        //     {
                        //         required: true,
                        //         message: '请输入采购数量',
                        //         trigger: 'blur',
                        //     },
                        // ],
                    },
                    {
                        label: '是否停用',
                        editDisplay: true,
                        prop: 'used',
                        type: 'switch',
                        dicData: [
                            { label: '关', value: false },
                            { label: '开', value: true }
                        ],
                        span: 12,
                        overflow: true,
                        search: false,
                        width: 200,
                        rules: [
                            {
                                required: true,
                                message: '请输入是否停用',
                                trigger: 'blur',
                            },
                        ],
                    },
                    // {
                    //     label: '周期/天',
                    //     prop: 'cycle',
                    //     span: 12,
                    //     overflow: true,
                    //     search: false,
                    //     width: 200,
                    //     // rules: [
                    //     //     {
                    //     //         required: true,
                    //     //         message: '请输入周期/天',
                    //     //         trigger: 'blur',
                    //     //     },
                    //     // ],
                    // },
                    {
                        label: '版本',
                        prop: 'releaseNo',
                        span: 12,
                        overflow: true,
                        editDisplay: false,
                        search: false,
                        width: 200,
                        rules: [
                            {
                                required: true,
                                message: '请输入版本',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '属性',
                        prop: 'materialProperty',
                        span: 12,
                        overflow: true,
                        editDisplay: false,
                        search: false,
                        width: 200,
                        rules: [
                            {
                                required: true,
                                message: '请输入属性',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '当前库存',
                        prop: 'currentQuantity',
                        editDisplay: false,
                        span: 12,
                        overflow: true,
                        search: false,
                        width: 200,
                        rules: [
                            {
                                required: true,
                                message: '请输入当前库存',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        editDisplay: false,
                        label: '在途',
                        prop: 'onthewayQuantity',
                        span: 12,
                        overflow: true,
                        search: false,
                        width: 200,
                        rules: [
                            {
                                required: true,
                                message: '请输入在途',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '锁料',
                        prop: 'lockQuantity',
                        span: 12,
                        overflow: true,
                        editDisplay: false,
                        search: false,
                        width: 200,
                        rules: [
                            {
                                required: true,
                                message: '请输入锁料',
                                trigger: 'blur',
                            },
                        ],
                    },
                    {
                        label: '组件模式',
                        prop: 'prtType',
                        span: 12,
                        overflow: true,
                        search: false,
                        hide: true,
                        width: 200,
                        // rules: [
                        //     {
                        //         required: true,
                        //         message: '请输入组件模式',
                        //         trigger: 'blur',
                        //     },
                        // ],
                    },
                    {
                        label: '最低库存预警',
                        prop: 'minWarning',
                        type: 'switch',
                        dicData: [
                            { label: '关', value: false },
                            { label: '开', value: true }
                        ],
                        span: 12,
                        overflow: true,
                        search: false,
                        width: 200,
                        rules: [
                            {
                                required: true,
                                message: '请选择是否开启最低库存预警',
                                trigger: 'blur',
                            },
                        ],
                    },
                ]

            },
            loading: false,
            data: [],
            form: {},
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0,
            },
            query:{}
        }
    },
    mounted() {

    },
    methods: {
        handleAdd(){
            if(this.goodsCode == ''){
                this.$message.error('请填写物料编码')
                return
            }
            addGoods({
                goodsCode:this.goodsCode
            }).then(res =>{
                if(res.data.code == 200){
                    this.$message.success('添加成功')
                    this.onLoad()
                }
            })
        },
        searchChange(params, done){
            this.query = params;
            this.page.currentPage = 1;
            this.onLoad()
            done();
        },
        searchReset(){
            this.query = {}
            this.onLoad()
        },
        beforeOpen(done, type){
            if(type == 'edit'){
                this.form.minWarning = this.form.minWarning != '' ? this.form.minWarning : false
                this.form.reissueType = (this.form.reissueType && this.form.reissueType != -1) ? this.form.reissueType : 0
                this.form.gcId = this.form.gcId == -1 ? '' : this.form.gcId
                if(this.form.reissueType != 0){
                    let tmp = this.option.column.find(item => item.prop == 'gcId')
                    tmp.rules[0].required = true
                    if(this.form.reissueType == 2){
                        let tmp = this.option.column.find(item => item.prop == 'minNum')
                        tmp.rules[0].required = true
                    }else{
                        let tmp = this.option.column.find(item => item.prop == 'minNum')
                        tmp.rules[0].required = false
                    }
                }else{
                    let tmp = this.option.column.find(item => item.prop == 'gcId')
                    tmp.rules[0].required = false
                    let tmp1 = this.option.column.find(item => item.prop == 'minNum')
                    tmp1.rules[0].required = false
                }
                done()
            }else{
                done()
            }
            
        },
        rowUpdate(row, index, done, loading){
            console.log('row----------------',row)
            row.minWarning = row.minWarning ? 1 : 0
            editGoods(row).then(res =>{
                if(res.data.code == 200){
                    this.$message.success('修改成功')
                    this.onLoad()
                    done()
                }
            })
        },
        onLoad() {
            getGoodsList({
                current:this.page.currentPage,
                size:this.page.pageSize,
                ...this.query
            }).then(res =>{
                res.data.data.records.forEach(item =>{
                    item.minWarning = item.minWarning == 1 ? true : false
                })
                // res.data.data.records.map(item =>{
                //     item.gcId =  item.gcId == 0 ? null :item.gcId
                // })
                this.data = res.data.data.records
                this.page.total = res.data.data.total;
            })
            // this.data = [
            //     {
            //         "allGoodsQuantity": null,
            //         "batch": null,
            //         "bomQuantity": null,
            //         "buyQuantity": null,
            //         "cavityno": null,
            //         "checkoutCode": "3333",
            //         "coGoodsClass": null,
            //         "coStorageLocation": null,
            //         "cplb": null,
            //         "createTime": "2023-07-15 16:18:30",
            //         "curQuantity": 684.0,
            //         "cycle": null,
            //         "deleted": false,
            //         "deliveryDate": null,
            //         "dept": null,
            //         "drawingNo": null,
            //         "ecnno": null,
            //         "enditem": 0,
            //         "erpQuantity": null,
            //         "goodsCode": "21E543-6108-B1-N",
            //         "goodsCodeAndGoodsName": "null——21E543-6108-B1-N——石墨模",
            //         "goodsId": 167,
            //         "goodsName": "石墨模",
            //         "grade": null,
            //         "houseQuantity": null,
            //         "lockQuantity": 0.0,
            //         "madeIn": null,
            //         "matbigclassify": null,
            //         "matsmallclassify": null,
            //         "maxNum": 300.0,
            //         "memo": null,
            //         "minNum": 50.0,
            //         "minStockWarning": true,
            //         "mtlsgrt": 0.0,
            //         "mtltmrk": "232",
            //         "mustCheck": null,
            //         "newGoodsName": null,
            //         "newSpecifications": null,
            //         "numAcc": null,
            //         "onthewayQuantity": 0.0,
            //         "partLink": null,
            //         "piNo": null,
            //         "price": null,
            //         "priority": null,
            //         "prtType": "零件",
            //         "prtrevedit": null,
            //         "prtstd": null,
            //         "prtwt": 0.0,
            //         "purchaseQuantity": 100.0,
            //         "releaseno": null,
            //         "releasenoTime": "2023-03-23 00:00:00",
            //         "remark": null,
            //         "rlsId": null,
            //         "singlePieceQuota": null,
            //         "source": null,
            //         "special": null,
            //         "specifications": null,
            //         "syQuantity": null,
            //         "trademark": null,
            //         "unitName": "每个",
            //         "updateTime": "2023-07-15 16:18:30",
            //         "used": true
            //     },
            //     {
            //         "allGoodsQuantity": null,
            //         "batch": null,
            //         "bomQuantity": null,
            //         "buyQuantity": null,
            //         "cavityno": null,
            //         "checkoutCode": "1111",
            //         "coGoodsClass": null,
            //         "coStorageLocation": null,
            //         "cplb": null,
            //         "createTime": "2023-07-15 16:18:30",
            //         "curQuantity": 1950.0,
            //         "cycle": null,
            //         "deleted": false,
            //         "deliveryDate": null,
            //         "dept": null,
            //         "drawingNo": null,
            //         "ecnno": null,
            //         "enditem": 0,
            //         "erpQuantity": null,
            //         "goodsCode": "04-05-025",
            //         "goodsCodeAndGoodsName": "null——04-05-025——锡铂金",
            //         "goodsId": 169,
            //         "goodsName": "锡铂金",
            //         "grade": null,
            //         "houseQuantity": null,
            //         "lockQuantity": 0.0,
            //         "madeIn": null,
            //         "matbigclassify": null,
            //         "matsmallclassify": null,
            //         "maxNum": 300.0,
            //         "memo": null,
            //         "minNum": 50.0,
            //         "minStockWarning": true,
            //         "mtlsgrt": null,
            //         "mtltmrk": "23",
            //         "mustCheck": 0,
            //         "newGoodsName": null,
            //         "newSpecifications": null,
            //         "numAcc": null,
            //         "onthewayQuantity": 0.0,
            //         "partLink": null,
            //         "piNo": null,
            //         "price": null,
            //         "priority": null,
            //         "prtType": "零件",
            //         "prtrevedit": null,
            //         "prtstd": null,
            //         "prtwt": 0.0,
            //         "purchaseQuantity": 100.0,
            //         "releaseno": null,
            //         "releasenoTime": null,
            //         "remark": null,
            //         "rlsId": null,
            //         "singlePieceQuota": null,
            //         "source": null,
            //         "special": null,
            //         "specifications": "100克/瓶",
            //         "syQuantity": null,
            //         "trademark": null,
            //         "unitName": "每克",
            //         "updateTime": "2023-07-15 16:18:30",
            //         "used": true
            //     },
            //     {
            //         "allGoodsQuantity": null,
            //         "batch": null,
            //         "bomQuantity": null,
            //         "buyQuantity": null,
            //         "cavityno": null,
            //         "checkoutCode": "2222",
            //         "coGoodsClass": null,
            //         "coStorageLocation": null,
            //         "cplb": null,
            //         "createTime": "2023-07-15 16:18:30",
            //         "curQuantity": 2999.0,
            //         "cycle": null,
            //         "deleted": false,
            //         "deliveryDate": null,
            //         "dept": null,
            //         "drawingNo": null,
            //         "ecnno": null,
            //         "enditem": 0,
            //         "erpQuantity": null,
            //         "goodsCode": "21E543-6108-B1",
            //         "goodsCodeAndGoodsName": "null——21E543-6108-B1——石墨模",
            //         "goodsId": 170,
            //         "goodsName": "石墨模",
            //         "grade": null,
            //         "houseQuantity": null,
            //         "lockQuantity": 0.0,
            //         "madeIn": null,
            //         "matbigclassify": null,
            //         "matsmallclassify": null,
            //         "maxNum": 300.0,
            //         "memo": null,
            //         "minNum": 50.0,
            //         "minStockWarning": false,
            //         "mtlsgrt": null,
            //         "mtltmrk": "4",
            //         "mustCheck": 0,
            //         "newGoodsName": null,
            //         "newSpecifications": null,
            //         "numAcc": null,
            //         "onthewayQuantity": 0.0,
            //         "partLink": null,
            //         "piNo": null,
            //         "price": null,
            //         "priority": null,
            //         "prtType": "零件",
            //         "prtrevedit": null,
            //         "prtstd": null,
            //         "prtwt": 0.0,
            //         "purchaseQuantity": 0.0,
            //         "releaseno": null,
            //         "releasenoTime": null,
            //         "remark": null,
            //         "rlsId": null,
            //         "singlePieceQuota": null,
            //         "source": null,
            //         "special": null,
            //         "specifications": null,
            //         "syQuantity": null,
            //         "trademark": null,
            //         "unitName": "每个",
            //         "updateTime": "2023-07-15 16:18:30",
            //         "used": true
            //     }
            // ]
            // this.page.total = this.data.length
        }
    }
}
</script>

<style></style>