import Api from '~/service/Api';
import { getTrnNameByInr } from "~/service/business/common";
import moment from 'moment';
import Utils from "~/utils"
import commonFunctions from '~/mixin/commonFunctions.js';

export default {
  mixins: [commonFunctions],
  methods: {
    async handleSearch() {
      const loading = this.loading();
      let param = {
        scenecode: this.model.scenecode,
        isdefault: "1",
        pageNumber: this.pagination.pageIndex,
        pageSize: this.pagination.pageSize,
      };
      let rtnmsg = await Api.post('/Remittance/autosignrule/list', param);
      if (rtnmsg.respCode == SUCCESS) {
        this.stmData.data = [];
        this.stmData.data = rtnmsg.data.list;
        this.pagination.total = rtnmsg.data.total;
      } else {
        this.$notify.error({ title: '错误', message: '服务请求失败!' });
      }
      loading.close();
    },
    async handleEdit(row) {
      this.clearRuleDialogData();
      const loading = this.loading();
      let param = {
        inr: row.inr
      };
      let rtnmsg = await Api.post('/Remittance/autosignrule/query', param);
      if (rtnmsg.respCode == SUCCESS) {
        this.model.signrule = rtnmsg.data;
      } else {
        this.$notify.error({ title: '错误', message: '服务请求失败!' });
      }
      loading.close();
      this.ruleEditdialog = true;
    },
    async addRule() {
      const loading = this.loading();
      let param = {
        scenecode: this.model.signrule.scenecode,
        isdefault: "1",
        rulekey: this.model.signrule.rulekey,//规则字段
        content: this.model.signrule.content,//规则对比内容
        keytyp: this.model.signrule.keytyp,//规则字段类型
        symbols: this.model.signrule.symbols//规则
      };
      let rtnmsg = await Api.post('/Remittance/autosignrule/add', param);
      if (rtnmsg.respCode == SUCCESS) {
        this.closeDialog();
        this.handleSearch();
      } else {
        this.$notify.error({ title: '错误', message: '服务请求失败!' });
      }
      loading.close();
    },
    async editRule() {
      const loading = this.loading();
      let param = {
        inr: this.model.signrule.inr,
        scenecode: this.model.signrule.scenecode,
        isdefault: "1",
        rulekey: this.model.signrule.rulekey,//规则字段
        content: this.model.signrule.content,//规则对比内容
        keytyp: this.model.signrule.keytyp,//规则字段类型
        symbols: this.model.signrule.symbols//规则
      };
      let rtnmsg = await Api.post('/Remittance/autosignrule/edit', param);
      if (rtnmsg.respCode == SUCCESS) {
        this.ruleEditdialog = false;
        this.handleSearch();
      } else {
        this.$notify.error({ title: '错误', message: '服务请求失败!' });
      }
      loading.close();
    },
    // pageSize改变
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.handleSearch();
    },
    // 页码改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagination.pageIndex = val;
      this.handleSearch();
    },

    clearDialogData() {
      Object.keys(this.model.signinfo).forEach(key => (this.model.signinfo[key] = ''));
      this.model.signinfo.pye = {
        ptytyp: '',
        pts: new Pts().data,
      };
    },
    selectableFn(row) {
      return true;
    },

    handleDeleteOne(row) {
      this.$confirm("确定删除选中的数据?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const loading = this.loading();
        let params = {
          inrs: []
        }
        params.inrs.push(row.inr);
        let rtnmsg = await Api.post('/Remittance/autosignrule/delete', params);
        if (rtnmsg.respCode == SUCCESS) {
          this.$notify({ title: '成功', message: '删除成功', type: 'success', duration: 1000 })
          this.handleSearch();
        } else {
          this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
        loading.close();
      });
    },
    handleDelete() {
      this.$confirm("确定删除选中的数据?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const loading = this.loading();
        let params = {
          inrs: []
        }
        params.inrs = this.multipleSelection;
        let rtnmsg = await Api.post('/Remittance/autosignrule/delete', params);
        if (rtnmsg.respCode == SUCCESS) {
          this.$notify({ title: '成功', message: '删除成功', type: 'success', duration: 1000 })
          this.handleSearch();
        } else {
          this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
        loading.close();
      });
    },
    // 表格多选
    handleSelectionChange(val) {
      this.multipleSelection = val.map((item) => {
        return item.inr
      })
      this.multipleSelectionObj = val.map((item) => {
        return item
      })
      this.multipleSelectionRow = val.map((item) => {
        return item
      })[0]
    },
    async handleReset() {
      this.model.recpan = {}
    },

  },
};