<!-- 币别及汇率 -->
<template>
  <div>
    <basic-container v-show="isShow" class="page-crad">
      <avue-crud ref="crud" :option="option" :data="dataList" :before-open="beforeOpen" id="out-table"
        :header-cell-class-name="headerClassName" :page.sync="page" :search.sync="search" @search-change="searchChange"
        @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad"
        :table-loading="loading" @resetColumn="resetColumnTwo('crud', 'option', 'optionBack', 305)"
        @saveColumn="saveColumnTwo('crud', 'option', 'optionBack', 305)" @selection-change="selectionChange"
        :cell-style="cellStyle" @search-criteria-switch="searchCriteriaSwitch">
        <template slot="code" slot-scope="scope">
          <avue-text-ellipsis :text="scope.row.code" :height="30" use-tooltip placement="top">
            <small slot="more">...</small>
          </avue-text-ellipsis>
        </template>
        <template slot="cnName" slot-scope="scope">
          <avue-text-ellipsis :text="scope.row.cnName" :height="30" use-tooltip placement="top">
            <small slot="more">...</small>
          </avue-text-ellipsis>
        </template>
        <template slot="enName" slot-scope="scope">
          <avue-text-ellipsis :text="scope.row.enName" :height="30" use-tooltip placement="top">
            <small slot="more">...</small>
          </avue-text-ellipsis>
        </template>
        <template slot-scope="{ row }" slot="updateUser">
          <span>{{ row.updateUserName }}</span>
        </template>
        <template slot="menuLeft">
          <el-button type="danger" size="small" icon="el-icon-delete" plain @click="handleDelete">删 除
          </el-button>
        </template>
        <template slot-scope="{ row, index }" slot="menu">
          <el-button type="text" size="small" @click.stop="editOpen(row, 1)">
            编辑
          </el-button>
          <!-- <el-button type="text" size="small" @click.stop="editOpen(row, 1)">
            编辑
          </el-button> -->
          <el-button type="text" size="small" @click.stop="rowDel(row, index)">
            删除
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
    <detailPage v-if="!isShow" ref="detail" @goBack="goBack" @copyOrder="copyOrder" :detailData="detailData"></detailPage>
  </div>
</template>

<script>
import detailPage from "./detailsPage";
import { getRateList, deleteDetails } from "@/api/iosBasicData/rateManagement";

export default {
  name: "index",
  components: {
    detailPage
  },
  data() {
    return {
      selectionList: [],
      form: {},
      dataList: [],
      loading: false,
      isShow: true,
      detailData: {},
      search: {},
      page: {
        pageSize: 10,
        currentPage: 1
      },
      option: {},
      optionBack: {
        searchShow: true,
        searchMenuSpan: 6,
        align: "center",
        border: true,
        index: true,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        menuWidth: 120,
        tip: false,
        selection: true,
        searchIcon: true,
        searchIndex: 3,
        calcHeight: 30,
        addBtnText: "新建币别及汇率",
        stripe: true,
        column: [
          {
            label: "编码",
            prop: "code",
            width: "180",
            overHidden: true,
            search: true
          },
          {
            label: "中文名称",
            prop: "cnName",
            width: "180",
            overHidden: true,
            search: true
          },
          {
            label: "英文名称",
            prop: "enName",
            width: "180",
            overHidden: true,
            search: true
          },
          {
            label: "国际三字码",
            prop: "unCode",
            overHidden: true,
            search: true
          },
          {
            label: "货币符号",
            prop: "symbol",
            overHidden: true
          },
          {
            label: "是否本币",
            prop: "isLocal",
            dicData: [{
              label: '非本币',
              value: 0
            }, {
              label: '本币',
              value: 1
            }],
            overHidden: true
          },
          {
            label: "对本币汇率",
            prop: "exrate",
            overHidden: true
          },
          {
            label: "国家编码",
            prop: "cntyCode",
            overHidden: true
          },
          {
            label: "国家名称",
            prop: "cntyName",
            overHidden: true
          },
          {
            label: "状态",
            prop: "status",
            type: "select",
            dicData: [{
              label: '启用',
              value: 0
            }, {
              label: '停用',
              value: 1
            }],
            value: 0,
            overHidden: true
          }
        ]
      }
    };
  },
  async created() {
    this.option.height = window.innerHeight - 210;
    this.option = await this.getColumnData(this.getColumnName(305), this.optionBack);
  },
  activated() {
    if (this.$route.query.check) {
      this.isShow = true;
      setTimeout(() => {
        this.editOpen({ id: this.$route.query.check.billId }, 1);
      }, 100);
    }
  },
  methods: {
    searchCriteriaSwitch(type) {
      if (type) {
        this.option.height = this.option.height - 46;
      } else {
        this.option.height = this.option.height + 46;
      }
      this.$refs.crud.getTableHeight();
    },
    cellStyle() {
      return "padding:0;height:40px;";
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    refreshChange() {
      this.onLoad(this.page, this.search);
    },
    currentChange(val) {
      this.page.currentPage = val;
    },
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      let queryParams = {
        ...params,
        ...this.search,
        size: page.pageSize,
        current: page.currentPage
      };
      getRateList(queryParams)
        .then(res => {
          this.dataList = res.data.data.records ? res.data.data.records : [];
          this.page.total = res.data.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //新增跳转页面
    beforeOpen() {
      this.isShow = false;
    },
    editOpen(row, status) {
      this.detailData = {
        id: row.id,
        status: status
      };
      this.isShow = false;
    },
    rowDel(row, index, done) {
      if (row.status === 1) {
        this.$confirm("确定删除数据?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          deleteDetails(row.id).then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.onLoad(this.page, this.search);
            }
          });
        });
      } else {
        this.$message({
          message: '非禁用状态无法删除',
          type: 'warning'
        });
      }
    },
    goBack() {
      if (this.$route.query.check) {
        this.$router.$avueRouter.closeTag(this.$route.fullPath);
        this.$router.push({
          path: "/salaryManagement/primarySchool/index"
        });
      }
      this.detailData = this.$options.data().detailData;
      this.onLoad(this.page, this.search);
      this.isShow = true;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }

      for (const selection of this.selectionList) {
        if (selection.status == 0) {
          this.$message.warning("选中的数据中有启用数据,启用数据不可删除!");
          return;
        }
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return deleteDetails(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    //自定义列保存
    async saveColumnTwo(ref, option, optionBack, code) {
      /**
       * 已定义全局方法，直接使用，saveColumnData保存列数据方法,参数传值(表格名称，当前表格的option数据)
       * 已定义全局方法，直接使用，getColumnName方法用来获取枚举值，参数根据自己定义的code值获取中文名
       * 一定要执行异步操作，要等接口成功返回，才能执行下一行编码
       */
      const inSave = await this.saveColumnData(this.getColumnName(code), this[option]);
      if (inSave) {
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs[ref].$refs.dialogColumn.columnBox = false;
      }
    },
    //自定义列重置
    async resetColumnTwo(ref, option, optionBack, code) {
      this[option] = this[optionBack];
      const inSave = await this.delColumnData(this.getColumnName(code), this[optionBack]);
      if (inSave) {
        this.$message.success("重置成功");
        this.$refs[ref].$refs.dialogColumn.columnBox = false;
      }
    },
    // 更改表格颜色
    headerClassName(tab) {
      //颜色间隔
      let back = ""
      if (tab.columnIndex >= 0 && tab.column.level === 1) {
        if (tab.columnIndex % 2 === 0) {
          back = "back-one"
        } else if (tab.columnIndex % 2 === 1) {
          back = "back-two"
        }
      }
      return back;
    },
  }
};
</script>

<style lang="scss" scoped>
.page-crad ::v-deep .basic-container__card {
  height: 94.2vh;
}

::v-deep#out-table .back-one {
  background: #ecf5ff !important;
}

::v-deep#out-table .back-two {
  background: #ecf5ff !important;
}

/deep/ .el-col-md-8 {
  width: 24.33333%;
}
</style>
