<template>
  <div class="app-container">
    <div style="margin-bottom: 20px">
      <el-select
        v-if="isAdmin"
        v-model="form.accountId"
        class="margin-left input"
        filterable
        clearable
        placeholder="请选择所属账号"
      >
        <el-option
          v-for="item in accountList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-select
        v-model="form.tag"
        class="margin-left input"
        filterable
        clearable
        placeholder="请选择所属模块"
      >
        <el-option
          v-for="item in operationTagList"
          :key="item.desc"
          :label="item.desc"
          :value="item.desc"
        />
      </el-select>
      <el-select
        v-model="form.businessType"
        class="margin-left input"
        filterable
        clearable
        placeholder="请选业务类型"
      >
        <el-option
          v-for="item in operationBusinessTagList"
          :key="item.code"
          :label="item.desc"
          :value="item.code"
        />
      </el-select>
      <el-input
        v-model="form.operName"
        placeholder="操作人"
        class="margin-left input"
      />
      <el-input
        v-model="form.operPhone"
        placeholder="操作手机"
        class="margin-left input"
      />
      <el-date-picker
        v-model="form.startTimeToEndTime"
        type="datetimerange"
        range-separator="至"
        class="margin-left"
        start-placeholder="选择开始时间"
        end-placeholder="选择结束时间"
      />
      <el-button
        class="margin-left"
        type="primary"
        icon="el-icon-search"
        @click="query(1)"
      >
        搜索
      </el-button>
    </div>
    <!-- 主体表格 -->
    <el-table
      v-loading="tableLoading"
      :data="tableData"
      element-loading-text="加载中..."
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" width="80" label="序号">
        <template slot-scope="scope">
          {{
            (pagination.page - 1) * pagination.pageSize + (scope.$index + 1)
          }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="accountName" label="所属账号" />
      <el-table-column align="center" prop="tag" label="操作模块" />
      <el-table-column align="center" prop="businessTypeName" label="业务类型" />
      <!--      <el-table-column align="center" prop="purchaserName" label="操作">
        <template v-slot="{ row }">
          {{ row.operParam | matchNull }}
        </template>
      </el-table-column>-->
      <el-table-column
        v-if="isAdmin"
        align="center"
        prop="purchaserName"
        label="操作人"
      >
        <template v-slot="{ row }">
          {{ row.operName | matchNull }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="isAdmin"
        align="center"
        prop="purchaserName"
        label="操作手机号"
      >
        <template v-slot="{ row }">
          {{ row.operPhone | matchNull }}
        </template>
      </el-table-column>
      <el-table-column label="操作时间">
        <template slot-scope="scope">
          {{ scope.row.operTime }}
        </template>
      </el-table-column>
      <!--分页栏-->
    </el-table>
    <pagination
      :total="pagination.total"
      :page.sync="pagination.page"
      :limit.sync="pagination.pageSize"
      :page-sizes="pagination.pageSizeOptions"
      :layout="pagination.layout"
      @pagination="handleSizeChange"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getList, getOperationBusinessTagList, getOperationTagList } from '@/api/operationLog'
import tableMixins from '@/mixins/tableMixins'
import { parseTime } from '@/utils'
import {
  getAccountList
} from '@/api/accountAuth'
export default {
  components: { Pagination },
  filters: {
    matchNull(str) {
      if (!str) {
        return '-'
      } else {
        return str
      }
    }
  },
  mixins: [tableMixins],
  data() {
    return {
      accountList: [],
      operationTagList: [], // 所属模块枚举
      operationBusinessTagList: [], // 业务类型枚举
      form: {
        accountId: null, // 所属账号
        tag: null, // 操作模块
        businessType: null, // 业务类型
        operName: null, // 操作人
        operPhone: null, // 操作手机
        startTime: null,
        endTime: null,
        startTimeToEndTime: null
      },
      pagination: {
        page: 1,
        current: 1
      }

    }
  },
  computed: {
    isAdmin() {
      return this.$store.getters.authAccountInfo?.accountType == 0
    }
  },
  created() {
    this.selectInit()
  },
  methods: {
    async selectInit() {
      this.enumLoading = true
      const accountAxios = await getAccountList()
      const operationTagAxios = await getOperationTagList()
      const operationBusinessTagAxios = await getOperationBusinessTagList()
      await Promise.all([accountAxios, operationTagAxios, operationBusinessTagAxios]).then(res => {
        const accountAxiosRes = res[0]
        this.accountList = (accountAxiosRes.data || [])
        const operationTagAxiosRes = res[1]
        this.operationTagList = (operationTagAxiosRes.data || [])
        const operationBusinessTagAxiosRes = res[2]
        this.operationBusinessTagList = (operationBusinessTagAxiosRes.data || [])
      }).finally(() => {
        this.enumLoading = false
      })
    },
    initData() {
      this.getTableList()
    },
    // 获取表格数据
    async getTableList() {
      this.pagination.current = this.pagination.page
      this.tableLoading = true
      try {
        const result = await getList(this.getFilterParams())
        this.setDataList(result.data)
      } catch (e) {
        console.log(e)
      } finally {
        this.tableLoading = false
      }
    },
    // 获取筛选信息
    query(type) {
      if (type) {
        this.pagination.page = 1
        this.pagination.current = 1
      }
      this.form.startTime = null
      this.form.endTime = null
      if (this.form.startTimeToEndTime && this.form.startTimeToEndTime.length > 0) {
        this.form.startTime = parseTime(this.form.startTimeToEndTime[0], '{y}-{m}-{d} {h}:{i}:{s}')
        this.form.endTime = parseTime(this.form.startTimeToEndTime[1], '{y}-{m}-{d} {h}:{i}:{s}')
      }
      this.clearPageParams()
      this.getTableList()
    },
    // 增加筛选条件
    transformFilterForm() {
      let startTime = null
      let endTime = null
      if (this.form.startTimeToEndTime && this.form.startTimeToEndTime.length > 0) {
        startTime = parseTime(this.form.startTimeToEndTime[0], '{y}-{m}-{d} {h}:{i}:{s}')
        endTime = parseTime(this.form.startTimeToEndTime[1], '{y}-{m}-{d} {h}:{i}:{s}')
      }
      return {
        ...this.form,
        startTime: startTime,
        endTime: endTime
      }
    }
  }
}
</script>

<style scoped>
.table{
  margin-top: 15px;
}
.flex {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.margin-left {
  margin: 10px 0 0 10px;
}
.search-layout {
  margin-bottom: 20px;
}
.search-layout .filter-item {
  margin-right: 20px;
  margin-top: 10px;
}
.search-layout .filter-item:last-of-type {
  margin-right: 0;
}
.head {
  margin-left: 16px;
  font-weight: 600;
  font-size: 18px;
  color: #333333;
}
.input {
  width: 200px;
  /*margin-right: 15px;*/
}
.m-r {
  margin-right: 15px;
}
</style>
