<template>
    <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" @selection-change="selectionChange"
        @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">
        <template #menu-left>

            <el-button type="primary" @click="moldAddFn(null, true)">新增</el-button>
            <!-- <el-button type="primary" @click="moldAddFn(null,true)">模具申报</el-button> -->
            <el-button type="primary" @click="closeNeed()">关闭需求</el-button>

        </template>
        <template #menu-right>

        </template>

        <template #poCode="{ row }">
            <el-text type="primary">{{ row.poCode }}</el-text>
        </template>
        <template #prodIdent="{ row }">
            <el-tag>{{ row.prodIdent }}</el-tag>
        </template>
        <template #menu="{ row }">
            <el-button type="primary" text plain style="border: 0; background-color: transparent !important"
                @click="detailsFn(row)">详情
            </el-button>
            <el-button type="primary" text plain style="border: 0; background-color: transparent !important"
                @click="moldAddOneFn(row, false)">模具申报
            </el-button>

        </template>
    </avue-crud>
    <!-- 模具申报 -->
    <addMoldDailog v-if="addOpen" :showDialog="addOpen" @closeDialog="closeDialog" :moldAddMore="moldAddMore">
    </addMoldDailog>
    <!-- 详情 -->
    <detailsInfoDialog v-if="detailsOpen" :showDialog="detailsOpen" @closeDialog="closeDialog"></detailsInfoDialog>
    <!-- 单个模具申报 -->
     <moldAddOneDialog v-if="moldAddOneOpen" :showDialog="moldAddOneOpen" @closeDialog="closeDialog"></moldAddOneDialog>
</template>
<script>
import addMoldDailog from './addMoldDailog.vue'
import detailsInfoDialog from './detailsInfoDialog.vue'
import moldAddOneDialog from './moldAddOneDialog.vue'
export default {
    components: {
        addMoldDailog,
        detailsInfoDialog,
        moldAddOneDialog
    },
    data() {
        return {
            activeName: 'first',
            inBatchesOpen: false,
            platingSmallOpen: false,
            planOrderArr: [],
            isOpen: false,
            rowItem: {},
            poId: null,
            form: {},
            box: false,
            props: {
                label: 'title',
                value: 'key',
            },
            menuGrantList: [],
            dataScopeGrantList: [],
            apiScopeGrantList: [],
            apiGrantList: [],
            menuTreeObj: [],
            dataScopeTreeObj: [],
            apiScopeTreeObj: [],
            selectionList: [],
            query: {},
            loading: true,
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0,
            },
            option: {
                columnSort: true,
                tip: false,
                height: 'auto',
                calcHeight: 32,
                simplePage: false,
                searchShow: true,
                searchMenuSpan: 12,
                searchIcon: true,
                searchIndex: 3,
                tree: false,
                border: true,
                index: false,
                selection: true,
                viewBtn: false,
                addBtn: false,
                delBtn: false,
                editBtn: false,
                editBtnText: '修改',
                labelWidth: 120,
                // searchLabelWidth: 120,
                menuWidth: 140,
                dialogWidth: 900,
                dialogClickModal: false,
                searchEnter: true,
                excelBtn: false,
                filterBtn: true,
                searchShowBtn: false,
                excelBtn: true,
                showOverflowTooltip: true,
                addBtnIcon: ' ',
                viewBtnIcon: ' ',
                delBtnIcon: ' ',
                editBtnIcon: ' ',
                gridBtn: false,
                searchLabelPosition: 'left',
                searchLabelPosition: 'left',
                searchGutter: 24,
                searchSpan: 6,
                menuAlign: 'left',
                gridBtn: false,
                searchMenuPosition: 'right',
                align: 'center',
                column: [
                    {
                        label: '需求单号',
                        prop: 'poCode',
                        search: false,
                        sortable: true,
                        span: 12,
                        headerAlign: 'center',
                        align: 'center',
                    },
                    {
                        label: '零件号',
                        prop: 'poCode2',
                        search: true,
                        sortable: true,
                        span: 12,
                        headerAlign: 'center',
                        align: 'center',
                        searchLabelWidth: 60,
                    },
                    {
                        label: '零件名称',
                        prop: 'poCode1',
                        search: false,
                        sortable: true,
                        span: 12,
                        headerAlign: 'center',
                        align: 'center',
                        searchLabelWidth: 60,
                    },
                    {
                        label: '工装号',
                        prop: 'poCode',
                        search: true,
                        sortable: true,
                        span: 12,
                        headerAlign: 'center',
                        align: 'center',
                        searchLabelWidth: 60,
                    },
                    {
                        label: '工装名称',
                        prop: 'partCode',
                        search: false,
                        sortable: true,
                        span: 12,
                        headerAlign: 'center',
                        align: 'left',
                    },
                    {
                        label: '订单需求量',
                        prop: 'productType',
                        search: false,
                        sortable: true,
                        span: 12,
                        headerAlign: 'center',
                        align: 'center',
                    },
                    {
                        label: '库存量',
                        prop: 'partName',
                        search: false,
                        sortable: true,
                        span: 12,
                        headerAlign: 'center',
                        align: 'center',
                    },
                    {
                        label: '占用量',
                        prop: 'prodIdent',
                        search: false,
                        sortable: true,
                        span: 12,
                        headerAlign: 'center',
                        align: 'center',
                    },
                    {
                        label: '关联订单',
                        prop: 'useDept',
                        search: false,
                        sortable: true,
                        span: 12,
                        headerAlign: 'center',
                        align: 'center',
                    },
                ],
            },

            data: [],
            addOpen: false,//模具申报
            moldAddMore: false,
            detailsOpen: false,//详情
            moldAddOneOpen:false,//单个模具申报
        };
    },

    methods: {
        // 关闭需求
        closeNeed() {
            this.$confirm('确定将选择需求关闭?', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    // return remove(row.id);
                })
                .then(() => {
                    // this.onLoad(this.page);
                    // this.$message({
                    //     type: 'success',
                    //     message: '操作成功!',
                    // });
                });
        },
        // 新增
        moldAddFn(row, flag) {
            this.addOpen = true
            this.moldAddMore = flag

        },
        // 模具申报
        moldAddOneFn(){
            this.moldAddOneOpen = true
        },
        // 详情
        detailsFn(row) {
            this.detailsOpen = true
        },
        // 关闭弹框
        closeDialog() {
            this.addOpen = false
            this.detailsOpen = false
            this.moldAddOneOpen = false
        },
        inBatchesFn(row) {
            this.rowItem = row;
            this.inBatchesOpen = true;
        },
        subclass() {
            this.platingSmallOpen = true;
        },
        lookProcessRoute(row) {
            this.isOpen = true;
            this.rowItem = row;
        },
        cancel(refresh) {
            if (refresh) {
                this.$refs.myTable.load();
            }
            this.inBatchesOpen = false;
            this.isOpen = false;
            this.planFormOpen = false;
            this.platingSmallOpen = false;
        },
        initData(roleId) {
            getRoleTreeById(roleId).then(res => {
                const column = this.findObject(this.option.column, 'parentId');
                column.dicData = res.data.data;
            });
        },
        submit() {
            const menuList = this.$refs.treeMenu.getCheckedKeys();
            const dataScopeList = this.$refs.treeDataScope.getCheckedKeys();
            const apiScopeList = this.$refs.treeApiScope.getCheckedKeys();
            grant(this.idsArray, menuList, dataScopeList, apiScopeList).then(() => {
                this.box = false;
                this.$message({
                    type: 'success',
                    message: '操作成功!',
                });
                this.selectionList = [];
                this.onLoad(this.page);
            });
        },
        rowSave(row, done, loading) {
            add(row).then(
                () => {
                    this.onLoad(this.page);
                    this.$message({
                        type: 'success',
                        message: '操作成功!',
                    });
                    done();
                },
                error => {
                    window.console.log(error);
                    loading();
                }
            );
        },
        rowUpdate(row, index, done, loading) {
            update(row).then(
                () => {
                    this.onLoad(this.page);
                    this.$message({
                        type: 'success',
                        message: '操作成功!',
                    });
                    done();
                },
                error => {
                    window.console.log(error);
                    loading();
                }
            );
        },
        rowDel(row) {
            this.$confirm('确定将选择数据删除?', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    return remove(row.id);
                })
                .then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: 'success',
                        message: '操作成功!',
                    });
                });
        },

        searchReset() {
            this.query = {};
            this.onLoad(this.page);
        },
        searchChange(params, done) {
            this.query = params;
            this.page.currentPage = 1;
            this.onLoad(this.page, params);
            done();
        },
        selectionChange(list) {
            this.selectionList = list;
        },
        selectionClear() {
            this.selectionList = [];
            this.$refs.crud.toggleSelection();
        },
        beforeOpen(done, type) {
            if (['add', 'edit'].includes(type)) {
                this.initData(this.form.id);
            }
            if (['edit', 'view'].includes(type)) {
                if (this.form.parentId === '0') {
                    this.form.parentId = '';
                }
            }
            getRoleAlias().then(res => {
                const column = this.findObject(this.option.column, 'currentAlias');
                column.dicData = res.data.data;
            });
            done();
        },
        handleRole() {
            if (this.selectionList.length !== 1) {
                this.$message.warning('只能选择一条数据');
                return;
            }
            this.menuTreeObj = [];
            this.dataScopeTreeObj = [];
            this.apiScopeTreeObj = [];
            grantTree().then(res => {
                this.menuGrantList = res.data.data.menu;
                this.dataScopeGrantList = res.data.data.dataScope;
                this.apiScopeGrantList = res.data.data.apiScope;
                getRole(this.ids).then(res => {
                    this.menuTreeObj = res.data.data.menu;
                    this.dataScopeTreeObj = res.data.data.dataScope;
                    this.apiScopeTreeObj = res.data.data.apiScope;
                    this.box = true;
                });
            });
        },
        handleRowRole(row) {
            this.menuTreeObj = [];
            this.dataScopeTreeObj = [];
            this.apiScopeTreeObj = [];
            grantTree().then(res => {
                this.menuGrantList = res.data.data.menu;
                this.dataScopeGrantList = res.data.data.dataScope;
                this.apiScopeGrantList = res.data.data.apiScope;
                getRole(row.id).then(res => {
                    this.menuTreeObj = res.data.data.menu;
                    this.dataScopeTreeObj = res.data.data.dataScope;
                    this.apiScopeTreeObj = res.data.data.apiScope;
                    this.selectionList.push(row);
                    this.box = true;
                });
            });
        },
        handleDelete() {
            if (this.selectionList.length === 0) {
                this.$message.warning('请选择至少一条数据');
                return;
            }
            this.$confirm('确定将选择数据删除?', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    return remove(this.ids);
                })
                .then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: 'success',
                        message: '操作成功!',
                    });
                    this.$refs.crud.toggleSelection();
                });
        },
        currentChange(currentPage) {
            this.page.currentPage = currentPage;
        },
        sizeChange(pageSize) {
            this.page.pageSize = pageSize;
        },
        refreshChange() {
            this.onLoad(this.page, this.query);
        },
        handleChange(file, fileList) {
            // proxy.$Export.xlsx(file.raw).then((data) => {
            //     data.value = data.results;
            // });
            this.$message({
                type: 'success',
                message: '操作成功!',
            });
        },

        onLoad(page, params = {}) {
            this.loading = true;
            this.data = [
                {
                    "alreadyBranchQty": 104.0,
                    "bsPlatingSmall": null,
                    "bsProcedureQua": null,
                    "closeMan": null,
                    "closeTime": null,
                    "curStatus": 4,
                    "curStatusText": "已派工",
                    "custodian": null,
                    "deliveryQty": 0.0,
                    "demandDate": "2025-03-13 00:00:00",
                    "dominantDept": "3400",
                    "dsPart": null,
                    "keyValue": 43933,
                    "memo": "最多三个批次0159BKFX",
                    "partCode": "21E6-575-1710_003-H14-C-N",
                    "partName": "09-J599_25烧结插座",
                    "planType": 2,
                    "planUser": "0300009",
                    "plate": "Ep.Ni3b",
                    "poArea": 0.0,
                    "poCode": "WO-2211134240",
                    "poId": 43933,
                    "poQty": 104,
                    "primaryCraft": "4（封接、表、涂色带）-库2",
                    "priority": 1,
                    "prodIdent": "JHT",
                    "prodLine": null,
                    "productType": "JY599",
                    "releaseDate": "2022-11-24 11:59:21",
                    "roamNo": "B5199029",
                    "routeNo": "LX08-05-002",
                    "sureType": null,
                    "sureTypeTitle": "未确认",
                    "syncTime": "2023-02-10 20:58:11",
                    "undeliveredQty": 104.0,
                    "useDept": "3502",
                    "wcName": null
                },
                {
                    "alreadyBranchQty": 104.0,
                    "bsPlatingSmall": null,
                    "bsProcedureQua": null,
                    "closeMan": null,
                    "closeTime": null,
                    "curStatus": 4,
                    "curStatusText": "已派工",
                    "custodian": null,
                    "deliveryQty": 0.0,
                    "demandDate": "2025-03-13 00:00:00",
                    "dominantDept": "3400",
                    "dsPart": null,
                    "keyValue": 43948,
                    "memo": "最多三个批次0159BKFX",
                    "partCode": "21E6-575-1710_003-H14-C-N",
                    "partName": "09-J599_25烧结插座",
                    "planType": 2,
                    "planUser": "0300009",
                    "plate": "Ep.Ni3b",
                    "poArea": 0.0,
                    "poCode": "WO-2211134240",
                    "poId": 43948,
                    "poQty": 104,
                    "primaryCraft": "4（封接、表、涂色带）-库2",
                    "priority": 1,
                    "prodIdent": "JHT",
                    "prodLine": null,
                    "productType": "JY599",
                    "releaseDate": "2022-11-24 11:59:21",
                    "roamNo": null,
                    "routeNo": "LX08-05-002",
                    "sureType": null,
                    "sureTypeTitle": "未确认",
                    "syncTime": "2023-02-10 20:58:11",
                    "undeliveredQty": 104.0,
                    "useDept": "3502",
                    "wcName": null
                },
                {
                    "alreadyBranchQty": 19.0,
                    "bsPlatingSmall": null,
                    "bsProcedureQua": null,
                    "closeMan": null,
                    "closeTime": null,
                    "curStatus": 4,
                    "curStatusText": "已派工",
                    "custodian": null,
                    "deliveryQty": 0.0,
                    "demandDate": "2025-02-23 00:00:00",
                    "dominantDept": "3400",
                    "dsPart": null,
                    "keyValue": 43932,
                    "memo": "最多三个批次0159BKFX",
                    "partCode": "21E6-575-1710_002-H14-N-N",
                    "partName": "09-J599_25烧结插座",
                    "planType": 2,
                    "planUser": "0300009",
                    "plate": "Ep.Ni3b",
                    "poArea": 0.0,
                    "poCode": "WO-2211134211",
                    "poId": 43932,
                    "poQty": 19,
                    "primaryCraft": "4（封接、表、涂色带）-库2",
                    "priority": 1,
                    "prodIdent": "JHT",
                    "prodLine": null,
                    "productType": "JY599",
                    "releaseDate": "2022-11-24 11:59:21",
                    "roamNo": null,
                    "routeNo": "LX08-05-002",
                    "sureType": null,
                    "sureTypeTitle": "未确认",
                    "syncTime": "2023-02-10 20:58:11",
                    "undeliveredQty": 19.0,
                    "useDept": "3502",
                    "wcName": null
                }
            ]
            this.page.total = this.data.length
            //   getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
            //     this.data = res.data.data.records;
            this.loading = false;
            //     this.page.total = res.data.data.total
            //     this.selectionClear();
            //   });
        },
    },
    mounted() {
    }
};
</script>
