<template>
    <basic-container>
        <avue-crud :option="option" :search.sync="searchForm" :table-loading="loading" :data="data" ref="crud"
            v-model="form" :permission="permissionList" @row-del="rowDel" @row-update="rowUpdate" @row-save="rowSave"
            :before-open="beforeOpen" :page.sync="page" @search-change="searchChange" @search-reset="searchReset"
            @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
            @refresh-change="refreshChange" @on-load="onLoad">
            <template slot-scope="{ row }" slot="status">
                <el-tag  :type="row.status == 0 ? 'danger' : 'success'">
                    {{row.status == 0 ? '待审核' : '已审核' }}
                </el-tag>
            </template>
            <template slot-scope="{ row }" slot="menu">
                <el-button @click="handleView(row)">查看</el-button>
                <el-button :disabled="row.status != 0" @click="handleImport(row)">审核</el-button>
            </template>
        </avue-crud>
        <el-dialog title="物品申领单" :visible.sync="dialogReceive" :append-to-body="true" width="70%" class="proRecive">
            <el-form ref="receiveForm" :model="receiveForm" :rules="reciveRules" label-width="80px"
                label-position="left">
                <div>
                    <div style="color: #101010;font-size: 20px;font-weight: 550;margin-bottom: 20px;">物品信息</div>
                    <el-table :data="receiveForm.tableData" border style="width: 100%;"
                        :style="{ 'margin-bottom': receiveForm.tableData.find(item => item.count > item.goodsNumber) ? '0px' : '30px' }">
                        <el-table-column label="物品名称" :resizable="false" prop="productName"></el-table-column>
                        <el-table-column label="品牌" :resizable="false" prop="brand"></el-table-column>
                        <el-table-column label="型号" :resizable="false" prop="xh"></el-table-column>
                        <el-table-column label="参数" :resizable="false" prop="rule"></el-table-column>
                        <!-- <el-table-column label="库存数量" :resizable="false" prop="goodsNumber"></el-table-column> -->
                        <el-table-column label="申领数量" :resizable="false" prop="applyNum">
                            <template slot-scope="scope">
                                <el-input-number @change="changeCount(scope.row)" disabled
                                    :class="scope.row.applyNum > scope.row.goodsNumber ? 'input_number' : ''" style="width:98%"
                                    v-model="scope.row.applyNum" label="入库数量"></el-input-number>
                            </template>
                        </el-table-column>
                        <el-table-column label="归还日期" :resizable="false">
                            <template slot-scope="scope">
                                <el-date-picker style="width:98%" disabled v-model="scope.row.returnTime" type="date"
                                    placeholder="请选择归还日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd">
                                </el-date-picker>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="error_tips" style="color: rgba(189,49,36,1);" v-show="isShowTips">超出库存，将进入采购流程</div>
                    <el-form-item label="申领人" prop="applyUser">
                        <el-input v-model="receiveForm.applyUser" disabled placeholder="请输入申领人"></el-input>
                    </el-form-item>
                    <el-form-item label="申领时间" prop="applyTime">
                        <el-date-picker style="width:98%" disabled v-model="receiveForm.applyTime" type="date" placeholder="请选择申领时间"
                            format="yyyy-MM-dd" value-format="yyyy-MM-dd">
                        </el-date-picker>
                    </el-form-item>
                </div>
                <div>
                    <div style="color: #101010;font-size: 20px;font-weight: 550;margin-bottom: 20px;">审核信息</div>
                    <el-form-item label="备注" prop="reason">
                        <el-input type="textarea" :disabled="viewType == 'view'" v-model="receiveForm.reason"  placeholder="请输入备注"></el-input>
                    </el-form-item>
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button v-show="viewType == 'view'" @click="closeRceive">关 闭</el-button>
                <el-button v-show="viewType != 'view'" @click="rejectRceive">驳 回</el-button>
                <el-button v-show="viewType != 'view'" type="primary" @click="handleRceive">通 过</el-button>
            </span>
        </el-dialog>
    </basic-container>

</template>

<script>
import {getReciviceList,getDetail,approveGoods} from '@/api/goodsManagement/recivice'
export default {
    data() {
        return {
            searchForm:{},
            treeOption: {
                nodeKey: 'id',
                // lazy: true,
                // treeLoad: function (node, resolve) {
                //   const parentId = (node.level === 0) ? 0 : node.data.id;
                //   getDeptLazyTree(parentId).then(res => {
                //     resolve(res.data.data.map(item => {
                //       return {
                //         ...item,
                //         leaf: !item.hasChildren
                //       }
                //     }))
                //   });
                // },
                addBtn: false,
                menu: false,
                size: 'small',
                props: {
                    labelText: '标题',
                    label: 'title',
                    value: 'value',
                    children: 'children'
                }
            },
            treeData: [
                {
                    hasChildren: true,
                    id: "1123598813738675201",
                    key: "1123598813738675201",
                    parentId: "0",
                    title: "物料库",
                    children: [
                        {
                            hasChildren: true,
                            id: "1123598813738675202",
                            key: "1123598813738675202",
                            parentId: "1123598813738675202",
                            title: "耗材",
                            value: "1123598813738675202",
                        },
                        {
                            hasChildren: true,
                            id: "1123598813738675203",
                            key: "1123598813738675203",
                            parentId: "1123598813738675203",
                            title: "备品备件",
                            value: "1123598813738675203",
                        }
                    ]
                },
                {
                    hasChildren: true,
                    id: "1123598813738675204",
                    key: "1123598813738675204",
                    parentId: "0",
                    title: "工具库",
                    children: [
                        {
                            hasChildren: true,
                            id: "1123598813738675205",
                            key: "1123598813738675205",
                            parentId: "1123598813738675205",
                            title: "工具",
                            value: "1123598813738675205",
                        },
                        {
                            hasChildren: true,
                            id: "1123598813738675206",
                            key: "1123598813738675206",
                            parentId: "1123598813738675206",
                            title: "设备",
                            value: "1123598813738675206",
                        }
                    ]
                }
            ],
            option: {
                height: 'auto',
                calcHeight: 80,
                tip: false,
                searchShow: true,
                searchMenuSpan: 6,
                border: true,
                index: true,
                selection: true,
                addBtn: false,
                viewBtn: false,
                editBtn: false,
                delBtn: false,
                dialogType: 'dialog',
                dialogClickModal: false,
                searchShowBtn: false,
                refreshBtn: false,
                columnBtn: false,
                menuWidth: 240,
                column: [
                    {
                        label: "申领人",
                        labelWidth: 120,
                        prop: "applyUser",
                        overHidden: true,
                        search:true
                    },
                    {
                        label: "种类数量",
                        labelWidth: 120,
                        prop: "applyTypeNum",
                        overHidden: true,
                    },
                    {
                        label: "提交时间",
                        labelWidth: 120,
                        prop: "applyTime",
                        overHidden: true,
                    },
                    {
                        label: "状态",
                        labelWidth: 120,
                        prop: "status",  //0-待审核  1-已审核
                        slot:true,
                        overHidden: true,
                    },
                    {
                        label: "审核人",
                        labelWidth: 120,
                        prop: "approveUser",  
                        overHidden: true,
                    },
                    {
                        label: "审核时间",
                        labelWidth: 120,
                        prop: "approveTime",  
                        overHidden: true,
                    },
                    {
                        label: "备注",
                        labelWidth: 120,
                        overHidden: true,
                        prop: "remark",
                    },
                ],
            },
            data: [],
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0,
            },
            selectedList: [],
            dialogVisible: false,
            tableData: [],
            dialogReceive: false,
            receiveForm: {
                tableData: []
            },
            reciveRules: {
                applyUser: { required: true, message: '请输入申领人', trigger: 'blur' },
                applyTime: { required: true, message: '请选择申领时间', trigger: 'blur' },
            },
            isShowTips: false,
            viewType:''
        }
    },
    created() { },
    methods: {
        beforeOpen(done, type) {
            done();
        },

        beforeClose(done) {
            done();
        },
        searchReset(params, done) {
            this.query = params;
            this.onLoad(this.page);
        },
        searchChange(params, done) {
            this.query = params;
            this.page.currentPage = 1;
            this.onLoad(this.page, params);
            done();
        },
        refreshChange() {
            this.onLoad(this.page, this.query);
        },
        selectionChange(list) {
            this.selectedList = list
        },
        // 批量入库
        handleAdd() {
            if (this.selectedList.length == 0) {
                this.$message.warning('请至少选择一条数据')
            } else {
                this.tableData = []
                this.selectedList.map(item => {
                    this.tableData.push({
                        id: item.goodsId,                    //物资id
                        goodsName: item.goodsName,		    //物资名称
                        brand: item.brand,						//品牌
                        model: item.model,						//型号
                        number: 1,						//数量
                    })
                })
                this.dialogVisible = true
            }
        },
        // 批量申领
        handleReceive() {
            if (this.selectedList.length == 0) {
                this.$message.warning('请至少选择一条数据')
            } else {
                this.isShowTips = false
                this.receiveForm.tableData = []
                this.selectedList.map(item => {
                    this.receiveForm.tableData.push({
                        id: item.goodsId,                    //物资id
                        goodsName: item.goodsName,		    //物资名称
                        brand: item.brand,					//品牌
                        model: item.model,	                //型号
                        parameter: '',                       //参数
                        count: 0,                            //申领数量					
                        goodsNumber: item.goodsNumber,		//库存数量
                        returnTime: ''                       //返还时间
                    })
                })
                this.dialogReceive = true
            }
        },
        handleView(row){
            getDetail({id:row.id}).then(res =>{
                this.viewType = 'view',
                this.isShowTips = false
                this.receiveForm = res.data.data
                this.receiveForm.tableData = res.data.data.details
                this.dialogReceive = true
            })
            
        },
        // 申领按钮
        handleImport(row) {
            getDetail({id:row.id}).then(res =>{
                this.viewType = 'examine'
                this.isShowTips = false
                this.receiveForm = res.data.data
                this.receiveForm.tableData = res.data.data.details
                this.dialogReceive = true
            })
            
        },
        // 点击批量入库中的删除按钮
        deleteGoods(row) {
            if (this.tableData.length == 1) {
                this.$message.warning('请至少为一条物品进行入库操作')
            } else {
                console.log('this.table==========>', this.tableData)
                this.$confirm("确定删除该条数据吗?", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(() => {
                    this.tableData = this.tableData.filter(item => item.id == row.id)
                })
            }
        },
        // 点击取消按钮
        handleCancel() {
            this.dialogVisible = false
            this.tableData = []
        },
        // 点击确认按钮
        handleSave() {
            console.log('table---------->', this.tableData)
            this.dialogVisible = false
        },
        // 申领数量发生变化
        changeCount(row, currentValue) {
            console.log('row===========>', row)
            if (row.count > row.goodsNumber) {
                this.isShowTips = true
            } else {
                this.isShowTips = false
            }
        },
        // 点击申领单确认按钮
        handleRceive() {
            this.$confirm('请确认是否通过该申领记录?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let query = {
                    id:this.receiveForm.id,
                    status:1,
                    reason:this.receiveForm.reason
                }
                approveGoods(query).then(res =>{
                    if(res.data.code == 200){
                        this.$message.success('通过成功')
                        this.onLoad()
                        this.dialogReceive = false
                    }
                })
                
            })
        },
        // 点击申领单取消按钮
        rejectRceive() {
            this.$confirm('请确认是否驳回该申领记录?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$message.success('驳回成功')
                this.dialogReceive = false
            })
        },
        // 点击申领审核关闭按钮
        closeRceive(){
            this.dialogReceive = false
        },
        onLoad(page, params = {}) {
            // this.data = [
            //     {applyUser:'张三',typeNumber:3,applyTime:'2024-07-16',status:0,examineUser:'',examineDate:'',remark:'',
            //     tableData:[
            //         {goodsId:'001',goodsName:'钻头',brand:'德力西',parameter:'',model:'50mm',goodsNumber:'60',count:'10',returnTime:'2024-07-14'},
            //         {goodsId:'002',goodsName:'开孔器',brand:'德力西',parameter:'',model:'50mm',goodsNumber:'50',count:'15',returnTime:'2024-07-14'},
            //         {goodsId:'003',goodsName:'空开',brand:'德力西',parameter:'',model:'48*48',goodsNumber:'50',count:'15',returnTime:'2024-07-14'},
            //     ],
            //     },
            //     {applyUser:'李四',typeNumber:2,applyTime:'2024-07-12',status:1,examineUser:'王五',
            //     examineDate:'2024-07-13',remark:'',
            //     tableData:[
            //         {goodsId:'001',goodsName:'钻头',brand:'德力西',parameter:'',model:'50mm',goodsNumber:'60',count:'10',returnTime:'2024-07-14'},
            //         {goodsId:'002',goodsName:'开孔器',brand:'德力西',parameter:'',model:'50mm',goodsNumber:'50',count:'15',returnTime:'2024-07-14'},
            //     ]
            // },
            // ]
            // this.page.total = this.data.length
            let query = {
                applyUser:this.searchForm.applyUser ? this.searchForm.applyUser : null,
                current:this.page.currentPage,				          //页数
                size:this.page.pageSize,
            }
            getReciviceList(query).then(res =>{
                this.data = res.data.data.records
                this.page.total = res.data.data.total
            })
        }
    }
}
</script>

<style lang="scss" scoped></style>

<style lang="scss">
.proRecive {
    .input_number {
        .el-input__inner {
            color: red;
        }
    }

    .error_tips {
        width: 180px;
        height: 30px;
        background: rgba(222, 134, 143, 0.43);
        margin-bottom: 30px;
        margin-left: 71.8%;
        margin-top: 5px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}
</style>