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";
export default {
  name: "frozen-member",
  components: {
    ImportDialog
  },
  mixins: [authMethods],
  filters: {
    formateTime(val){
      if (!val || val == -1) return "--";
      return formatLongTime(val, 1);
    },
  },
  data() {
    return {
      dialogImportVisible: false,
      defaultImg,
      searchData: {
        pageSize: 20,
        currentPage: 1,
        frozenType: -1,
        phoneNameCard: '',
        wxOrPos: -1,
        fieldNames:
          "name,nickName,photoUrl,cardNo,crateCardDateString,wxMember,posMember,phoneNumber",
      },
      total: 0,
      list: [],
      multipleList: [],
      formData:{
        frozenType: '',
        remark: ''
      }
    };
  },
  created() {
    this.loadData();
  },
  activated(){
    this.loadData();
  },
  methods: {
    loadData(){
      doFetch(url.getMembersForFrozen, 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);
        }
      });
    },
    clearSelectedAll(){
      this.$refs.multipleTable.clearSelection();
    },
    delSelectedItem(item){
      this.$refs.multipleTable.toggleRowSelection(item,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();
    },
    handleSelectionChange(selectionList){
      this.multipleList = selectionList || [];
    },
    resetSelected(){
      this.$refs.selectedForm.resetFields();
      this.$refs.multipleTable.clearSelection();
    },
    submitForm() {
      if(!this.multipleList.length){
        this.$message({
          message: "请选择要冻结的会员",
          type: "warning"
        });
        return;
      }
      this.$refs.selectedForm.validate(valid => {
        if (valid) {
          this.$confirm("是否冻结所选会员?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            this.frozenMethod();
          });
        } else {
          return false;
        }
      });
      
    },
    frozenMethod() {
      let memberIds = this.multipleList.map((item)=>{
        return item.memberId
      }).join(',')
      const datas = {
        memberIds: memberIds, // 会员id 多个拼接起来, 隔开
        remark: this.formData.remark, // 备注暂留
        isCurrent: 0, // 所有会员是2 当前是1 其他是0
        status: 1, // 1 冻结 0 解冻
        frozenType: this.formData.frozenType, // 101.异常冻结、102.会员主动冻结
      };
      this.loading = true;
      doFetch(url.batchUpdateFrozen, datas)
        .then((res) => {
          if (res.data.errorCode === 0) {
            this.resetSelected();
            this.handleToTaskCenter(res.data.result);
          } else {
            checkFalse(res.data.message);
          }
        })
        .catch((err) => {
          this.loading = false;
          checkFalse(err);
        });
    },
    handleToTaskCenter(id) {
      this.$confirm(
        `请去【系统】-【操作任务】-【任务中心】查看处理结果和执行进度`,
        `任务发起成功`,
        {
          confirmButtonText: "去任务中心",
          cancelButtonText: "取消",
          closeOnClickModal: false,
          customClass: "import-link-confirm-content",
          type: "success"
        }
      ).then(()=>{
        this.loadData();
        window.open(`/gic-web/#/taskDetail/${id}`);
      }).catch(()=>{
        this.loadData();
      });
    },
    batchUpload(){
      this.dialogImportVisible = true;
    },
    linkDetail(memberId) {
      this.$router.push({
        path: "/customerDetail",
        query: { memberId }
      });
    },
  }
};
