<template>
    <el-dialog :title="title" v-if="visible" :visible.sync="visible" :before-close="handleClose"
        :close-on-click-modal="true" :close-on-press-escape="false" append-to-body width="70%">

        <el-card shadow="never">
            <saleOrderSearch @search="reload" />

            <!-- 数据表格 -->
            <ele-pro-table ref="table" :columns="columns" :datasource="datasource" :selection.sync="selection" row-key="id">

            </ele-pro-table>


        </el-card>

        <div class="btns">
            <el-button type="primary" size="small" @click="selected">选择</el-button>
            <el-button size="small" @click="handleClose">关闭</el-button>
        </div>
    </el-dialog>
</template>
  
<script>
import saleOrderSearch from './saleOrder-search'
import { getPageList } from '@/api/saleOrder';
import dictMixins from '@/mixins/dictMixins';
export default {
    components: { saleOrderSearch },
    mixins: [dictMixins],
    data() {
        return {
            visible: false,
            title: '销售订单',

            // 表格列配置
            columns: [
                {
                    columnKey: 'selection',
                    type: 'selection',
                    width: 45,
                    align: 'center',
                    selectable: (row, index) => {
                        return !this.tableData.some((it) => it.id == row.id ||  it.salesOrderId == row.id);
                    },
                    reserveSelection: true,
                    fixed: 'left'
                },
                {
                    prop: 'code',
                    label: '销售订单号',
                    align: 'center',
                    showOverflowTooltip: true,
                    minWidth: 150,
                    slot: 'code',
                    sortable: true
                },
                {
                    prop: 'lineNumber',
                    label: '行号',
                    align: 'center',
                    showOverflowTooltip: true
                },
                {
                    prop: 'productCode',
                    label: '产品编码',
                    align: 'center',
                    showOverflowTooltip: true,
                    minWidth: 140
                },

                {
                    prop: 'productName',
                    label: '产品名称',
                    align: 'center',
                    minWidth: 120
                },
            
                {
                    prop: 'brandNo',
                    label: '牌号',
                    align: 'center'
                },
                {
                    prop: 'model',
                    label: '型号',
                    align: 'center',
                    minWidth: 120
                },
                {
                    prop: 'productSumWeight',
                    label: '订单重量',
                    align: 'center'
                },
                {
                    prop: 'contractNum',
                    label: '订单数量',
                    align: 'center'
                },

                {
                    prop: 'status',
                    label: '生产状态',
                    align: 'center',
                    showOverflowTooltip: true,
                    formatter: (_row, _column, cellValue) => {
                        return this.getDictValue('生产状态', _row.status);
                    }
                },


            ],

            // 表格选中数据
            selection: [],

            tableData: [],
            current: null

        }
    },

    watch: {

    },
    created() {
        this.requestDict('生产状态');
    },
    methods: {

        open(item) {
            if (item) {
                this.tableData = item
            }

            this.visible = true



        },


        /* 表格数据源 */
        async datasource({ page, limit, where }) {
            where.status = [1, 2,  3, 4, 7,8]
            const data = await getPageList({
                ...where,
                pageNum: page,
                size: limit
            });



            return data;
        },

        /* 刷新表格 */
        reload(where) {
            this.$refs.table.reload({ page: 1, where: where });
        },






        handleClose() {
            this.visible = false
            this.$refs.table.setSelectedRows([]);
            this.selection = []

        },
        selected() {

            if (!this.selection.length) {
                this.$message.error('请至少选择一条数据');
                return;
            }
            this.$emit('chooseOrder', this.selection)
            this.handleClose()
        },
    }
}
</script>
  
<style lang="scss" scoped>
.btns {
    text-align: center;
    padding: 10px 0;
}
</style>
  