<!--风险指标选择框-->
<template>
  <div>
    <BasicModal
      v-bind="$attrs"
      @register="register"
      :title="modalTitle"
      width="900px"
      wrapClassName="j-user-select-modal"
      @ok="handleOk"
      destroyOnClose
      @visible-change="visibleChange"
    >
      <a-row>
        <a-col :span="showSelected ? 18 : 24">
          <BasicTable
            :columns="columns"
            :bordered="true"
            :useSearchForm="true"
            :formConfig="formConfig"
            :api="getMetricList"
            :searchInfo="searchInfo"
            :rowSelection="rowSelection"
            :indexColumnProps="indexColumnProps"
            v-bind="getBindValue"
          ></BasicTable>
        </a-col>
        <a-col :span="showSelected ? 6 : 0">
          <BasicTable
            v-bind="selectedTable"
            :dataSource="selectRows"
            :useSearchForm="true"
            :formConfig="{ showActionButtonGroup: false, baseRowStyle: { minHeight: '40px' } }"
          >
            <!--操作栏-->
            <template #action="{ record }">
              <a href="javascript:void(0)" @click="handleDeleteSelected(record)"><Icon icon="ant-design:delete-outlined"></Icon></a>
            </template>
          </BasicTable>
        </a-col>
      </a-row>
    </BasicModal>
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { getMetricList } from '/@/api/common/api';
  import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
  import { useSelectBiz } from '/@/components/Form/src/jeecg/hooks/useSelectBiz';
  import { useAttrs } from '/@/hooks/core/useAttrs';
  import { selectProps } from '/@/components/Form/src/jeecg/props/props';
  import { render } from '/@/utils/common/renderUtils';

  export default defineComponent({
    name: 'MetricmonSelectModal',
    components: {
      //此处需要异步加载BasicTable
      BasicModal,
      BasicTable: createAsyncComponent(() => import('/@/components/Table/src/BasicTable.vue'), {
        loading: true,
      }),
    },
    props: {
      ...selectProps,
      //选择框标题
      modalTitle: {
        type: String,
        default: '指标选择',
      },
    },
    emits: ['register', 'getSelectResult'],
    setup(props, { emit, refs }) {
      //注册弹框
      const [register, { closeModal }] = useModalInner();
      const attrs = useAttrs();
      //表格配置
      const config = {
        canResize: false,
        bordered: true,
        size: 'small',
        rowKey: 'mtrcNo',
      };
      const getBindValue = Object.assign({}, unref(props), unref(attrs), config);
      const [{ rowSelection, visibleChange, indexColumnProps, getSelectResult, handleDeleteSelected, selectRows }] = useSelectBiz(
        getMetricList,
        getBindValue
      );
      const searchInfo = ref(props.params);
      //查询form
      const formConfig = {
        labelCol: {
          span: 8,
        },
        baseColProps: {
          xs: 24,
          sm: 10,
          md: 10,
          lg: 10,
          xl: 10,
          xxl: 10,
        },
        schemas: [
          {
            label: '指标编码',
            field: 'mtrcNo',
            component: 'Input',
            colProps: { span: 10 },
          },
          {
            label: '指标名称',
            field: 'mtrcName',
            component: 'JInput',
            colProps: { span: 10 },
          },
          {
          label: '监测频率',
           field: 'collFreq',
          component: 'JDictSelectTag',
          componentProps: {
            dictCode: 'colletrate',
            stringToNumber: false,
            placeholder: '请选择监测频率',
          },
          },
          {
          label: '采集方式',
          field: 'collMethod',
          component: 'JDictSelectTag',
          componentProps: {
            dictCode: 'colletway',
            stringToNumber: false,
            placeholder: '请选择采集方式',
          },
          },
        ],
      };
      //定义表格列
      const columns = [
        {
          title: '指标编码',
          dataIndex: 'mtrcNo',
          width: 40,
          align: 'left',
        },
        {
          title: '指标名称',
          dataIndex: 'mtrcName',
          width: 140,
        },
        {
          title: '监测频率',
          dataIndex: 'collFreq',
          width: 40,
          customRender: ({ text }) => {
            return render.renderDict(text, 'colletrate');
          },
        },
        {
          title: '采集方式',
          dataIndex: 'collMethod',
          width: 40,
          customRender: ({ text }) => {
            return render.renderDict(text, 'colletway');
          },
        },
      ];
      //已选择的table信息
      const selectedTable = {
        pagination: false,
        showIndexColumn: false,
        scroll: { y: 390 },
        size: 'small',
        canResize: false,
        bordered: true,
        rowKey: 'id',
        columns: [
          {
            title: '指标名称',
            dataIndex: 'mtrcName',
            width: 140,
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            width: 40,
            slots: { customRender: 'action' },
          },
        ],
      };
      /**
       * 确定选择
       */
      function handleOk() {
        getSelectResult((options, values) => {
          //回传选项和已选择的值
          emit('getSelectResult', options, values);
          //关闭弹窗
          closeModal();
        });
      }
      return {
        handleOk,
        getMetricList,
        register,
        visibleChange,
        getBindValue,
        formConfig,
        indexColumnProps,
        columns,
        rowSelection,

        selectedTable,
        selectRows,
        handleDeleteSelected,
      };
    },
  });
</script>
