<template>
  <div class="app-container archives-container">
    <div class="container-main" style="justify-content: flex-start;">
      <CategoryTree ref="categoryTree" @nodeClick="handleNodeClick" />
      <div v-if="currentCategory && currentCategory.isType !== 1" class="elect-cont-right">
        <div class="connection-header collect-header">
          <h4 class="is-anjuan">{{ currentCategory && currentCategory.cnName }} </h4>
          <div class="head-search">
            <date-range-picker v-model="blurryTime" class="date-item" />
            <rrOperation />
          </div>
          <crudOperation>
            <template v-slot:right>
              <el-button size="mini" @click="doLoalHitch">
                <i class="iconfont icon-bendiguajie" />
                本地挂接
              </el-button>
              <el-button :loading="crud.downloadLoading" size="mini" :disabled="crud.selections.length === 0" @click="doExport(crud.selections)">
                <i class="iconfont icon-daochu" />
                导出
              </el-button>
            </template>
          </crudOperation>
        </div>
        <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler" @row-dblclick="handleDetail">
          <el-table-column type="selection" align="center" width="55" />
          <el-table-column prop="code" label="编号" width="300" />
          <el-table-column prop="create_by" label="操作人" width="100" />
          <el-table-column prop="create_time" label="操作时间">
            <template slot-scope="scope">
              <div>{{ scope.row.create_time | parseTime }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="type" label="重复检验方式" />
          <el-table-column prop="filed" label="挂接字段" />
          <el-table-column prop="total" label="挂接结果" width="240">
            <template slot-scope="scope">
              <div>共 {{ scope.row.total }} 条原文，成功<span class="success-status"> {{ scope.row.success }} </span>条，失败<span class="error-status"> {{ scope.row.error }} </span>条</div>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <pagination v-if="crud.data.length !== 0" />
      </div>
    </div>
    <LocalForm ref="localForm" :current-category="currentCategory" />
    <HitchDetail ref="mDetail" />
  </div>
</template>

<script>
// import crudConnection from '@/api/system/role'
import crudRoles from '@/api/system/role'
import CRUD, { presenter, header, crud } from '@crud/crud'
import DateRangePicker from '@/components/DateRangePicker'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import CategoryTree from '@/views/components/categoryTree'
import LocalForm from './module/form'
import HitchDetail from './module/detail'
// import { exportFile } from '@/utils/index'
// import qs from 'qs'
import { mapGetters } from 'vuex'

import tableData from './table.json'

export default {
  name: 'BatchConnection',
  components: { CategoryTree, LocalForm, HitchDetail, DateRangePicker, rrOperation, crudOperation, pagination },
  cruds() {
    return [
      CRUD({
        title: '批量挂接', url: 'api/role/initRoleList',
        crudMethod: { ...crudRoles },
        optShow: {
          add: false,
          edit: false,
          del: false,
          reset: true,
          download: false,
          group: false
        }
      })
    ]
  },
  mixins: [presenter(), header(), crud()],
  props: {
  },
  data() {
    return {
      currentCategory: null,
      blurryTime: null,
      formVisible: false
    }
  },
  computed: {
    ...mapGetters([
      'baseApi'
    ])
  },
  created() {
  },
  mounted() {
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      if (this.blurryTime) {
        this.crud.query.startTime = this.blurryTime[0]
        this.crud.query.endTime = this.blurryTime[1]
      }
    },
    [CRUD.HOOK.afterRefresh]() {
      this.crud.data = tableData
    },
    handleNodeClick(data) {
      this.currentCategory = data
    },
    doLoalHitch() {
      this.$nextTick(() => {
        this.$refs.localForm.localHitchVisible = true
      })
    },
    handleDetail() {
      this.$refs.mDetail.hitchDetialVisible = true
    },
    doExport(data) {
      crud.downloadLoading = true
      this.$confirm('此操作将导出所选数据' + '<span>你是否还要继续？</span>', '提示', {
        confirmButtonText: '继续',
        cancelButtonText: '取消',
        type: 'warning',
        dangerouslyUseHTMLString: true
      }).then(() => {
        const ids = []
        data.forEach(val => {
          ids.push(val.id)
        })
        // const params = {
        //   'roleIds': ids
        // }
        // exportFile(this.baseApi + '/api/role/exportRole?' + qs.stringify(params, { indices: false }))
      }).catch(() => {
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/collect-reorganizi.scss";
</style>
