<template>
    <lj-win-grid-view :title="title" api-name="GetL1OrderMl" :columns="columns"
                      :query-fields="queryFields"
                      :query-hard-parms="queryHardParms"
                      :reset-query-parms="handleResetQueryParms"
                      @loaded = "handleLoaded"
                      @cell-click="handleCellClick"
                      @query-field-click="queryClick"
                      @query-field-change="queryChange"
                      @scan-click="handleClickScan"
                      pagination
                      if-show-searchbar
                      if-show-scan
                      :searchText="keyword"
                      hint="输入指令单号/生产批号/物料编码/物料名称查找"
                      ref="baseWin">
        <mu-row slot="footer" v-if="selectMode" align-items="center">
            <mu-col>
                <div style="display:flex;align-items:center;margin-left: 10px;">
                    <i class="iconfont icon-cart"></i>
                    共{{selectedItems.length}}套
                </div>
            </mu-col>
            <mu-col span="4" tag="mu-button" color="primary" @click="handleSelectFinish">
                选好了
            </mu-col>
        </mu-row>
    </lj-win-grid-view>
</template>

<script>
    import LjWinGridView from "components/lj-win-grid-view";

    export default {
        name: "mtrlware",
        components: {LjWinGridView},
        created() {
            let pageParam = $lj.getPageParam();
            if (pageParam.winMode) {
                this.winMode = pageParam.winMode;
                this.winFrom = pageParam.winFrom;
            }

            if (typeof pageParam.queryParams !== 'undefined') {
                this.queryHardParms = Object.assign(this.queryHardParms, pageParam.queryParams);
            }
            if (this.selectMode) {
                this.columns = [{
                    property: "checked",
                    label: "选",
                    type: "selection",
                    editable: true,
                    width: 70,
                    fixed: true,
                    headerType: "selection"
                }].concat(this.columns);

                this.queryFields[3].initValue = 1;
                this.queryFields[3].disabled = true;

                this.queryFields[0].initValue = this.wrkGrpid;
                this.queryFields[0].disabled = true;

            }
        },
        data() {
            return {
                selectedItems: [],
                winMode: "",
                wrkGrpid: -1,
                queryHardParms: {
                },
                keyword:"",
                queryFields: [
					{
						title: '车间', property: 'wrkGrpid',
						selectOptions: {
							optionsApi: "GetScWkps",
                            valueMap: ["wrkGrpid", "wrkGrpName"],
                            extraOptions: [{wrkGrpid: -1, wrkGrpName: '全部'}]
                        },
                        initValue: -1,
					},
					{title: '开始日期', property: 'begindate', type: 'date', staticDateInit: -90},
					{title: '结束日期', property: 'enddate', type: 'date'},
					{
						title: '状态', property: 'status',
						selectOptions: {
							options: [
								{status: -1, dscrp: '全部'},
								{status: 10, dscrp: '未完成'},
								{status: 1, dscrp: '在进行'},
								{status: 2, dscrp: '已完成'},
								{status: 0, dscrp: '待审核'},
								{status: 3, dscrp: '已暂停'},
								{status: 4, dscrp: '已终止'},
							],
                            valueMap: ["status", "dscrp"],
                        },
                        initValue: 0,
					},
					{
						title: '订单分类', property: 'typeid',
						selectOptions: {
							optionsApi: "GetL1OrderMlType",
                            valueMap: ["typeid", "typename"],
                            extraOptions: [{typeid: -1, typename: '全部'}]
						}
					}
				],
                columns: [
                    {property: "OrderCode", label: "指令单号", width: screen.width * 0.3, fixed: true},
                    {property: "mtrlcode", label: "物料编码", width: screen.width * 0.3, fixed: true},
                    {property: "mtrlname", label: "物料名称", width: screen.width * 0.3, fixed: true},
                    {property: "mtrlmode", label: "物料规格", width: screen.width * 0.3, fixed: true},
                    {property: "unit", label: "单位", width: screen.width * 0.1, fixed: true},
                    {property: "orderqty", label: "计划数", width: screen.width * 0.3, fixed: true,type:"number"},
                    {property: "AcmpQty", label: "完成数", width: screen.width * 0.3, fixed: true,type:"number"},
                    {property: "notauditqty", label: "已开单\r\n未审数", width: screen.width * 0.3, fixed: true,type:"number"},
                    {property: "stopqty", label: "终止数", width: screen.width * 0.3, fixed: true,type:"number"},
                    {property: "nocmpqty", label: "未完成数", width: screen.width * 0.3, fixed: true,type:"number"},
                    {property: "orderdate", label: "计划日期", width: screen.width * 0.3, fixed: true},
                    {property: "requiredate", label: "完成日期", width: screen.width * 0.3, fixed: true},
                    {property: "taskrelcode", label: "生产批号", width: screen.width * 0.3, fixed: true},
                    {property: "plan_flag", label: "备料审", width: screen.width * 0.3, fixed: true,type:"ifbox"},
                    {property: "statusStr", label: "状态", width: screen.width * 0.3, fixed: true},
                    {property: "status_mode", label: "配置", width: screen.width * 0.3, fixed: true},
                    {property: "woodcode", label: "配置一", width: screen.width * 0.3, fixed: true},
                    {property: "pcode", label: "配置二", width: screen.width * 0.3, fixed: true}
                ],
                winFrom: "",
                tableData: []
            }

        },
        methods: {
            handleResetQueryParms() {
                //return this.initQueryParams;
                // return { wrkGrpid : this.wrkGrpid,isCalPrice : this.isCalPrice }
            },
            handleCellClick(eventData) {
                if (this.selectMode) {
                    if (eventData.column.property !== 'checked') {
                        this.$set(eventData.row, "checked", !eventData.row.checked);
                    }
                    if(this.isSingleMode) {
                        // 单选
                        for (let item of this.selectedItems) {
                            this.$set(item, "checked", false);
                        }
                        this.selectedItems = [];
                        eventData.row.checked && this.selectedItems.push(eventData.row);
                    } else {
                        // 多选
                        if (eventData.row.checked) {
                            this.selectedItems.push(eventData.row);
                        } else {
                            let itemsTemp = [];
                            for (let item of this.selectedItems) {
                                if (item !== eventData.row) {
                                    itemsTemp.push(item);
                                }
                            }
                            this.selectedItems = itemsTemp;
                        }
                    }
                }
            },
            handleSelectFinish() {
                let selected = [];
                for(let item of this.tableData) {
                    if(item.checked) selected.push(item);
                }

                $lj.sendEvent("orderml_select", {selectedItems: selected});
                $lj.closeWin();
            },
            queryClick(event){
                // alert(JSON.stringify(event))
            },
            handleClickScan(){
                alert(JSON.stringify())
            },
            queryChange(event){
                
            },
            handleLoaded(list){
                if (list) {
                    this.tableData = list;
                }
            },
        },
        computed: {
            title() {
                if (this.selectMode) {
                    return "指令单选择";
                } else {
                    return "计划(指令单)";
                }
            },
            selectMode() {
                return this.winMode === "multi-select" || this.winMode === "single-select";
            },
            isSingleMode() {
                return this.winMode === "single-select";
            }
        }
    }
</script>

<style scoped>

</style>