<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: 120px" />
      </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"
          filterable
          @change="search"
        >
          <template v-if="$i18n.locale === 'zh_CN'">
            <el-option v-for="(item,index) in czlxList" :key="index" :label="item" :value="item" />
          </template>
          <template v-else>
            <el-option v-for="(item,index) in czlxListEn" :key="index" :label="item" :value="item" />
          </template>
        </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: 120px" />
      </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-button type="warning" plain icon="el-icon-download" @click="handleExport" v-hasPermi="['business:systemLog:operate:export']">{{$t('form.export')}}</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')">
        <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,systemLog_czlxList } from "@/api/business/systemLog/systemLog"
export default {
  name: 'Czrz',
  props: {
  },
  computed: {
  },
  data() {
    return {
      daterange:[],
      czlxList:[],
      czlxListEn:[],
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        qmrMc: '',
        jcmc: '',
        jcnr: '',
        startDate:'',
        endDate:'',
      },
      loading: false,
      total: 0,
      list: [],
    }
  },
  created() {
    this.getCzlxList()
    this.getList()
  },
  methods: {
    getCzlxList(){
      systemLog_czlxList().then(response => {
        this.czlxList = _.map(response.data,'jcmc')
        this.czlxListEn = _.map(response.data,'jcmcEn')
      })
    },
    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 (row.name?('【'+row.name+'】'): '')+nr+(row.remark?('；'+this.$t('form.remark')+":"+row.remark):'')
        }else{
          return (row.name?('【'+row.name+'】'): '')+(row.jcnr || '')+(row.remark?('；'+this.$t('form.remark')+":"+row.remark):'')
        }
      }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 (row.nameEn?('【'+row.nameEn+'】'): '')+nr+(row.remark?('；'+this.$t('form.remark')+":"+row.remark):'')
        }else{
          return (row.nameEn?('【'+row.nameEn+'】'): '')+(row.jcnrEn || '')+(row.remark?('；'+this.$t('form.remark')+":"+row.remark):'')
        }
      }
    },
    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;
      }
    },
    handleExport(){
      this.saveSimpleLog({name:'',nameEn:'',jcmc:'操作日志导出',jcmcEn:'User Action Log Export'})
      this.searchForm.startDate = this.daterange && this.daterange.length > 0 ? this.daterange[0] : ''
      this.searchForm.endDate = this.daterange && this.daterange.length > 1 ? this.daterange[1] : ''
      this.download('system/business/systemLog/export', this.searchForm, `userActionLog_${new Date().getTime()}.xlsx`)
    }
  }
}
</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>
