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 FreezeDialog from '../../components/dialog/freeze.vue';
export default {
  name: "frozen-member",
  components: {
    FreezeDialog,
    ImportDialog
  },
  mixins: [authMethods],
  filters: {
    formatDate(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;
    },
    formateTime(val){
      if (!val || val == -1) return "--";
      return formatLongTime(val, 1);
    },
  },
  data() {
    return {
      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();
  },
  watch: {

  },
  methods: {
    loadData(){
      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);
        }
      });
    },
    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){
      doFetchGet(url.removeFrozenWhiteMember,{
        removeType: 1,
        memberId: item.memberId,
      }).then(res=>{
        if(res.data.errorCode) {
          this.$message({
            message: "移出成功",
            type: "success"
          });
          this.$nextTick(_ => {
            this.loadData();
          });
        }
      })
    },
    // 点击删除并冻结
    clickRemoveAndFreeze(item){
      this.freezeDialogOptions= {
        count: 1,
        isBatchFreeze: 0,
        visible: true,
        title: '移出并冻结会员',
        submit:async (options)=>{
          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
              };
            }
          })
        }
      }
    },
    linkDetail(memberId){
      this.$router.push({
        path: "/customerDetail",
        query: { memberId }
      });
    }
  }
};
