<template>
    <basic-container>
        <avue-crud ref="materalCrud" :option="option" :table-loading="loading" :data="data" v-model="form" v-model:page="page" 
            @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset"
            @row-save="rowSave" @row-update="rowUpdate" :before-open="beforeOpen"
            @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
            @refresh-change="refreshChange" @on-load="onLoad">
            <!-- <template #goodsCode="{ row }">{{ row.coGoods.goodsCode }}</template>
            <template #goodsName="{ row }">{{ row.coGoods.goodsName }}</template>
            <template #bsBasicClass="{ row }">{{ row.bsBasicClass.name }}</template>
            <template #turnover="{ row }">{{ row.turnover.userName }}</template>
            <template #mtltmrk="{ row }">{{ row.coGoods.mtltmrk }}</template>
            <template #specifications="{ row }">{{ row.coGoods.specifications }}</template> -->
            <template #menu-left>
                <el-button type="danger" @click="handleDelete">删除</el-button>
                <el-button type="primary" @click="handlePaint">缺漆信息</el-button>
            </template>
            <template #menu-right>
                <el-button type="danger" @click="handleSearch(1)" plain>需报料</el-button>
                <el-button type="warning" @click="handleSearch(2)" plain>需跟踪</el-button>
                <el-button type="primary" @click="handleSearch(3)" plain>待领料</el-button>
                <el-button type="success" @click="handleSearch(4)" plain>正常</el-button>
                <el-button type="info" @click="handleSearch(5)" plain>长期未使用</el-button>
            </template>
        </avue-crud>
        <paint-loss-dialog v-if="isOpen" :openShow="isOpen" title="缺漆信息" :data="laInData" @closeDialog="closeDialog"></paint-loss-dialog>
    </basic-container>
</template>

<script>
import {getMaterialStatusList,addMaterialStatus,editMaterialStatus,delMaterialStatus} from "@/api/storeManagement/materialPreserve"
import paintLossDialog from './components/paintLossDialog.vue'
export default {
    components: {
        paintLossDialog
    },
    data() {
        return {
            selectionList: [],
            loading: false,
            data: [],
            form: {},
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0,
            },
            isOpen:false,
            query:{},
            laInData:[],
            option: {
                tip: false,
                align: 'center',
                size: 'medium',
                height: 'auto',
                
                labelWidth: 150,
                simplePage: true,
                searchShow: true,
                searchMenuSpan: 6,
                searchIcon: true,
                searchIndex: 3,
                tree: false,
                border: true,
                selection: true,
                viewBtn: false,
                editBtn: true,
                addBtn: true,
                delBtn: false,
                editBtnText: '修改',
                addBtnIcon: ' ',
                viewBtnIcon: ' ',
                delBtnIcon: ' ',
                editBtnIcon: ' ',
                viewBtnText: '详情',
                menuWidth: 70,
                dialogWidth: 1200,
                dialogClickModal: false,
                searchEnter: true,
                excelBtn: false,
                filterBtn: true,
                searchShowBtn: false,
                excelBtn: true,
                index: false,
                showOverflowTooltip: true,
                searchLabelPosition: 'left',
                searchLabelPosition: 'left',
                searchGutter: 24,
                searchSpan: 6,
                menuAlign: 'left',
                gridBtn: false,
                searchMenuPosition: 'right',
                column: [
                    {
                        label: '物料编码',
                        prop: 'goodsCode',
                        type: 'select',
                        addDisplay: false,
                        editDisplay: false,
                        span: 8,
                        hide:true,
                        width: 200,
                        search: true,
                        // 
                        rules: [
                            {
                                required: true,
                                message: '请输入物料编码',
                                trigger: 'click',
                            },
                        ],
                        dicUrl:"/api/blade-wms/stGoods/list",
                        remote: true,
                        props:{
                            label: 'goodsCode',
                            value: 'goodsCode',
                            res: 'data.records',
                        },
                        // onChange:(value =>{
                        //     let tmp = value.dic.find(item => item.id == value.value)
                        //     this.form.goodsName = tmp.goodsName
                        // })
                    },
                    {
                        label: '物料编码',
                        prop: 'goodsId',
                        type: 'select',
                        addDisplay: true,
                        editDisplay: true,
                        span: 8,
                        width: 200,
                        search: false,
                        // 
                        rules: [
                            {
                                required: true,
                                message: '请输入物料编码',
                                trigger: 'click',
                            },
                        ],
                        dicUrl:"/api/blade-wms/stGoods/list",
                        remote: true,
                        props:{
                            label: 'goodsCode',
                            value: 'id',
                            res: 'data.records',
                        },
                        onChange:(value =>{
                            let tmp = value.dic.find(item => item.id == value.value)
                            this.form.goodsName = tmp.goodsName
                        })
                    },
                    {
                        label: '物料名称',
                        prop: 'goodsName',
                        addDisabled: true,
                        addDisplay: true,
                        editDisplay: true,
                        span: 8,
                        width: 200,
                        search: true,
                        rules: [
                            {
                                required: true,
                                message: '请输入物料名称',
                                trigger: 'click',
                            },
                        ],
                    },
                    
                    // {
                    //     label: '物料标记',
                    //     prop: 'bsBasicClass',
                    //     type: 'select',
                    //     addDisplay: true,
                    //     editDisplay: true,
                    //     span: 8,
                    //     width: 200,
                    //     search: true,
                    //     rules: [
                    //         {
                    //             required: true,
                    //             message: '请输入物料标记',
                    //             trigger: 'click',
                    //         },
                    //     ],
                    //     props: {
                    //         label: 'name',
                    //         value: 'code'
                    //     },
                    //     dicData: [
                    //         {
                    //             "bcId": 49,
                    //             "code": "11",
                    //             "deleted": false,
                    //             "keyValue": 49,
                    //             "memo": "11",
                    //             "name": "bj01",
                    //             "type": 1,
                    //             "updateTime": null
                    //         },
                    //         {
                    //             "bcId": 69,
                    //             "code": "bj02",
                    //             "deleted": false,
                    //             "keyValue": 69,
                    //             "memo": "22",
                    //             "name": "bj02",
                    //             "type": 1,
                    //             "updateTime": null
                    //         }
                    //     ]
                    // },
                    {
                        label: '型号/牌号',
                        prop: 'materialModel',
                        addDisplay: false,
                        editDisplay: false,
                        span: 8,
                        width: 200,
                        search: false,
                        rules: [
                            {
                                required: true,
                                message: '请输入型号/牌号',
                                trigger: 'click',
                            },
                        ],
                    },
                    {
                        label: '物料规格',
                        prop: 'specifications',
                        addDisplay: false,
                        editDisplay: false,
                        span: 8,
                        width: 200,
                        search: false,
                        rules: [
                            {
                                required: true,
                                message: '请输入物料规格',
                                trigger: 'click',
                            },
                        ],
                    },
                    {
                        label: '季度月使用量均值',
                        prop: 'useAverage',
                        addDisplay: true,
                        editDisplay: true,
                        span: 8,
                        width: 200,
                        search: false,
                        // rules: [
                        //     {
                        //         required: true,
                        //         message: '请输入季度月使用量均值',
                        //         trigger: 'click',
                        //     },
                        // ],
                    },
                    {
                        label: '一级库存',
                        prop: 'firstStore',
                        addDisplay: false,
                        editDisplay: false,
                        span: 8,
                        width: 200,
                        search: false,
                        rules: [
                            {
                                required: true,
                                message: '请输入一级库存',
                                trigger: 'click',
                            },
                        ],
                    },
                    {
                        label: '二级库存',
                        prop: 'lastStore',
                        addDisplay: false,
                        editDisplay: false,
                        span: 8,
                        width: 200,
                        search: false,
                        rules: [
                            {
                                required: true,
                                message: '请输入二级库存',
                                trigger: 'click',
                            },
                        ],
                    },
                    {
                        label: '计划数量',
                        prop: 'planQty',
                        addDisplay: false,
                        editDisplay: false,
                        span: 8,
                        width: 120,
                        search: false,
                        rules: [
                            {
                                required: true,
                                message: '请输入计划数量',
                                trigger: 'click',
                            },
                        ],
                    },
                    {
                        label: '是否油漆',
                        prop: 'paint',
                        type: 'select',
                        addDisplay: true,
                        editDisplay: true,
                        span: 8,
                        width: 120,
                        search: false,
                        rules: [
                            {
                                required: true,
                                message: '请输入是否油漆',
                                trigger: 'click',
                            },
                        ],
                        dicData: [
                            { label: '是', value: 1 },
                            { label: '否', value: 0 },
                        ]
                    },
                    {
                        label: '周转工',
                        prop: 'turnover',
                        type: 'select',
                        addDisplay: true,
                        editDisplay: true,
                        span: 8,
                        width: 200,
                        search: true,
                        searchLabelWidth: 60,
                        // rules: [
                        //     {
                        //         required: true,
                        //         message: '请输入周转工',
                        //         trigger: 'click',
                        //     },
                        // ],
                        props: {
                            label: 'realName',
                            value: 'id',
                            res: 'data.records',
                        },
                        dicUrl:'/api/blade-system/user/page'
                    },
                    {
                        label: '配送标记',
                        prop: 'delivery',
                        type: "select",
                        addDisplay: true,
                        editDisplay: true,
                        span: 8,
                        width: 120,
                        search: true,
                        rules: [
                            {
                                required: true,
                                message: '请输入配送标记',
                                trigger: 'click',
                            },
                        ],
                        dicData: [
                            { label: '是', value: 1 },
                            { label: '否', value: 0 },
                        ]
                    },
                    {
                        label: '是否特殊',
                        prop: 'special',
                        type: 'select',
                        addDisplay: true,
                        editDisplay: true,
                        span: 8,
                        width: 120,
                        search: true,
                        rules: [
                            {
                                required: true,
                                message: '请输入是否特殊',
                                trigger: 'click',
                            },
                        ],
                        dicData: [
                            { label: '是', value: true },
                            { label: '否', value: false },
                        ]
                    },
                    {
                        label: '状态',
                        prop: 'goodsStatus',
                        type: 'select',
                        addDisplay: false,
                        editDisplay: false,
                        span: 8,
                        width: 200,
                        search: true,
                        rules: [
                            {
                                required: true,
                                message: '请输入状态',
                                trigger: 'click',
                            },
                        ],
                        dicData: [
                            {
                                value: 1,
                                label: '需报料'
                            }, {
                                value: 2,
                                label: '需跟踪'
                            }, {
                                value: 3,
                                label: '待领料'
                            }, {
                                value: 4,
                                label: '正常'
                            }, {
                                value: 5,
                                label: '长期未使用'
                            }
                        ]
                    },
                ]
            }
        }
    },
    mounted() {

    },
    methods: {
        searchChange(params, done){
            this.query = params
            this.page.currentPage = 1;
            this.onLoad()
            done()
        },
        searchReset(){
            this.query = {}
            this.onLoad()
        },
        currentChange(currentPage){
            this.page.currentPage = currentPage
        },
        sizeChange(pageSize){
            this.page.pageSize = pageSize
        },
        refreshChange() {
            this.onLoad();
        },
        selectionChange(val) {
            this.selectionList = val
        },
        handleSearch(val){
            this.query.goodsStatus = val
            this.onLoad()
        },
        closeDialog(){
            this.isOpen = false
        },
        beforeOpen(done, type) {
            if (['edit', 'view'].includes(type)) {
                console.log('form---------------------',this.form)
                this.form.goodsId = this.form.goodsId + ''
            }
            done();
        },
        handlePaint(){
            this.laInData = []
            const tableData = this.data
            console.log('tableData------------------',tableData)
            tableData.forEach(item => {
                if (item.goodsStatus === 1 && item.paint) {
                    this.laInData.push(item);
                }
            });
            console.log('laInData------------------',this.laInData)
            if (this.laInData.length > 0) {
                this.isOpen = true;
            } else {
                this.$message.info('暂无缺漆信息数据');
            }
        },
        handleDelete() {
            if (this.selectionList.length == 0) {
                this.$message.error('请至少选择一条数据')
            } else {
                this.$confirm('确定删除选中数据?', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }).then(() => {
                    delMaterialStatus({
                        ids:this.selectionList.map(item => item.id).join(',')
                    }).then(res =>{
                        if(res.data.code == 200){
                            this.$message.success('删除成功')
                            this.onLoad()
                        }
                    })
                })
            }
        },
        rowSave(row, done, loading){
            let params = {
                goodsId:row.goodsId,
                useAverage:row.useAverage,
                delivery:row.delivery == 1 ? true : false,
                paint:row.paint == 1 ? true : false,
                special:row.special,
                turnover:row.turnover,
            }
            addMaterialStatus(params).then(res =>{
                if(res.data.code == 200){
                    this.$message.success('新增成功')
                    this.onLoad()
                    done()
                }
            })
        },
        rowUpdate(row, index, done, loading){
            row.delivery = row.delivery == 1 ? true : false
            row.paint = row.paint == 1 ? true : false
            editMaterialStatus(row).then(res =>{ 
                if(res.data.code == 200){
                    this.$message.success('修改成功')
                    this.onLoad()
                    done()
                }
            })
        },
        onLoad() {
            getMaterialStatusList({
                current:this.page.currentPage,
                size:this.page.pageSize,
                ...this.query
            }).then(res =>{
                res.data.data.records.map(item =>{
                    item.paint = item.paint != '' && item.paint ? 1 : 0 
                    item.delivery = item.delivery != '' && item.delivery ? 1 : 0 
                })
                this.data = res.data.data.records
                console.log('data-------------',this.data)
                this.page.total = res.data.data.total;
            })
        }
    }
}
</script>

<style></style>