import { doFetch, doFetchGet } from "@/components/axios/api";
import url from "@/components/axios/url";
import {
  checkFalse,
  checkSuccess
} from "../../../../../static/js/checkStatus";
import authMethods from "@/mixins/auth";
import defaultImg from "../../../../../static/img/default.png";
import ImportDialog from "@/components/allCustomers/components/importDialog.vue";
import { formatLongTime } from "@/utils/utils";
import {dateformat} from '@/utils/formatTime';
import FreezeDialog from '../../components/dialog/freeze.vue';
export default {
  name: "frozenMemberWhiteList",
  components: {
    FreezeDialog,
    ImportDialog
  },
  mixins: [authMethods],
  filters: {
    formatLongTimeDate(val, format) {
      if (!val || val == -1) return format == "hms" ? "" : "--";
      let str = formatLongTime(val, 1);
      let arr = str.split(" ");
      if (format == "ymd") {
        return arr[0];
      } else if (format == "hms") {
        return arr[1] || "";
      }
      return str;
    },
    formatDate(val, format) {
      if (!val || val == -1) return format == "hms" ? "" : "--";
    
      if (format == "ymd") {
        return dateformat(new Date(val), 'yyyy-MM-dd');
      } else if (format == "hms") {
        return dateformat(new Date(val), 'hh:mm:ss');
      }
      return dateformat(new Date(val), 'yyyy-MM-dd hh:mm:ss');
    },
    formateTime(val){
      if (!val || val == -1) return "--";
      return dateformat(val, 'yyyy-MM-dd hh:mm:ss');
    },
  },
  data() {
    return {
      load: false,
      freezeDialogOptions: {
        count: 1,
        isBatchFreeze: 0,
        visible: false
      },
      whiteImportList:[
        {
          importType: 17,
          importValue: "移入白名单",
        }
      ],
      dialogImportVisible: false,
      defaultImg,
      searchData: {
        pageSize: 20,
        currentPage: 1,
        cardNo: '',
      },
      total: 0,
      list: [],
      multipleList: [],
      formData:{
        frozenType: '',
        remark: ''
      }
    };
  },
  created() {
    this.loadData();
  },
  activated(){
    this.loadData();
  },
  methods: {
    loadData(){
      this.load = true;
      doFetchGet(url.getFrozenWhiteList, this.searchData).then((res) => {
        let {errorCode,result} = res.data;
        if (errorCode === 0) {
          let {result:list,totalCount} = result;
          this.list = list || [];
          this.total = totalCount || 0;
        } else {
          checkFalse(res.data.message);
        }
      }).finally(()=>this.load = false)
    },
    handleSizeChange(pageSize){
      this.searchData.pageSize = pageSize;
      if((this.searchData.currentPage - 1) * this.searchData.pageSize > this.total){
        this.searchData.currentPage = 1;
      }      
      this.loadData();
    },
    handleCurrentChange(currentPage){
      this.searchData.currentPage = currentPage;
      this.loadData();
    },
    batchUpload(){
      this.dialogImportVisible = true;
    },
    // 移出白名单
    clickRemoveItem(item){
      this.load = true;
      doFetchGet(url.removeFrozenWhiteMember,{
        removeType: 1,
        memberId: item.memberId,
      }).then(res=>{
        if(res.data.errorCode == 0) {
          this.$message({
            message: "移出成功",
            type: "success"
          });
          this.loadData();
        }
      }).finally(()=>this.load = false)
    },
    // 点击删除并冻结
    clickRemoveAndFreeze(item){
      this.freezeDialogOptions= {
        count: 1,
        isBatchFreeze: 0,
        visible: true,
        title: '移出并冻结会员',
        submit:async (options,callBack)=>{
          doFetchGet(url.removeFrozenWhiteMember,{
            removeType: 2,
            status: 1,
            memberId: item.memberId,
            frozenType: options.frozenType,
            remark: options.remark || ""
          }).then((res)=>{
            if (res.data.errorCode === 0){
              checkSuccess();
              this.freezeDialogOptions = {
                visible: false
              };
              this.loadData();
            }
          }).finally(()=>callBack && callBack())
        }
      }
    },
    linkDetail(memberId){
      this.$router.push({
        path: "/customerDetail",
        query: { memberId }
      });
    },
    handleToTaskCenter(id) {
      this.$confirm(
        `请去【系统】-【操作任务】-【任务中心】查看处理结果和执行进度`,
        `任务发起成功`,
        {
          type: "success",
          customClass: "import-link-confirm-content",
          confirmButtonText: "去任务中心",
          cancelButtonText: "取消",
          showClose: false,
        }
      ).then(()=>{
        this.loadData();
        window.open(`/gic-web/#/taskDetail/${id}`);
      }).catch(()=>{
        this.loadData();
      });
    },
  }
};
