<!-- 成本分析 -->
<template>
    <div class="app-container">
        <el-header>
            <div class="left-panel">
                <el-select v-model="query.mgroup" placeholder="工段" clearable @change="mgroupChange" class="headerSearch">
                    <el-option v-for="item in mgroupOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
                <el-select v-model="query.type" placeholder="查询类型" clearable class="headerSearch" @change="typeCange">
                    <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
                <el-date-picker v-model="query.day" type="date" value-format="YYYY-MM-DD" format="YYYY-MM-DD"
                    placeholder="日" class="headerSearch" v-if="query.type == 0" />
                <!-- 选择班次 -->
                <el-select v-if="query.type == 0" v-model="query.shift" placeholder="班次" clearable class="headerSearch">
                    <el-option v-for="item in optionsShift" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
                <el-date-picker v-model="query.month" type="month" value-format="YYYY-MM" format="YYYY-MM" placeholder="月份"
                    class="headerSearch" v-if="query.type == 1" />
                <el-date-picker v-model="query.year" type="year" value-format="YYYY" format="YYYY" placeholder="年份"
                    class="headerSearch" v-if="query.type == 2" />
                <el-button type="primary" icon="el-icon-search" @click="getTableData"></el-button>
                <el-button type="primary" @click="exportExcel()" :loading="exportLoading">导出xlsx
                </el-button>
                <el-button type="primary" @click="handlePrint">打印
                </el-button>
            </div>
        </el-header>
        <el-card style="margin-top:5px">
            <div ref="print" id="myReport" class="printContainer">
                <table border="1" cellspacing="0" :key="timeStamp" id="myTable" class="myTable">
                    <thead class="myTableHead">
                        <tr>
                            <th :colspan="allCol">{{ tableName }}</th>
                        </tr>
                        <tr>
                            <th rowspan="3">日期</th>
                            <th rowspan="3" v-if="query.type == 0">班次</th>
                            <th rowspan="3" v-if="query.type == 0">班组</th>
                            <th rowspan="3">实际产量（t）</th>
                            <!-- <th rowspan="3">总成本（元）</th> -->
                            <th rowspan="3">单位成本（元/吨）</th>
                            <th :colspan="materialList.length">直接材料</th>
                            <th colspan="10">制造费用</th>
                        </tr>
                        <tr>
                            <th v-if="data1 > 0" :colspan="data1">原材料</th>
                            <th v-if="data3 > 0" :colspan="data3">辅助材料</th>
                            <th v-if="data2 > 0" :colspan="data2">电/水</th>
                            <th>摊销</th>
                            <th>职工薪酬</th>
                            <th>其他费用</th>
                            <th>折旧</th>
                            <th>差旅费</th>
                            <th>装卸费</th>
                            <th>修理费</th>
                            <th>材料出库</th>
                            <th>倒运费用</th>
                            <th>直接人工</th>
                        </tr>
                        <tr>
                            <th v-for="(item, index) in materialList" :key="index">{{ item }}</th>
                            <th>单位成本（元/吨）</th>
                            <th>单位成本（元/吨）</th>
                            <th>单位成本（元/吨）</th>
                            <th>单位成本（元/吨）</th>
                            <th>单位成本（元/吨）</th>
                            <th>单位成本（元/吨）</th>
                            <th>单位成本（元/吨）</th>
                            <th>单位成本（元/吨）</th>
                            <th>单位成本（元/吨）</th>
                            <th>单位成本（元/吨）</th>
                        </tr>
                    </thead>
                    <tr v-for="(item, index) in tableData1" :key="index">
                        <td class="tableCell" v-for="(item1, index1) in item" :key="index1">{{ item1 }}</td>
                    </tr>
                </table>
            </div>
        </el-card>
    </div>
</template>
<script>
export default {
    data() {
        return {
            query: {
                type: 1,
                day: '',
                month: '',
                year: '',
                shift: '',
            },
            allCol: 16,
            data1: 0,//原材料
            data2: 0,//电和水
            data3: 0,//辅助材料
            data4: 0,//助剂
            data5: 0,//包装
            materialList: [],
            mgroupOptions: [],
            tableName: '班组成本计算',
            options: [
                // { id: 0, name: '班统计' },
                { id: 1, name: '日统计' },
                { id: 2, name: '月统计' },
            ],
            tableNmae: '',
            tableData1: [],
            optionsShift: [],
            sourceData: {}
        };
    },
    mounted() {
        var myDate = new Date();
        let month = myDate.getMonth() + 1;
        if (month < 10) {
            month = '0' + month;
        }
        this.query.month = myDate.getFullYear() + '-' + month;
        this.getShift();
        this.getMgroup();

    },
    methods: {
        //获取工段列表
        getMgroup() {
            this.$API.mtm.mgroup.list.req({ page: 0, cate: 'section' }).then(res => {
                res.forEach(item => {
                    if (item.name !== '煤磨' && item.name !== '水泥包装') {
                        this.mgroupOptions.push(item);
                    }
                })
                this.mgroupOptions = this.mgroupOptions.reverse();
                this.query.mgroup = this.mgroupOptions[0].id;
                this.tableName = this.mgroupOptions[0].name;
                this.getTableData();
            })
        },
        //班组列表
        getShift() {
            this.$API.mtm.shift.list.req({ page: 0 }).then(res => {
                this.optionsShift = res;
            })
        },
        mgroupChange(value) {
            if(value){
                let arr = this.mgroupOptions.filter(item => {
                return item.id == value
            })
            let name = arr[0].name;
            if (name == '电石渣') {
                this.tableName = '电石渣干粉核算'
            } else if (name == '原料磨') {
                this.tableName = '生料核算'
            } else if (name == '回转窑') {
                this.tableName = '熟料核算'
            } else if (name == '水泥磨') {
                this.tableName = '水泥核算'
            }
            }
            

        },
        //查询类型
        typeCange(value) {
            if (value == 1) {
                this.tableName = '班组成本计算'
            } else if (value == 1) {
                this.tableName = '日成本计算'
            } else {
                this.tableName = '月成本计算'
            }
        },
        getData() {
            let that = this;
            that.tableData1 = [];
            that.data1 = 0;
            that.data2 = 0;
            that.data3 = 0;
            that.data4 = 0;
            that.data5 = 0;
            let params = {};
            let arr = [];
            params.page = 0;
            params.mgroup = that.query.mgroup;
            if (that.query.type == 0) {//班
                arr = that.query.day.split('-');
                params.year_s = Number(arr[0]);
                params.month_s = Number(arr[1]);
                params.day_s = Number(arr[2]);
                params.type = "day_s"
            } else if (that.query.type == 1) {//日
                arr = that.query.month.split('-');
                params.year_s = Number(arr[0]);
                params.month_s = Number(arr[1]);
                params.type = "day_s"
            } else {//月
                params.year_s = Number(that.query.year);
                params.type = "month_s"
            }
            that.$API.enm.enstat.req(params).then(res => {
                if (res.length > 0) {
                    let materials = res[res.length - 1].imaterial_data;
                    let arr = [];
                    let dataArr = [];
                    materials.forEach(item => {
                        if (item.material_name == "湿电石渣进厂"||item.material_name == "工业水"||item.material_name == "动力电") {
                            // Skip adding item details to arr for "湿电石渣进厂"
                            return;
                        }
                        if (item.material_name === "入磨原煤" || item.material_name === "蒸汽" ) {
                            return;
                        }
                        if (item.material_type == 30 || item.material_type == 20) {//原材料
                            that.data1 = that.data1 + 4;
                        } else if (item.material_type == 40) {//辅助材料
                            that.data3 = that.data3 + 4;
                        }
                        let materialName = item.material_name 
                        
                        if (materialName === "入窑生料") {
                            materialName = "生料";
                        } else if (materialName === "入窑煤粉") {
                            materialName = "入磨原煤";
                        }
                        arr.push(materialName)
                        arr.push('单价（元/吨）')
                        arr.push('总成本（元）')
                        arr.push('单位成本（元/吨）')
                    })
                    materials.forEach(item => {
                        if(item.material_name == "动力电"){ 
                            that.data2 =  that.data2 + 4
                            arr.push(item.material_name)
                            arr.push('单价（元/度）')
                            arr.push('总成本（元）')
                            arr.push('单位成本（元/吨）')
                        }
                    })
                    materials.forEach(item => {
                       if(item.material_name == "工业水"){
                           that.data2 =  that.data2 + 4
                           arr.push(item.material_name)
                           arr.push('单价（元/吨）')
                           arr.push('总成本（元）')
                           arr.push('单位成本（元/吨）')
                       }
                   })
                    that.allCol = arr.length + 16
                    that.materialList = arr;
                    
                    res.forEach(itemData => {
                        let time = '';
                        if (that.query.type == 0) {
                            time = itemData.year_s + '.' + itemData.month_s + '.' + itemData.day_s;
                        } else if (that.query.type == 1) {
                            time = itemData.year_s + '.' + itemData.month_s + '.' + itemData.day_s;
                        } else {
                            time = itemData.year_s + '.' + itemData.month_s;
                        }
                        dataArr.push(time);
                        if (that.query.type == 0) {
                            dataArr.push(itemData.shift);
                            dataArr.push(itemData.team_name);
                        }
                        dataArr.push(itemData.total_production);
                        // dataArr.push(itemData.coal_consume_unit);
                        dataArr.push(itemData.production_cost_unit);
                        let sub1data = itemData.imaterial_data;
                        for (let i = 0; i < sub1data.length; i++) {
                            if (sub1data[i].material_name === '湿电石渣进厂'|| sub1data[i].material_name === '动力电'|| sub1data[i].material_name === '工业水'){
                                continue;
                            } else if (sub1data[i].material_name === '入磨原煤'|| sub1data[i].material_name === '蒸汽'){
                                continue;
                            };
                            
                            dataArr.push((sub1data[i].amount_consume).toFixed(2))
                            dataArr.push((sub1data[i].price_unit).toFixed(2))
                            dataArr.push((sub1data[i].cost).toFixed(2))
                            dataArr.push((sub1data[i].cost_unit).toFixed(2))
                        }

                        for (let i = 0; i < sub1data.length; i++) {
                        if (sub1data[i].material_name == '动力电') {
                            dataArr.push((sub1data[i].amount_consume).toFixed(2));
                            dataArr.push((sub1data[i].price_unit).toFixed(2))
                            dataArr.push((sub1data[i].cost).toFixed(2));
                            dataArr.push((sub1data[i].cost_unit).toFixed(2));
                         }
                        }
                        for (let i = 0; i < sub1data.length; i++) {
                        if (sub1data[i].material_name == '工业水') {
                            dataArr.push((sub1data[i].amount_consume).toFixed(2));
                            dataArr.push((sub1data[i].price_unit).toFixed(2));
                            dataArr.push((sub1data[i].cost).toFixed(2));
                            dataArr.push((sub1data[i].cost_unit).toFixed(2));
                         }
                        }
                        let otherdata = itemData.other_cost_data;
                        for (let j = 0; j < otherdata.length; j++) {
                            dataArr.push((otherdata[j].cost_unit).toFixed(2))
                        }
                        that.tableData1.push(dataArr)
                        dataArr = [];
                        arr = [];

                    })
                }
            })
        },
        getTableData() {
            this.getData();
        },

        handlePrint() {
            this.$PRINT('#myReport');
        },
        exportExcel() {
            this.exportLoading = true;
            this.$XLSX('#myTable', this.tableName)
            this.exportLoading = false;
        },
    }
};
</script>
<style scoped>
.printContainer {
    width: 100%;
    overflow-x: scroll;
}
</style>