<template>
    <div class="toutiaoBatch">
        
        
        <div class="toolbox-automate-rules-create-content_title">
            <h3>账户配置信息</h3>
            <a-button type='primary' @click="goNewConfigInfo" class="goNewConfigInfo">新建配置信息</a-button>
        </div>
        
        <div class="topOptions">
            <div>
                <Treeselect :appId.sync="accountId" :multiple="false"  request="2,4"   ref="treeSelect" style="margin-bottom:8px;width:600px " />
                <!-- <a-select mode="multiple"
                    v-model="accountId"
                    style="width: 600px"
                    placeholder="请选择账户"
                    :maxTagCount='1'
                    allowClear
                    show-search
                    option-filter-prop="children"
                    :filter-option="filterOption"
                    >
                    <a-select-option v-for="item in accountList" :key="item.account_id" :value='item.account_id'>
                    {{ item.auth_name+'_('+(item.project_name||"无所属项目")+')' }}
                    </a-select-option>
                </a-select> -->
            </div>
            <!-- <span class="OptinItem">
                <a-input-search placeholder="模板ID" v-model="strategyId" style="width: 280px"  />
            </span> -->
            <span class="optionBtn">
                <a-button type='primary' @click="search" class="searchBtn">查询</a-button>
            </span>
        </div>
        <div class="tableArea">
            <div class="tableOption">
               
            </div>
            <div class="table">
                <a-table
                    :columns="columns"
                    :data-source="data"
                    bordered
                    :pagination="ipagination"
                    :loading="loading"
                    @change="sort"
                    :scroll="{'x':scrollX}"
                    
                >

                    <span slot="cpaBid" slot-scope="text">
                        {{text/1000}}
                    </span>
                    <span slot="accountId_dictText" slot-scope="text">
                        <!-- <a href="javascript:;" >{{text}}</a> -->
                        {{text || '-'}}
                    </span>
                    <span slot="time" slot-scope="text,record">
                        {{record.startTime}}至{{record.endTime}}
                    </span>
                    <span slot="operator" slot-scope="text,record">
                        <a href="javascript:;"  @click="goPlanList(record)">编辑</a>
                        <a-divider type="vertical" />
                        <a href="javascript:;"  @click="deleteRecord(record)">删除</a>
                    </span>

                </a-table>
            </div>
        </div>
        
       
              
    <!-- </a-spin> -->
    </div>
</template>

<script>
import moment from 'moment';
import { getAction, postAction, downFile, downFilePost,deleteAction } from '@/api/manage';
// import { JeecgListMixin } from '@/mixins/ipagination'
// import editableCell from './editableCell'
import Treeselect from '@/views/modules/Statistics/components/Treeselect.vue'
import qs from 'qs'
let typeValue='预算'


const strategyColumns = [
  
  {
    title: 'ID',
    dataIndex: 'id',
    key: 'id',
    align: 'center',
    scopedSlots: { customRender: 'id' },
    width:110,
    fixed: 'left' ,
  },
  
  {
    title: '操作',
    dataIndex: 'operator',
    key: 'operator',
    align: 'center',
    scopedSlots: { customRender: 'operator' },
    width:150,
    fixed: 'left' ,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime',
    align: 'center',
    scopedSlots: { customRender: 'createTime' },
        
  },
  
  {
    title: '所属账户名称',
    dataIndex: 'accountId_dictText',
    key: 'accountId_dictText',
    align: 'center',
    scopedSlots: { customRender: 'accountId_dictText' }, 
    width:200  
  },
  {
    title: '所属账户ID',
    dataIndex: 'accountId',
    key: 'accountId',
    align: 'center',
    scopedSlots: { customRender: 'accountId' },   
  },

];

const publicColumns = [    
    {
        title: '出价',
        dataIndex: 'cpaBid',
        align: 'center',    
        scopedSlots: { customRender: 'cpaBid' },
        // sorter:() => {}
    },
]

export default {
    name:'strategy-list',
    components: {
        // editableCell,
        Treeselect,
    },
    // mixins: [JeecgListMixin],
    data() {
        return {
            accountId:[],
            accountList:[],
            strategyState:undefined,
            strategyName:'',
            strategyId:undefined,
            accountId:[],
            scrollX:1500,
            ipagination: {
                current: 1,
                pageSize: 10,
                //   pageSizeOptions: ['10', '20', '30'],
                showTotal: (total, range) => {
                    return range[0] + "-" + range[1] + " 共" + total + "条"
                },
                showQuickJumper: true,
                showSizeChanger:true,
                pageSizeOptions: ['10', '30', '50'],
                total: 0,
                onChange: (current, pageSize) => {
                    // 切换分页时的回调，
                    // 当在页面定义change事件时，切记要把此处的事件清除，因为这两个事件重叠了，可能到时候会导致一些莫名的bug
                    this.ipagination.current = current;
                    this.ipagination.pageSize= pageSize;
                }
            },
           
            spinning:false,
            moment,
           
            optionId:'',//input输入的id或关键词
          
           
            selectDisable:true,
            optValue:undefined,//操作设置的值 删除 暂停 开启
            visible: false,//出价里的弹出框

            //主页的表格
            data:[],
            columns:[...strategyColumns,...publicColumns],
            selectedRowKeys:[],
            selectedRows:[],
            loading:false,
            editID:0,

            url: {               
                list:'/ctop/aiKuaishouAdvertiserStrategy/list',
                delete:'/ctop/aiKuaishouAdvertiserStrategy/delete',
            },


            xiugaiData:[],//存分别修改时候的数组
        }
    },
    
    methods: {
        goNewConfigInfo(){
            this.$router.push('/autoLaunch/configLaunchInfo')
        },
        goPlanList(record){
            console.log(record)
            this.$parent.currentStrategyId=record.id;
            this.$router.push(`/autoLaunch/configLaunchInfo?id=${record.id}`)
        },
        deleteRecord(record){
            this.loading=true
            deleteAction(this.url.delete,{
                id:record.id
            }).then(res=>{
                this.loading=false
                if(res.success){
                    this.$message.success(res.message);
                    this.getStrategyList()
                }else{
                    this.$message.success(res.message);
                }
            })
        },
        filterOption(input, option) {
            return (
                option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
            );
        },
        
        //查询按钮事件
        search(){
            this.ipagination.current=1;
            this.getStrategyList()            
        },
        //点击分页
        sort(pagination, filters, sorter, { currentDataSource }){
            console.log(pagination)
            this.ipagination.current=pagination.current;

            this.ipagination.pageSize=pagination.pageSize;
            this.getStrategyList()
        },

        getStrategyList(){
            this.loading = true
            console.log(this.accountId)
            getAction(this.url.list,{
                strategyId:this.strategyId==undefined?null:this.strategyId,
                accountId:this.accountId,
                pageNo:this.ipagination.current,
                pageSize:this.ipagination.pageSize
            }).then((res)=>{
                this.data=[]
                console.log(res)
                this.loading = false
                if(res.success){

                    this.ipagination.total = res.result.total
                    
                    res.result.records.forEach((item)=>{
                        item.key=item.id
                    })

                    this.data = res.result.records;
                    this.selectedRowKeys=[];
                }
            })
        },
      
    },
    mounted() {
       
        this.getStrategyList()
    },
    
    activated() {
        this.getStrategyList()
    },
    beforeDestroy() {

    },
    filters: {
        toPercentage(val) {
            if(val){
                if(typeof val != 'string'){
                    return (val * 100).toFixed(2) + '%'
                }else{
                    return val
                }
            }else if(typeof val =='string'){
                    return val
                }
            else{
                return 0
            }
        },
        //保留两位小数并且变成货币格式
        decimalsHandle(val){

            if(val && typeof val !='string'){
                if(val>=0){
                    val=parseFloat(val).toFixed(2);
                    let numberStr = val.toString()
                    let str = numberStr.split('.')

                    let str0=str[0].split('').reverse();
                    for (let i = 0; i < str0.length; i++) {
                        if ((i + 1) % 4 === 0) {
                            str0.splice(i, 0, ',')
                        }
                    }
                    str0.reverse()
                    let handleResult = ''
                    for (let j = 0; j < str0.length; j++) {
                        handleResult += str0[j]
                    }

                    return handleResult+'.'+str[1]
                }else{
                    val=parseFloat(val).toFixed(2);
                    let numberStr = val.toString()
                    let str = numberStr.split('.')

                    let str0=str[0].substr(1).split('').reverse();
                    for (let i = 0; i < str0.length; i++) {
                        if ((i + 1) % 4 === 0) {
                            str0.splice(i, 0, ',')
                        }
                    }
                    str0.reverse()
                    let handleResult = ''
                    for (let j = 0; j < str0.length; j++) {
                        handleResult += str0[j]
                    }

                    return '-'+handleResult+'.'+str[1]
                }

            }else if(typeof val =='string'){
                return val
            }
            else{
                return 0
            }
        },
        //变成货币格式
        formatCurrency(val){
            if((val||val==0)&& typeof val !='string'){
                let numberStr = val.toString()
                let str = numberStr.split('').reverse()
                for (let i = 0; i < str.length; i++) {
                    if ((i + 1) % 4 === 0) {
                    str.splice(i, 0, ',')
                    }
                }
                str.reverse()
                let handleResult = ''
                for (let j = 0; j < str.length; j++) {
                    handleResult += str[j]
                }
                return handleResult
            }else if(typeof val =='string'){
                    return val
            }
            else{
                return '-'
            }

        },
        
        
    }
}
</script>
<style lang="scss">
    #closeIcon{
         color: #0E74D2;
    }
    .toutiaoBatch{
        .ant-tag .anticon-close {      
            color: #1890ff;        
        }
        .ant-tag .anticon-close:hover{      
            color: rgba(0, 0, 0, 0.85);    
        }

    }
    
    .ant-tooltip-inner,.ant-tooltip,.ant-tooltip-arrow::before{
        background-color: #fff;
        color: #333;
    }
    #batchModal{
        .rules,.totalPrice{
            font-weight: 600;
        }

        .selectePlan{
            font-size: 14px;
            font-weight: 700;
            color: #333;
            padding: 20px 0;
        }
        .totalPrice{
            margin-left: 20px;
        }
        .modifyTable{
            margin-top: 20px;
            width: 100%;
            height: 400px;
            overflow: auto;
            font-weight: 600;
        }
    }
    #copyModal{
        .ant-modal-header{
            text-align: center;
        }
        .copyTip{
            font-size: 12px;
            color: #999;
        }
        .copyName{
            width: 118px;
            display: inline-block;
            text-align: right;
            .copyKeyStart{
                color: red;
            }
        }
        // font-weight: 600;
    }
</style>
<style lang="scss" scoped>
.toutiaoBatch{
    .breadcrumb{
            height: 22px;
            font-size: 12px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #000000;
            line-height: 22px;
        }
    background-color: #fff;
    padding: 10px 20px;
    position: relative;
    width: 100%;
    .OptinItem{
        display: inline-block;
        margin-right: 15px;
        margin-top: 15px;
        .ok{
            margin-right: 15px;
        }
    }
    .OptinItem:first-child{
        margin-right: 10px;
    }
    .topOptions{
        padding: 15px 0;
        .topOpt{
            width: 100%;
            margin-bottom: 10px;
        }
    }
    .optionBtn{
        // display: inline-block;
        float: right;
        // .searchBtn{
            // margin-right: 10px;
        // }
    }
    .tableArea{
        .tableOption{
            padding: 20px 0;
        }
    }
    .toolbox-automate-rules-create-content_title {
        background-color: #fff;
        display: flex;
        flex-flow: row nowrap;
        justify-content: flex-start;
        align-items: center;
        margin-bottom: 28px;
        position: relative;
        h3 {
            font-size: 22px;
            margin: 0;
            font-weight: 400;
            height: 30px;
            line-height: 30px;
        }
        .goNewConfigInfo{
            position: absolute;
            right: 0;
            top: 4px;
        }
    }

}

</style>