<template>
  <div class="RCSbox">
    <OperateCard
      v-if="showOpCard"
      class="cleft"
      :icon-left-list="iconLeftList"
      :icon-right-list="iconRightList"
      :form-list="formList"
    />
    <div class="cright">
      <FormSearch
        :form-arr="formArr"
        :form-data="formData"
        @outOperation="outOperation"
        @searchSubmit="searchSubmit"
        @reset="resetForm"
      />
      <div class="tableBox">
        <PublicTable
          ref="table"
          class="table"
          row-key="id"
          :loading="loading"
          :has-index="false"
          :need-select="false"
          :table-data="tableData.slice((pageParams.pageNum-1)*pageParams.pageSize,pageParams.pageNum*pageParams.pageSize)"
          :table-info="tableInfo"
          :table-column="columns"
          :events="events"
          operation-width="150px"
          :btn-button="operations"
          :table-top-button="tableTopButton"
          :operation-fixed="true"
          :is-need-pagination="true"
          :current-page="pageParams.pageNum"
          :page-size="pageParams.pageSize"
          :total="pageParams.total"
          :operated-string="operatedString"
          :is-need-customcolumn="false"
          @sizeChange="handleSizeChange"
          @currentChange="handleCurrentChange"
          @customColumnChange="handleCustomColumnChange"
          @handleTableTopColumnIconClick="handleTableTopColumnIconClick"
          @handleSelectionChange="handleSelectionChange"
        />
      </div>
    </div>
    <HistoryVersion
      ref="historyVersion"
      :data-id="dataId"
      :selected-value="historySelectedValue"
      :visible.sync="historyVisible"
      @handleCancel="historyVisible = false"
    />
    <OperateDialog
      ref="operateDialog"
      :title="dialogTitle"
      :percent-width="60"
      :form-arr="modalFormList"
      :form-data="opDialogData"
      :visible.sync="dialogVisible"
      :loading="opDialogLoading"
      @handleCancel="dialogVisible = false"
      @dialogOK="getOpDialogForm"
    />
  </div>
</template>

<script>
import FormSearch from '../../../components/formSearch/index.vue';
import PublicTable from '../../../components/table/index.vue';
import OperateCard from '../../../components/operateCard/index.vue';
import OperateDialog from '../components/operateDialog.vue';
import HistoryVersion from '../../../components/historyVersion/index.vue';
import mockData from './mockData';
import mockData4 from './mockData4';
import mockData5 from './mockData5';
export default {
  components: {
    FormSearch,
    PublicTable,
    OperateCard,
    OperateDialog,
    HistoryVersion,
  },
  data() {
    return {
      formArr: [
        {
          // input: true,
          type: 'input',
          prop: 'input1',
          span: 6,
          attrs: {
            label: '关键字',
          },
        },
      ],
      // 默认值
      formData: {},
      // 获取列表前是否loading加载
      loading: false,
      // table数据源
      tableData: [],
      // 表格项绑定的属性
      columns: [
        {
          prop: 'code',
          minWidth: '100px',
          align: 'center',
          label: this.$t('field.code'),
        },
        {
          prop: 'name',
          minWidth: '100px',
          align: 'center',
          label: this.$t('field.shortName'),
        },
        {
          prop: 'localName',
          minWidth: '200px',
          align: 'center',
          label: this.$t('field.name'),
        },
        {
          prop: 'quotationUnitName',
          minWidth: '100px',
          align: 'center',
          label: this.$t('field.quotationUnitName'),
        },
        {
          prop: 'pointsDigit',
          minWidth: '100px',
          align: 'center',
          label: this.$t('field.pointsDigit'),
        },
        {
          prop: 'spotNoDecimal',
          minWidth: '100px',
          align: 'center',
          label: this.$t('field.spotNoDecimal'),
        },
        {
          prop: 'fwdNoDecimal',
          minWidth: '100px',
          align: 'center',
          label: this.$t('field.fwdNoDecimal'),
        },
        {
          prop: 'swapNoDecimal',
          minWidth: '100px',
          align: 'center',
          label: this.$t('field.swapNoDecimal'),
        },
        {
          prop: 'volNoDecimal',
          minWidth: '100px',
          align: 'center',
          label: this.$t('field.volNoDecimal'),
        },
      ],
      // 表格行单机双击事件
      events: {
        'row-dblclick': (row) => {
          // 双击表格 行 触发的函数
          // console.log('row-dblclick',row);
        },
        'row-click': (row) => {
          // 单机表格 行 触发的函数
          // console.log("row-click", row);
        },
      },
      // 操作栏自定义按钮
      operations: [
        {
          text: this.$t('button.edit'),
          type: 'text',
          class: 'el-text-color',
          callback: (row) => {
            this.handleAddOrEdit('edit', row);
          },
        },
        {
          text: this.$t('button.delete'),
          type: 'text',
          class: 'el-text-color',
          callback: (row) => {
            this.handleAddOrEdit('edit', row);
          },
        },
        {
          text: this.$t('field.history'),
          type: 'text',
          class: 'el-text-color',
          callback: (row) => {
            this.historyVersionOpen();
          },
        },
      ],
      tableTopButton: [
        {
          text: this.$t('button.add'),
          type: 'primary',
          class: 'el-text-color',
          callback: () => {
            this.handleAddOrEdit('add');
          },
        },
        {
          text: this.$t('button.pairGrp'),
          type: 'primary',
          class: 'el-text-color',
          callback: (value) => {
            this.showOperate();
          },
        },
        {
          text: this.$t('button.export'),
          type: 'primary',
          class: 'el-text-color',
          callback: (value) => {
            this.handletableTopButton(value);
          },
        },
        {
          text: this.$t('button.import'),
          type: 'primary',
          class: 'el-text-color',
          callback: (value) => {
            this.handletableTopButton(value);
          },
        },
      ],
      // 左方操作列图标参数
      // 操作栏自定义按钮
      iconLeftList: [
        {
          text: this.$t('button.add'),
          type: 'el-icon-plus',
          callback: (row) => {
            this.handleAddOrEdit('add', row);
          },
        },
        {
          text: this.$t('button.edit'),
          type: 'el-icon-edit',
          callback: (row) => {
            this.handleAddOrEdit('edit', row);
          },
        },
        {
          text: this.$t('button.delete'),
          type: 'el-icon-delete',
          callback: (row) => {
            this.handleAddOrEdit('edit', row);
          },
        },
      ],
      iconRightList: [
        {
          type: 'el-icon-close',
          callback: () => {
            this.closeOperateCard();
          },
        },
      ],
      // 搜索查询的参数
      pageParams: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      formList: [
        {
          label: 'G7',
          value: 'test',
          chosen: false,
          callback: (index) => {
            this.getNodeInfo(index);
          },
        },
        {
          label: 'G6',
          value: 'test',
          chosen: false,
          callback: (index) => {
            this.getNodeInfo(index);
          },
        },
      ],
      modalFormList: [
        {
          type: 'select',
          prop: 'select1',
          span: 12,
          attrs: {
            label: '货币1',
            needStar: true,
            placeholder: '请选择',
          },
          options: [],
          rules: { required: true, message: '请选择货币' },
        },
        {
          type: 'select',
          prop: 'select2',
          span: 12,
          attrs: {
            label: '货币2',
            needStar: true,
            placeholder: '请选择',
          },
          options: [],
          rules: { required: true, message: '请选择货币' },
        },
        {
          type: 'input',
          prop: 'input1',
          span: 12,
          attrs: {
            label: `${this.$t('field.code')}`,
            needStar: true,
            placeholder: '请填写',
          },
          rules: { required: true, message: '请输入文本' },
        },
        {
          type: 'input',
          prop: 'input2',
          span: 12,
          attrs: {
            label: `${this.$t('field.shortName')}`,
            needStar: true,
            placeholder: '请填写',
          },
          rules: { required: true, message: '请输入文本' },
        },
        {
          type: 'input',
          prop: 'input3',
          span: 12,
          attrs: {
            label: `${this.$t('field.name')}`,
            placeholder: '请填写',
          },
        },
        {
          type: 'inputNumber',
          prop: 'inputNumber1',
          span: 12,
          attrs: {
            label: `${this.$t('field.quotationUnitName')}`,
            needStar: true,
          },
          rules: { required: true, message: '请填写正确的报价单位' },
        },
        {
          type: 'inputNumber',
          prop: 'inputNumber2',
          span: 12,
          attrs: {
            label: `${this.$t('field.pointsDigit')}`,
            needStar: true,
          },
          rules: { required: true, message: '请填写正确的基点位数' },
        },
        {
          type: 'inputNumber',
          prop: 'inputNumber3',
          span: 12,
          attrs: {
            label: `${this.$t('field.spotNoDecimal')}`,
            needStar: true,
          },
          rules: { required: true, message: '请填写正确的即期汇率位数' },
        },
        {
          type: 'inputNumber',
          prop: 'inputNumber4',
          span: 12,
          attrs: {
            label: `${this.$t('field.fwdNoDecimal')}`,
            needStar: true,
          },
          rules: { required: true, message: '请填写正确的远期汇率位数' },
        },
        {
          type: 'inputNumber',
          prop: 'inputNumber5',
          span: 12,
          attrs: {
            label: `${this.$t('field.swapNoDecimal')}`,
            needStar: true,
          },
          rules: { required: true, message: '请填写正确的掉期点位数' },
        },
        {
          type: 'inputNumber',
          prop: 'inputNumber6',
          span: 12,
          attrs: {
            label: `${this.$t('field.volNoDecimal')}`,
            needStar: true,
          },
          rules: { required: true, message: '请填写正确的波动率位数' },
        },
        {
          type: 'inputNumber',
          prop: 'inputNumber7',
          span: 12,
          attrs: {
            label: '掉期率位数',
            needStar: true,
          },
          rules: { required: true, message: '请填写正确的掉期率位数' },
        },
        {
          type: 'inputNumber',
          prop: 'inputNumber8',
          span: 12,
          attrs: {
            label: '%费率位数',
            needStar: true,
          },
          rules: { required: true, message: '请填写正确的%费率位数' },
        },
        {
          type: 'inputNumber',
          prop: 'inputNumber9',
          span: 12,
          attrs: {
            label: 'pips费率位数',
            needStar: true,
          },
          rules: { required: true, message: '请填写正确的pips费率位数' },
        },
        {
          type: 'inputNumber',
          prop: 'inputNumber10',
          span: 12,
          attrs: {
            label: '即期起息天数',
            needStar: true,
          },
          rules: { required: true, message: '请填写正确的pips费率位数' },
        },
        {
          type: 'select',
          prop: 'select3',
          span: 12,
          attrs: {
            label: `${this.$t('field.holidayId')}`,
            needStar: true,
            placeholder: '请选择',
            multiple: true,
          },
          options: [],
          rules: { required: true, message: '请选择节假日' },
        },
      ],
      operatedString: '',
      showOpCard: false,
      historyVisible: false,
      historySelectedValue: [],
      dataId: '', // 历史版本组件查询需要
      dialogTitle: '',
      dialogVisible: false,
      opDialogData: {}, // 编辑框的formData
      opDialogLoading: false, // 操作框保存按钮的loading
    };
  },

  async mounted() {
    this.queryPage();
  },
  methods: {
    searchSubmit(val) {
      console.log('search结果', val);
    },
    resetForm() {
      this.formData = {};
    },
    // 展示左方操作卡片
    showOperate() {
      this.showOpCard = true;
    },
    closeOperateCard() {
      this.showOpCard = false;
    },
    outOperation(val, index, prop, num) {
      console.log(val, index, prop, num);
    },
    // 页面展示条数改变事件-pageSize
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize;
    },
    // 页面切换事件-pageNum
    handleCurrentChange(pageNum) {
      // console.log('');
      this.pageParams.pageNum = pageNum;
    },
    handleAddOrEdit(type, row) {
      // console.log("新增编辑", type, row);
      // 请求节假日的接口,需要处理成lable-value格式
      this.modalFormList[0].options = this.getSelectOptions2(
        this.deepRemoveLeftQuotes(mockData4),
      );
      this.modalFormList[1].options = this.getSelectOptions2(
        this.deepRemoveLeftQuotes(mockData4),
      );
      this.modalFormList[15].options = this.getSelectOptions2(
        this.deepRemoveLeftQuotes(mockData5),
      );
      // console.log("我是formArr", this.modalFormList);
      if (type === 'add') {
        this.dialogTitle = '新增';
      } else if (type === 'edit') {
        this.dialogTitle = '编辑';
        this.opDialogData = {
          select1: row.currencyCode1,
          select2: row.currencyCode2,
          input1: row.code,
          input2: row.name,
          input3: row.localName,
          inputNumber1: row.quotationUnitName,
          inputNumber2: row.pointsDigit,
          inputNumber3: row.spotNoDecimal,
          inputNumber4: row.fwdNoDecimal,
          inputNumber5: row.swapNoDecimal,
          inputNumber6: row.volNoDecimal,
          inputNumber7: row.swapRateNoDecimal,
          inputNumber8: row.percentNoDecimal,
          inputNumber9: row.pipNoDecimal,
          inputNumber10: row.premiumNoDecimal,
          select3: row.holidayIdsStr.split(','),
        };
      }
      this.dialogVisible = true;
    },
    getOpDialogForm(form, type) {
      const params = {
        currencyCode1: form.select1,
        currencyCode2: form.select2,
        code: form.ipnut1,
        name: form.input2,
        localName: form.input3,
        quotationUnitName: form.inputNumber1,
        pointsDigit: form.inputNumber2,
        spotNoDecimal: form.inputNumber3,
        fwdNoDecimal: form.inputNumber4,
        swapNoDecimal: form.inputNumber5,
        volNoDecimal: form.inputNumber6,
        swapRateNoDecimal: form.inputNumber7,
        percentNoDecimal: form.inputNumber8,
        pipNoDecimal: form.inputNumber9,
        premiumNoDecimal: form.inputNumber10,
        holidayIdsStr: form.select3.join(','),
      };
      if (type === 1) {
        console.log('我要新增一条数据', params);
      } else if (type === 2) {
        console.log('我要编辑一条数据', params);
      }
    },
    // 数据列确定修改事件
    handleCustomColumnChange(val) {
      // console.log('数据列确定修改事件',val);
      // this.tableData = mockData2;
    },
    // 导入按钮点击事件
    handleDataImport() {},
    // 导出按钮点击事件
    handleDataExport() {},
    // 点击当前列前的图标事件
    handleTableTopColumnIconClick(val) {
      this.operatedString = '我是计算了之后的字符串';
      console.log('当前列 ', val);
    },
    // 多选事件
    handleSelectionChange(val) {
      console.log('多选事件 ', val);
    },
    // 自定义按钮 点击事件
    handletableTopButton(val) {
      console.log('自定义按钮', val);
    },
    getNodeInfo(index) {
      for (const i in this.formList) {
        this.formList[i].chosen = false;
      }
      this.formList[index].chosen = true;
    },
    historyVersionOpen() {
      // 打开请求版本接口,请求选中的值以及所有的可选值。
      this.dataId = 'pubCurrency_30171999';
      this.getSelectedValue(this.dataId);
    },
    getSelectedValue(dataId) {
      // 请求选中的版本数据
      this.historySelectedValue = [2, 3];
      if (this.historySelectedValue.length < 1) {
        this.$message.warning('该条数据未存储过历史版本数据');
        return;
      }
      this.historyVisible = true;
      this.$refs.historyVersion.onOpen();
    },
    versionChange(value, index) {
      console.log('我是改变的值', value, index);
    },
    getSelectOptions1(arr) {
      return arr.map((item) => ({
        value: item.value,
        label: item.text,
      }));
    },
    getSelectOptions2(arr) {
      return arr.map((item) => ({
        value: item.code,
        label: item.name,
      }));
    },
    async queryPage() {
      // const params = {
      //   modelName: "pubCurrency",
      //   grpType1: "CU",
      //   queryParam: {
      //     grpid: "",
      //     code: "",
      //     pageStart: this.pageParams.pageNum,
      //     pageLimit: this.pageParams.pageSize,
      //   },
      // };
      // const res = await this.$apis.queryPage(params);
      // if (success) {
      //   this.tableData = res.data.result.datals;
      //   this.total = res.data.result.total;
      //   this.$message.success("请求成功");
      // }
      const res = this.deepRemoveLeftQuotes(mockData);
      this.tableData = res.datals;
      this.pageParams.total = Number(res.total);
      this.pageParams.pageSize = Number(res.pageSize);
    },
    // 去除对象数据左方的引用号，处理返回复制的返回数据用
    deepRemoveLeftQuotes(obj) {
      if (typeof obj !== 'object' || obj === null) return obj;

      if (Array.isArray(obj)) {
        return obj.map((item) => this.deepRemoveLeftQuotes(item));
      }

      const newObj = {};
      for (const key in obj) {
        const newKey = key.replace(/^["']|["']$/g, '');
        newObj[newKey] = this.deepRemoveLeftQuotes(obj[key]);
      }

      return newObj;
    },
  },
};
</script>

<style lang="scss" scoped>
.RCSbox {
  width: 100%;
  height: 100%;
  display: flex;
  flex-wrap: nowrap;
  .cright {
    flex: 1;
    width: 800px;
    margin-left: 12px;
    padding: 0 12px;
  }
}
</style>
