<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
                :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="mb-[10px]">
        <div v-show="showSearch">
          <el-card shadow="hover">
            <el-form ref="queryFormRef" :inline="true" :model="queryParams" label-width="100px">
              <el-form-item label="报表类型" prop="type">
                <el-select v-model="queryParams.type" placeholder="全部">
                  <el-option v-for="item in money_statement_type" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
          </el-card>
        </div>

        <el-card class="mt-[10px]" shadow="never">
          <el-table v-loading="loading" :data="reportList">
            <el-table-column
              v-for="{ label, prop, align, minWidth } in headers"
              :key="prop"
              :align="align || 'center'"
              :label="label"
              :min-width="minWidth || '100px'"
              prop="expertName"
            >
              <template #default="{ row }">
                <span v-if="prop == 'type'">{{ moneyStatementType[row.type] }}</span>
                <span v-else-if="prop == 'period'">{{ moneyStatementPeriod[row.period] }}</span>
                <span v-else-if="prop == 'timeRange'">{{ row.scopeStartTime.split(' ')[0] + ' ~ ' + row.scopeEndTime.split(' ')[0] }}</span>
                <span v-else-if="prop == 'nameDate'" class="color-click" @click="gotoDetail(row)">{{ nameFormat(row.nameDate, row.type) || '-'
                  }}</span>
                <span v-else>{{ row[prop] || '-' }}</span>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="page.total > 0" v-model:limit="page.pageSize" v-model:page="page.pageNum"
                      :total="page.total" @pagination="getList" />
        </el-card>
      </div>
    </transition>
  </div>
</template>

<script lang="ts" setup>
import {getTableList} from '@/api/expertMoneyConfig/report'
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { money_statement_type } = toRefs<any>(proxy?.useDict('money_statement_type'));
const { money_statement_period   } = toRefs<any>(proxy?.useDict('money_statement_period'));
const moneyStatementType  = computed(() => {
  return money_statement_type.value.reduce((acc: any, cur: any) => {
    acc[cur.value] = cur.label;
    return acc;
  }, {});
});
const moneyStatementPeriod  = computed(() => {
  return money_statement_period.value.reduce((acc: any, cur: any) => {
    acc[cur.value] = cur.label;
    return acc;
  }, {});
});

const reportList = ref<any[]>([]);
const loading = ref(true);
const showSearch = ref(true);

const nameFormat = (val: string, type: number | string) => {
  if(!val) return ''
  let valArr = val.split('-')
  let json = {
    1: `${valArr[0]}-${valArr[1]}`,
    2: valArr[0]
  }
  return `${json[type]}专家费用报表`
}

const queryFormRef = ref<ElFormInstance>();

interface Header {
  label: string;
  prop: string;
  align?: 'center' | 'left' | 'right';
  minWidth?: string | number;
}

const headers = ref<Header[]>([
  {
    label: '报表编号',
    prop: 'statementSn'
  },
  {
    label: '报表名称',
    prop: 'nameDate'
  },
  {
    label: '报表类型',
    prop: 'type'
  },
  {
    label: '周期',
    prop: 'period'
  },
  {
    label: '时间范围',
    prop: 'timeRange'
  }
]);

const page = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
});
const queryParams = ref<any>({
  type: ''
});


const getList = async () => {
  loading.value = true;
  let params = {
    ...page.value,
    ...queryParams.value
  };
  delete params.total;
  const res = await getTableList(params);
  reportList.value = res.rows;
  loading.value = false;
  page.value.total = res.total;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  page.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
const gotoDetail = (row: any) => {
  proxy?.$router.push({
    path: '/expertmoneyconfig/exportDetail',
    query: {
      id: row.id,
      type: 'detail'
    }
  });
};
onActivated(() => {
  getList();
});
onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
.color-click {
  color: var(--el-color-primary);
  cursor: pointer;
}
</style>
