<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="130px">
                <el-input v-model="searchFrom.NAME" placeholder="请输入机构（企业）名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="类型" prop="TYPE">
                <el-select v-model="searchFrom.TYPE" style="width: 100%;">
                  <el-option value="0" label="机构"/>
                  <el-option value="1" label="企业"/>
                </el-select>
              </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
            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"
            prop="dependency"
            label="属地"/>
          <el-table-column
            prop="EXPIRE"
            label="最大可培训人数"/>
          <el-table-column
            v-if="checkList.includes('类型')"
            :show-overflow-tooltip="true"
            prop="TYPE"
            label="类型">
            <template slot-scope="{row}">
              {{ row.TYPE === '0' ? '机构' : '企业' }}
            </template>
          </el-table-column>
          <el-table-column
            prop="PROBATION"
            label="机构状态">
            <template slot-scope="{row}">
              {{ row.PROBATION === '0' ? '正式' : '试用' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="250">
            <template slot-scope="{row}">
              <el-button type="text" @click="fnAdd(row.CORPINFO_ID,'view')">查看</el-button>
              <el-divider direction="vertical"/>
              <el-button type="text" @click="fnAdd(row.CORPINFO_ID,'edit')">变更记录</el-button>
              <el-divider direction="vertical"/>
              <el-button type="text" @click="renewal(row.CORPINFO_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>
    <el-dialog
      :visible.sync="dialogVisible"
      title="最大使用人数修改"
      width="70%">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="最大使用人数" prop="MAXUSE">
          <el-input
            v-model="form.MAXUSE"
            oninput ="value=value.replace(/[^0-9.]/g,'')"
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item prop="CONTRACT" label="文件">
          <upload-file :file-list.sync="form.CONTRACT" :limit="1" list-type="picture-card" accept=".pdf"/>
        </el-form-item>
        <el-form-item label="备注" prop ="REMAKE">
          <el-input v-model="form.REMAKE" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import { upload } from '@/utils/upload'
import waves from '@/directive/waves' // waves directive
import UploadFile from '@/components/UploadFile'

export default {
  components: { Pagination, UploadFile },
  directives: { waves },
  data() {
    return {
      CORPINFO_ID: '',
      dialogVisible: false,
      form: {},
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      varList: [],
      searchFrom: {
        NAME: '',
        TYPE: '',
        STATE: ''
      },
      searchFromCollapse: true,
      checkList: ['主要负责人', '电话', '属地', '状态', '类型'],
      checkboxList: ['主要负责人', '电话', '属地', '状态', '类型'],
      rules: {
        MAXUSE: [
          { required: true, message: '输入最大培训人数', trigger: 'blur' }
        ],
        CONTRACT: [
          { required: true, message: '请选择文件', trigger: 'change' }
        ]
      }
    }
  },
  inject: ['setActiveName', 'setType', 'setCorpInfoId'],
  created() {
    this.listLoading = false
    this.getList()
  },
  methods: {
    confirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$confirm('该机构最大可培训学员数将会变更为' + this.form.MAXUSE, '是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            const formData = new FormData()
            formData.append('MAXUSE', this.form.MAXUSE)
            formData.append('CONTRACT', this.form.CONTRACT[0].raw)
            formData.append('CORPINFO_ID', this.CORPINFO_ID)
            formData.append('REMAKE', this.form.REMAKE)
            upload('/expireCorpinfo/save', formData).then((res) => {
              if (res.errorMessage) {
                this.$message({
                  message: res.errorMessage,
                  type: 'error'
                })
              } else {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                this.close()
                this.getList()
              }
            })
          }).catch((e) => {
            console.log(e)
          })
        }
      })
    },
    close() {
      this.dialogVisible = false
    },
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.$refs.searchFrom.resetFields()
      this.listQuery = {
        page: 1,
        limit: 10
      }
      this.getList()
    },
    getRowKey(row) {
      return row.CORPINFO_ID
    },
    getList() {
      requestFN(`/expireCorpinfo/list?showCount=${this.listQuery.limit}&currentPage=${this.listQuery.page}`, {
        ...this.searchFrom
      }).then(data => {
        this.varList = data.varList
        this.total = data.page.totalResult
        this.listLoading = false
      })
    },
    renewal(CORPINFO_ID) {
      this.CORPINFO_ID = CORPINFO_ID
      this.form = {}
      this.dialogVisible = true
    },
    fnAdd(CORPINFO_ID, TYPE) {
      this.setActiveName('Add')
      this.setType(TYPE)
      this.setCorpInfoId(CORPINFO_ID)
    }
  }
}

</script>
<style>
</style>
