<template>
  <div class="app-warp">
    <div class="app-structure">
      <div class="app-search">
        <el-form ref="searchFrom" :model="searchFrom" label-width="60px">
          <el-row>
            <el-col :span="6">
              <el-form-item label="被培训企业名称" prop="NAME" label-width="120px">
                <el-input v-model="searchFrom.NAME" placeholder="请输入被培训企业名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="服务机构名称" prop="CORP_NAME" label-width="120px">
                <el-input v-model="searchFrom.CORP_NAME" placeholder="请输入服务机构名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label-width="10px">
                <el-button v-waves type="primary" icon="el-icon-search" class="ml-10" @click="getQuery">
                  搜索
                </el-button>
                <el-button icon="el-icon-refresh" @click="goKeyReset">
                  重置
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="app-tools">
        <div class="app-function">
          <el-button type="primary" icon="el-icon-plus" @click="fnAdd('','add')">新建账号</el-button>
        </div>
        <div class="app-minbtn">
          <span class="screening">
            <el-dropdown trigger="click">
              <el-button type="text" icon="el-icon-finished"/>
              <el-dropdown-menu slot="dropdown" style="margin-top:3px;">
                <div class="app-checkbox-group">
                  <el-checkbox-group v-model="checkList">
                    <el-checkbox v-for="item in checkboxList" :key="item" :label="item" style="display: block"/>
                  </el-checkbox-group>
                </div>
              </el-dropdown-menu>
            </el-dropdown>
          </span>
          <el-button type="text" icon="el-icon-refresh-left"/>
          <el-button type="text" icon="el-icon-full-screen"/>
        </div>
      </div>
      <div class="app-container">
        <el-table
          v-loading="listLoading"
          ref="multipleTable"
          :data="varList"
          :row-key="getRowKey"
          :header-cell-style="{
            background:'#f5f7fa',
            'font-weight': 'bold',
            'color': '#000'
          }"
          tooltip-effect="dark"
          border
          fit
          highlight-current-row>
          <el-table-column :reserve-selection="true" type="selection" width="55" align="center"/>
          <el-table-column type="index" label="序号" width="50" align="center"/>
          <el-table-column show-overflow-tooltip prop="NAME" label="被培训企业名称"/>
          <el-table-column show-overflow-tooltip prop="SERVICES_NAME" label="服务机构名称"/>
          <el-table-column
            v-if="checkList.includes('行业类型')"
            :show-overflow-tooltip="true"
            prop="INDUSTRY_ALL_NAME"
            label="行业类型"/>
          <el-table-column
            v-if="checkList.includes('主要负责人')"
            :show-overflow-tooltip="true"
            prop="COMMANDER"
            label="主要负责人"/>
          <el-table-column
            v-if="checkList.includes('电话')"
            :show-overflow-tooltip="true"
            prop="COMMANDER_PHONE"
            label="电话"/>
          <el-table-column
            v-if="checkList.includes('状态')"
            :show-overflow-tooltip="true"
            label="状态">
            <template slot-scope="{row}">
              {{ row.in_service?'服务中':'待服务' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="350">
            <template slot-scope="{row}">
              <el-button type="text" @click="fnResetPassword(row.ENTERPRISE_ID)">重置密码</el-button>
<!--              <el-divider direction="vertical"/>-->
<!--              <el-button type="text" @click="openScheduling(row)">查看服务期</el-button>-->
              <el-divider direction="vertical"/>
              <el-button type="text" @click="fnAdd(row.ENTERPRISE_ID,'edit')">编辑</el-button>
              <el-divider direction="vertical"/>
              <el-button type="text" @click="fnAdd(row.ENTERPRISE_ID,'view')">查看</el-button>
              <el-divider direction="vertical"/>
              <el-button type="text" @click="fnChangeRecord(row.ENTERPRISE_ID)">变更记录</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="page-btn-group">
          <div/>
          <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList"/>
        </div>
      </div>
    </div>

    <service-info ref="serviceInfo"/>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
import moment from 'moment'
import ServiceInfo from './serviceInfo'

export default {
  components: { ServiceInfo, Pagination },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      varList: [],
      searchFrom: {
        NAME: '',
        CORP_NAME: ''
      },
      searchFromCollapse: true,
      checkList: ['主要负责人', '行业类型', '电话', '状态'],
      checkboxList: ['主要负责人', '行业类型', '电话', '状态']
    }
  },
  inject: ['setActiveName', 'setType', 'setEnterpriseId'],
  created() {
    this.listLoading = false
    this.getList()
  },
  methods: {
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.$refs.searchFrom.resetFields()
      this.listQuery = {
        page: 1,
        limit: 10
      }
      this.getList()
    },
    getRowKey(row) {
      return row.ENTERPRISE_ID
    },
    getList() {
      requestFN(`/enterprises/list?showCount=${this.listQuery.limit}&currentPage=${this.listQuery.page}`, {
        ...this.searchFrom
      }).then(data => {
        this.varList = data.list
        this.total = data.page.totalResult
        this.listLoading = false
      })
    },
    fnResetPassword(ENTERPRISE_ID) {
      this.$confirm('确认重置密码吗？', '提示', {
        type: 'warning'
      }).then(() => {
        requestFN('/enterprises/initPass', {
          ENTERPRISE_ID
        }).then(data => {
          this.$message({
            type: 'success',
            message: '重置成功!'
          })
          this.goKeyReset()
        })
      }).catch()
    },
    openScheduling(row) {
      this.$refs.serviceInfo.init(row)
    },
    ifState(DEADLINE_START, DEADLINE_END) {
      if (moment(DEADLINE_START).diff(moment(moment().format('YYYY-MM-DD'))) > 0) {
        return '待服务'
      }
      if (moment(DEADLINE_START).diff(moment(moment().format('YYYY-MM-DD'))) <= 0 && moment(DEADLINE_END).diff(moment(moment().format('YYYY-MM-DD'))) >= 0) {
        return '服务中'
      } else {
        return '服务结束'
      }
    },
    fnChangeRecord(ENTERPRISE_ID) {
      this.setActiveName('RecordList')
      this.setEnterpriseId(ENTERPRISE_ID)
    },
    fnAdd(ENTERPRISE_ID, TYPE) {
      this.setActiveName('Add')
      this.setType(TYPE)
      this.setEnterpriseId(ENTERPRISE_ID)
    }
  }
}

</script>
<style>
</style>
