<template>
    <el-dialog id="tableBody" title="批次打款" :visible.sync="dialogVisible" width="35%"   center :before-close="handleClose">
        <div style="margin-left:20%">
            <h3>确定要对：</h3>
            <br/>
            <div>
                <el-row :gutter="20" style="margin-left:5%;line-height: 25px;">
                    <el-col :span="16"><div class="grid-content bg-purple">打款批次：{{  this.client.batchNo }}</div></el-col><br/>
                    <el-col :span="16"><div class="grid-content bg-purple">导入数量：{{  this.client.totalCount }}</div></el-col><br/>
                    <el-col :span="16"><div class="grid-content bg-purple">总金额：{{  this.client.totalAmt }}</div></el-col><br/>
                </el-row>
            </div>
            <br/>

            <h3>进行批次打款吗</h3>
            <br/>

            <div slot="footer" style="margin-right:30%">
                <el-button type="info" plain @click="handleClose">取 消</el-button>
                <el-button type="primary" @click="submitMarket" style="margin-left:150px">确 定</el-button>
            </div>
        </div>
    </el-dialog>
</template>

<script>
    export default {
        name: "byhExportMarket",
        props: {
            dialogVisible: {
                type: Boolean
            },
            batchNo: {
                type: String
            },
            totalCount: {
                type: Number
            },
            totalAmt: {
                type: Number
            },
        },
        data () {
            return {
                client: {
                    batchNo:'',
                    totalAmt:'',
                    totalCount:'',
                },
                records: [],
                options: [
                     {
                        value: '选项0',
                        label: '所有选项'
                    }, 
                ],
                oldOptions: [[]],
                 form: {
                    name: '',
                    region: '',
                    date1: '',
                    date2: '',
                    delivery: false,
                    type: [],
                    resource: '',
                    desc: ''
                }
            }
        },
        created() {
            this.client = {
                batchNo: this.batchNo,
                totalAmt: this.totalAmt,
                totalCount: this.totalCount,
            }
        },
        methods: {

            /** 关闭处理 */
            handleClose () {
                this.visible = false
                setTimeout(() => {
                    this.$emit('update:dialogVisible', false)
                }, 100)
            },

            submitMarket() {
                this.$$get('/raise/submit', this.buildParam()).then(res => {

                    if (res.data.code != 200) {
                        this.$message.error(res.data.message);
                        return
                    }
                    this.$message({message: '提交成功', type: 'success'});
                    window.location.reload()
                    this.handleClose();

                })
                .catch(error => {
                    this.$Message.error(error || '系统异常');
                });
            },
                //参数
            buildParam() {
                var return_hash = {
                };

                if (this.client.batchNo != '') {
                    return_hash['batchNo'] = this.client.batchNo
                }
                return return_hash;
            },
            selectAll(val) {
                let allValues = []
                //保留所有值
                for (let item of this.options) {
                    allValues.push(item.value)
                }
                
                // 用来储存上一次的值，可以进行对比
                const oldVal = this.oldOptions.length === 1 ? [] : this.oldOptions[1]

                // 若是全部选择
                if (val.includes('选项0')) this.records = allValues

                // 取消全部选中  上次有 当前没有 表示取消全选
                if (oldVal.includes('选项0') && !val.includes('选项0')) this.records = []

                // 点击非全部选中  需要排除全部选中 以及 当前点击的选项 
                // 新老数据都有全部选中 
                if (oldVal.includes('选项0') && val.includes('选项0')) {
                const index = val.indexOf('选项0')
                val.splice(index, 1) // 排除全选选项
                this.records = val
                }

                //全选未选 但是其他选项全部选上 则全选选上 上次和当前 都没有全选
                if (!oldVal.includes('选项0') && !val.includes('选项0')) {
                console.log(11)
                if (val.length === allValues.length - 1) this.records = ['选项0'].concat(val)
                }

            //储存当前最后的结果 作为下次的老数据 
            this.oldOptions[1] = this.records
            },
            loadOperate() {
                this.$$post('/market/getDesire', {
                    parentId  : 0,
                })
                    .then(res => {
                    if (res.data.code != 200) {
                        this.$message.error(res.data.message);
                        return
                    }

                    var arr_data = res.data.data;
                    //a_l = arr_data.length
                   // console.log(a_l);

                    //arr_data[a_l]['name'] =   'ALL_SELECT';
                    //arr_data[a_l]['desireNo'] =    'ALL';

                    var arr = new Array()
                    var c = new Object();
                    c.value = '选项0';
                    c.label = '所有选项';
                    arr.push(c);
                    
                    arr_data.forEach(e=>{
                        if(e.parentId==0){
                        var a = new Object();
                        a.value = e.desireNo;
                        a.label = e.name;
                        
                        var arr2 = new Array();
                        arr_data.forEach(item=>{
                            if (item.parentId==e.id) {
                            var b = new Object;
                            b.value = item.desireNo;
                            b.label = item.name;
                            
                            arr2.push(b);
                            }
                        });
                        a.children = arr2;
                        arr.push(a)
                        }
                    })
                   
                    this.options  = arr;
                    })
                    .catch(err => {
                    alert("error!")
                    })
            },

        },
         mounted: function () {
            // this.loadOperate();
        },
    }
</script>

<style scoped lang="stylus" >

#tableBody{
    text-align:center;
}
 .el-row {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
  }
</style>