

import Login from "@/views/login/login.vue";
import {getHtmlUrl,getUrlList,getUrlInfo,getChangeUrlInfo,getDelPage} from "../../../api/readOnlyDisplay";

export default {
    data() {
        return {
            activeName: "mining",
            dialogVisible: false,
            labelPosition: "top",
            formLabelAlign: {

            },
            currencyList: [
                // {
                //     value: "grs",
                //     label: "grs",
                //     // img: require("../assets/images/grs.png"),
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/258.png",

                // },
                // {
                //     value: "mona",
                //     label: "mona",
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/213.png",
                // },
                // {
                //     value: "dgb_skein",
                //     label: "dgb-skein-pool1",
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/109.png",
                // },
                // {
                //     value: "dgb_qubit",
                //     label: "dgb-qubit-pool1",
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/109.png",
                // },
                // {
                //     value: "dgb_odo",
                //     label: "dgb-odocrypt-pool1",
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/109.png",
                // },
                // {
                //     value: "dgb2_odo",
                //     label: "dgb-odocrypt-pool2",
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/109.png",
                // },
                // {
                //     value: "dgb_qubit_a10",
                //     label: "dgb-qubit-pool2",
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/109.png",
                // },
                // {
                //     value: "dgb_skein_a10",
                //     label: "dgb-skein-pool2",
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/109.png",
                // },
                // {
                //     value: "dgb_odo_b20",
                //     label: "dgb-odoscrypt-pool3",
                //     imgUrl: "https://s2.coinmarketcap.com/static/img/coins/64x64/109.png",
                // },

            ],
            Search: "",
            value: "https://www.f2pool.com/mining-user/10f74b7beb73e27e8d442e7958801fda?user_name=lx497681109",
            currency: "mona",
            readOnlyList: [

                // {
                //     remark: "测试备注",
                //     jurisdiction: [
                //         "矿工", "收益", "支付",
                //     ],
                //     account: "afhaifhauhf",
                //     coin: "BTC",
                //     url: "https://www.m2pool.com/mining-user/10f74b7beb73e27e8d442e7958801fda?user_name=lx497681109",
                // },
                // {
                //     remark: "测试备注",
                //     jurisdiction: [
                //         "矿工", "收益",
                //     ],
                //     account: "afhaifddddhauhf",
                //     coin: "BTC",
                //     url: "https://www.m2pool.com/mining-user/10f74b7beb73e27e8d442e7958801fda?user_name=lx497681109",
                // },


            ],
            checkAll: false,
            checkedItems: [],
            isIndeterminate: true,
            selectedIndices: [],
            params: {
                account: "",
                coin: "",
                config: "",
                remark: "",
                lang:"zh",
            },
            listParams:{
                page: 1,
                limit:10,
            },
            checked: "",
            checkList: ["1"],
            miningAccountList: [],
         
            newMiningAccountList:[],
            currentPage:1,
            TotalSize: 0,
            establishLoading:false,
            dialogData:{
                account:["nexa","a21miner"],
                remark:"566",
                config:["1","2"]
            },
            modifyDialogVisible:false,
            dialogCheckList:[],
            modifyParams:{
                key:"",
                remark:"",
                config:"",
            },
            modifyLoading:false,
            jurisdictionList:[
                {
                    value:"1",
                    label:"mining.miner"
                },
                {
                    value:"2",
                    label:"mining.profit"
                },
                {
                    value:"3",
                    label:"mining.payment"
                },
                
              
            ],
            UrlListLoading:false,
            UrlListParams:{
                page:1,
                limit:10,
            },
            dialogTitle:"",
        }
    },
    watch:{
        miningAccountList: {
            handler(val){
                this.newMiningAccountList= this.miningAccountList.map(item => ({
                    value: item.coin,
                    img: item.img,
                    label: item.title,
                    children: item.children.map(child => ({
                      value: child.account,
                      label: child.account,
                      id: child.id
                    }))
                  }));

            },
            deep: true,
            immediate: true,
        
           
        }
    },
    mounted() {

     
        
        let miningAccountList = localStorage.getItem("miningAccountList")
        this.miningAccountList = JSON.parse(miningAccountList)


        this.currencyList = JSON.parse(localStorage.getItem(`currencyList`))
        window.addEventListener("setItem", () => {
            this.currencyList = JSON.parse(localStorage.getItem("currencyList"))
            let miningAccountList = localStorage.getItem("miningAccountList")
            this.miningAccountList = JSON.parse(miningAccountList)
        });
     

        this.newMiningAccountList= this.miningAccountList.map(item => ({
            value: item.coin,
            img: item.img,
            label: item.title,
            children: item.children.map(child => ({
              value: child.account,
              label: child.account,
              id: child.id,
              img: item.img,
            }))
          }));

   
          
     
          this.fetchUrlList(this.UrlListParams)
          this.registerRecoveryMethod('fetchUrlList', this.UrlListParams);
    },
    methods: {
        async fetchHtmlUrl(params){
            // this.establishLoading = true
            this.setLoading('establishLoading', true);
            const data = await getHtmlUrl(params)
            
            if (data && data.code == 200) {
                this.fetchUrlList(this.UrlListParams)
                this.dialogVisible=false
            }

            this.setLoading('establishLoading', false);
        },
        async fetchUrlList(params){
            // this.UrlListLoading = true
            this.setLoading('UrlListLoading', true);
            const data = await getUrlList(params)
            console.log(data,666    );
            
            this.readOnlyList = data.rows

            this.TotalSize = data.total

            this.setLoading('UrlListLoading', false);
        },
        async fetchUrlInfo(params){
            const data = await getUrlInfo(params)
            console.log(data);
            if (data && data.code == 200) {
                this.dialogData = data.data
                this.modifyDialogVisible = true
                this.dialogTitle = `${this.dialogData.label}/${this.dialogData.account}`
                
            }
          
           
            
        },
        async fetchChangeUrlInfo(params){
            // this.modifyLoading=true
            this.setLoading('modifyLoading', true);
            const data = await getChangeUrlInfo(params)
            console.log(data);
            if (data && data.code == 200) {
                this.fetchUrlList(this.UrlListParams)
                this.modifyDialogVisible=false
                console.log("修改成功");
                
            }
            this.setLoading('modifyLoading', false);
            
        },
        async fetchDelete(params){
            const data = await getDelPage(params)
            console.log(data);
            if (data && data.code == 200) {
                this.checkedItems =[]
                this.fetchUrlList(this.UrlListParams)
            }
        },


        addTo() {
            this.dialogVisible = true


        },
        handleChange(selectedOptions){
         
            // const allSelectedNodes = this.$refs.cascader.getCheckedNodes(true);
            // console.log(allSelectedNodes,"sfkospfko ");
            this.params.account=selectedOptions[1]
            this.params.coin = selectedOptions[0]



        },
        confirmAddition() {
            if (!this.params.account || !this.params.coin) {
                this.$message({
                    showClose: true,
                    message: this.$t(`personal.accountSelection`),
                    type: 'error'
                });
                return
            }
            console.log(this.checkList.length,6666);
            
            if (this.checkList.length < 1) {
                this.$message({
                    showClose: true,
                    message: this.$t(`personal.selectPermissions`),
                    type: 'error'
                });
                return
            }

            this.params.config = this.checkList.join(",")
            this.params.lang = this.$i18n.locale
            this.fetchHtmlUrl(this.params)
            // this.readOnlyList.push(

            //     {
            //         remarks:this.params.remarks,
            //         jurisdiction:this.checkList,
            //         account:this.params.account,
            //         currency:this.params.miningPool,
            //         link:"https://www.m2pool.com/mining-user/10f74b7beb73e27e8d442e7958801fda?user_name=lx497681109",
            //     },

            // )
            // this.dialogVisible=false
            // this.checkedItems=[]
            // this.selectedIndices = [];
            // this.handleCheckAllChange()
        },
        modifyInfo(){
            
            if (this.dialogData.config.length < 1) {
                this.$message({
                    showClose: true,
                    message: this.$t(`personal.selectPermissions`),
                    type: 'error'
                });
                return
            }

            this.modifyParams.remark = this.dialogData.remark
            this.modifyParams.config = this.dialogData.config.join(",")

            this.fetchChangeUrlInfo(this.modifyParams)
        },
        handleClose() {
            for (const key in this.params) {
                this.params[key] = ""
            }

            this.checkList = ["1"]
        },

        handleCheckAllChange(val) {
            console.log(this.checkAll,val,6565);

            if (val) {
                // this.checkedItems = this.readOnlyList.map(item => item.account);
                this.checkedItems = this.readOnlyList.map(item => item.key);
            } else {
                this.checkedItems = []

            }
            this.isIndeterminate = false;



            // if (val) {
            //     this.checkedItems = this.readOnlyList.map(() => true);
            //     this.selectedIndices = this.readOnlyList.map((_, index) => index); 
            //   } else {
            //     this.checkedItems = this.readOnlyList.map(() => false);
            //     this.selectedIndices = []; 

            //   }
            //   this.isIndeterminate = false;
        },
        handleSingleCheckChange(value) {
            console.log(value,"value");
            
            let checkedCount = value.length;
            this.checkAll = checkedCount === this.readOnlyList.length;
            this.isIndeterminate = checkedCount > 0 && checkedCount < this.readOnlyList.length;

            // if ( (this.checkedItems.every((item) => item)) && (this.checkedItems.length == this.readOnlyList.length)) {
            //     this.checkAll=true
            // }else{
            //     this.checkAll=false
            // }
            // if (this.checkedItems[index]) {
            //     this.selectedIndices.push(index);
            //   } else {
            //     this.selectedIndices = this.selectedIndices.filter(i => i!== index);
            //   }


        },
        deleteSelected() {

            console.log( this.checkedItems,"积分积分就");

             this.fetchDelete({key:this.checkedItems.join(",")})
            
            // 从选中索引的最大值开始删除，以避免删除后索引的变化影响后续删除
            // this.selectedIndices.sort((a, b) => b - a);
            // this.selectedIndices.forEach(index => {
            //     this.readOnlyList.splice(index, 1);
            // });
            // 清空选中索引数组
            // this.selectedIndices = [];
            // this.handleCheckAllChange()



        },
        async handelCopy(id) {
         
            var d = document.getElementById(id) //获取需要复制的元素

         
            
            d.select() //选中
            // document.execCommand("copy") //直接复制

            try {
                await navigator.clipboard.writeText(d.value);
                this.$message({
                    showClose: true,
                    message: this.$t(`personal.copySuccessful`),
                    type: 'success'
                });
            } catch (err) {
                console.log(err);
                this.$message({
                    showClose: true,
                    message: this.$t(`personal.copyFailed`),
                    type: 'error'
                });
            }
        },
        handelSetUp(item) {
          
            this.modifyDialogVisible = true
            this.fetchUrlInfo({key:item.key})
            this.modifyParams.key= item.key
        },
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.UrlListParams.limit = val
            this.UrlListParams.page = 1
            this.fetchUrlList(this.UrlListParams)
            this.currentPage = 1
          },
          handleCurrentChange(val) {
            console.log(`当前页: ${val}`);  
            this.UrlListParams.page = val
            this.fetchUrlList(this.UrlListParams)
          },
          handelJurisdiction(subItem){
            let obj=  this.jurisdictionList.find(item=>subItem == item.value)
       
            try{
                if (obj.value) {
                    return obj.label
                }else{
                    return ""
                }
            }catch{
                console.log(111);
                
            }
           
         
            
          },
          clickCopy(item){

                // 创建临时输入框
                var tempInput = document.createElement('input');
                tempInput.value = item.url;
                document.body.appendChild(tempInput);
    
                try {
                    tempInput.select();
                    const isCopySuccessful = document.execCommand('copy');
                    if (isCopySuccessful) {
                        this.$message({
                            showClose: true,
                            message: this.$t(`personal.copySuccessful`),
                            type: 'success',
                      
                        
                        });
                    } else {
                        this.$message({
                            showClose: true,
                            message: this.$t(`personal.copyFailed`),
                            type: 'success',
                      
                        
                        });
                    }
                  } catch (error) {
                    this.$message({
                        showClose: true,
                        message: this.$t(`personal.copyFailed`),
                        type: 'success',
                  
                    
                    });
                  } finally {
                    if (document.body.contains(tempInput)) {
                        document.body.removeChild(tempInput);
                      } else {
                        console.log('临时输入框不是 body 的子节点，无法移除。');
                      }
                  }



            // navigator.clipboard.writeText(value).then(() => {
            //     this.$message({
            //         showClose: true,
            //         message: this.$t(`personal.copySuccessful`),
            //         type: 'success',
                
            //     });
            //   }).catch(err => {
            //     this.$message({
            //         showClose: true,
            //         message: this.$t(`personal.copyFailed`),
            //         type: 'error',
                   
            //     });
            //   });
        }
    }
}