<template>
<div class="system-log-list">
  <div class="ytbd-search">
    <el-form :model="searchForm" ref="searchForm" :inline="true" label-width="80px">
      <el-form-item :label="$t('page.system.systemLog.czr')" prop="qmrMc">
        <el-input v-model="searchForm.qmrMc" :placeholder="$t('form.placeholderInput')" clearable
          style="width: 150px" />
      </el-form-item>
      <el-form-item :label="$t('page.system.systemLog.czsj')">
          <el-date-picker v-model="daterange" clearable type="daterange" range-separator="-" :start-placeholder="$t('page.business.study.study.startDate')"
            :end-placeholder="$t('page.business.study.study.endDate')" value-format="yyyy-MM-dd" @change="search" style="width: 200px" />
        </el-form-item>
      <el-form-item prop="jcmc" :label="$t('page.system.systemLog.czlx')">
        <el-select
          v-model="searchForm.jcmc"
          :placeholder="$t('form.placeholderSelect')"
          clearable
          style="width: 150px"
          @change="search"
        >
          <el-option :label="$t('page.business.study.study.sqgd')" :value="$t('page.business.study.study.sqgd')" />
          <el-option :label="$t('page.business.study.study.tygd')" :value="$t('page.business.study.study.tygd')" />
          <el-option :label="$t('page.business.study.study.jjgd')" :value="$t('page.business.study.study.jjgd')" />
          <el-option :label="$t('page.business.study.study.sqjd')" :value="$t('page.business.study.study.sqjd')" />
          <el-option :label="$t('page.business.study.study.tyjd')" :value="$t('page.business.study.study.tyjd')" />
          <el-option :label="$t('page.business.study.study.jjjd')" :value="$t('page.business.study.study.jjjd')" />
          <el-option :label="$t('page.business.study.study.sqjy')" :value="$t('page.business.study.study.sqjy')" />
          <el-option :label="$t('page.business.study.study.tyjy')" :value="$t('page.business.study.study.tyjy')" />
          <el-option :label="$t('page.business.study.study.jjjy')" :value="$t('page.business.study.study.jjjy')" />
          <el-option :label="$t('page.business.study.study.qrgh')" :value="$t('page.business.study.study.qrgh')" />
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('page.system.systemLog.czxq')" prop="jcnr">
        <el-input v-model="searchForm.jcnr" :placeholder="$t('form.placeholderInput')" clearable
          style="width: 200px" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search">{{ $t('page.business.study.study.search') }}</el-button>
        <el-button icon="el-icon-refresh" @click="reset">{{ $t('form.reset') }}</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="ytbd-content">
    <el-table v-loading="loading" :data="list">
      <el-table-column :label="$t('page.system.systemLog.czr')" prop="qmrMc" width="200" />
      <el-table-column :label="$t('page.system.systemLog.czlx')" :prop="$i18n.locale === 'zh_CN'?'jcmc':'jcmcEn'" width="200" />
      <el-table-column :label="$t('page.system.systemLog.czxq')" align="center">
        <template slot-scope="scope">
          <span>{{initNr(scope.row)}}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('page.business.archive.czsj')" align="center" prop="createTime" width="200" />
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageNum" :limit.sync="searchForm.pageSize"
      @pagination="getList" />
  </div>
</div>

</template>

<script>
import { systemLog_list } from "@/api/business/systemLog/systemLog"
export default {
  name: 'Czrz',
  props: {
  },
  computed: {
  },
  data() {
    return {
      daterange:[],
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        qmrMc: '',
        jcmc: '',
        jcnr: '',
        startDate:'',
        endDate:'',
      },
      loading: false,
      total: 0,
      list: [],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    search() {
      this.searchForm.pageNum = 1
      this.getList()
    },
    reset() {
      this.searchForm.qmrMc = ''
      this.searchForm.jcmc = ''
      this.searchForm.jcnr = ''
      this.searchForm.startDate = ''
      this.searchForm.endDate = ''
      
      this.daterange = []
      this.search()
    },
    getList() {
      this.loading = true
      this.searchForm.startDate = this.daterange && this.daterange.length > 0 ? this.daterange[0] : ''
      this.searchForm.endDate = this.daterange && this.daterange.length > 1 ? this.daterange[1] : ''

      systemLog_list(this.searchForm).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    initNr(row){
      if(this.$i18n.locale === 'zh_CN'){
        if(this.isJSON(row.jcnr)){
          let nr = ''
          let list = JSON.parse(row.jcnr)
          _.forEach(list,(o,index)=>{
            nr+=o.name+'：'+o.value+(index===list.length-1?'':'；')
          })
          return nr
        }else{
          return row.jcnr
        }
      }else{
        if(this.isJSON(row.jcnrEn)){
          let nr = ''
          let list = JSON.parse(row.jcnrEn)
          _.forEach(list,(o,index)=>{
            nr+=o.name+'：'+o.value+(index===list.length-1?'':'；')
          })
          return nr
        }else{
          return row.jcnrEn
        }
      }
    },
    isJSON(str) {
      if (typeof str !== 'string') {
        return false;
      }
      
      try {
        const result = JSON.parse(str);
        
        // 可选：确保解析后是对象或数组，而不是简单类型
        const type = Object.prototype.toString.call(result);
        return type === '[object Object]' || type === '[object Array]';
      } catch (e) {
        return false;
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.system-log-list {
  .ytbd-search {
    background: #fff;
    padding: 20px;
    padding-bottom: 0;
    margin-bottom: 10px;

    .right-btn {
      text-align: right
    }
  }

  .ytbd-content {
    padding: 20px;
    background: #fff;

    .add-box {
      margin-bottom: 10px;
    }
  }
}
</style>
