<template>
  <div class="app-warp">
    <div class="app-structure">
      <div class="app-search">
        <el-form :inline="true" :model="formInline" class="app-search-form">
          <div class="form-item">
            <label>企业名称:</label>
            <el-input v-model="KEYWORDS" placeholder="搜索" class="filter-item"/>
          </div>
          <div class="form-item"/>
          <!--          <div class="form-item">-->
          <!--            <label>审批批人:</label>-->
          <!--            <el-input v-model="formInline.user" placeholder="审批批人"/>-->
          <!--          </div>-->
          <!--          <div v-show="!searchFromCollapse" class="form-item">-->
          <!--            <label>审批批人:</label>-->
          <!--            <el-input v-model="formInline.user" placeholder="审批批人"/>-->
          <!--          </div>-->
          <!--          <div v-show="!searchFromCollapse" class="form-item">-->
          <!--            <label>审批批人:</label>-->
          <!--            <el-input v-model="formInline.user" placeholder="审批批人"/>-->
          <!--          </div>-->
          <!--          <div v-show="!searchFromCollapse" class="form-item">-->
          <!--            <label>审批批人:</label>-->
          <!--            <el-input v-model="formInline.user" placeholder="审批批人"/>-->
          <!--          </div>-->
          <div class="form-item text-right">
            <el-button v-waves type="primary" icon="el-icon-search" @click="getQuery">查询</el-button>
            <el-button class="filter-item" type="primary" icon="el-icon-search" @click="getReset">重置</el-button>
            <!--            <el-button v-if="searchFromCollapse==true" type="text" @click="searchFromCollapse = false">展开 <i class="el-icon-arrow-down"/></el-button>-->
            <!--            <el-button v-else type="text" @click="searchFromCollapse = true">合并 <i class="el-icon-arrow-up"/></el-button>-->
          </div>

        </el-form>
      </div>
      <div class="app-tools">
        <div class="app-function">
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</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="{
            '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 prop="ENTERPRISE" label="企业名称" />
          <el-table-column v-if="checkList.includes('统一社会信用代码')" prop="CODE" label="统一社会信用代码" />
          <el-table-column v-if="checkList.includes('变更次数')" prop="NUMBERCHANGE" label="变更次数" />
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="{row}">
              <el-button type="text" @click="handleEdit(row.ENTERPRISE_ID)">编辑</el-button>
              <span class="app-divider"/>
              <el-button type="text" @click="ChangeNumber(row.ENTERPRISE_ID)">变更次数</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page-btn-group">
        <div/>
        <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
      </div>
    </div>

    <el-dialog :visible.sync="dialogFormEdit" :title="dialogType==='edit'?'修改':'新增'" width="600px">
      <el-form ref="form" :rules="rules" :model="form" label-width="140px" style="width: 500px;">
        <el-form-item label="企业名称" prop="ENTERPRISE">
          <el-input id="ENTERPRISE" ref="ENTERPRISE" v-model="form.ENTERPRISE" maxlength="255" placeholder="这里输入被培训企业名称..." title="被培训企业名称"/>
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="CODE">
          <el-input id="CODE" ref="CODE" v-model="form.CODE" maxlength="255" placeholder="这里输入统一社会信用代码..." title="统一社会信用代码"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormEdit = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormChange" title="被培训企业变更记录" top="80px" width="800px">
      <div v-for="(item, i) in changeList" :key="item.ENTERPRISELOG_ID">
        <h3>第{{ i+1 }}次变更</h3>
        <table class="table-ui">
          <tr>
            <td style="text-align: center" class="tbg" colspan="2">变更前</td>
            <td style="text-align: center" class="tbg" colspan="2">变更后</td>
          </tr>
          <tr>
            <td class="tbg">企业名称:</td>
            <td>{{ item.OLDENTERPRISE }}</td>
            <td class="tbg">企业名称:</td>
            <td>{{ item.NEWENTERPRISE }}</td>
          </tr>
          <tr>
            <td class="tbg">统一社会信用代码</td>
            <td >{{ item.OLDCODE }}</td>
            <td class="tbg">统一社会信用代码</td>
            <td >{{ item.NEWCODE }}</td>
          </tr>
          <tr>
            <td class="tbg">变更人</td>
            <td >{{ item.CREATOR }}</td>
            <td class="tbg">变更时间</td>
            <td >{{ item.CREATTIME }}</td>
          </tr>
        </table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormChange = false">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

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

export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      rules: {
        ENTERPRISE: [{ required: true, message: '企业名称不能为空', trigger: 'blur' }],
        CODE: [{ required: true, message: '统一社会信用代码不能为空', trigger: 'blur' }]
      },
      dialogImageUrl: '',
      dialogFormEdit: false,
      dialogFormChange: false,
      dialogVisible: false,
      disabled: false,
      formInline: {
        user: '',
        region: ''
      },
      ISEDELETE: '0',

      searchFromCollapse: true,
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      KEYWORDS: '',
      varList: [],
      changeList: [],
      pd: [],
      form: {
        ENTERPRISE_ID: '', // 被培训企业id
        ENTERPRISE: '', // 被培训企业名称
        CODE: '', // 统一社会信用代码
        NUMBERCHANGE: '', // 变更次数
        OLDENTERPRISE: '',
        NEWENTERPRISE: '',
        OLDCODE: '',
        NEWCODE: ''
      },
      formLabelWidth: '120px',
      dialogTableVisible: false,
      dialogType: 'add', // 习题总分数
      checkList: ['统一社会信用代码', '变更次数'],
      checkboxList: ['统一社会信用代码', '变更次数']
    }
  },
  created() {
    this.getList()
    this.getDict()
  },
  methods: {
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    // 重置
    getReset() {
      this.KEYWORDS = null
      this.getList()
    },
    getRowKey(row) {
      return row.ENTERPRISE_ID
    },
    // 保存
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.listLoading = true
          requestFN(
            '/enterprise/' + this.dialogType,
            this.form
          ).then((data) => {
            if (data.result == 'success') {
              this.$message.success('保存成功')
              this.listLoading = false
              this.dialogFormEdit = false
              this.getList()
            }
          }).catch((e) => {
            this.listLoading = false
          })
        } else {
          return false
        }
      })
    },
    // 获取数据字典数据
    getDict: function() {
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/enterprise/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          CODE: this.CODE,
          NUMBERCHANGE: this.NUMBERCHANGE
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        console.log(data.varList[0])
        this.total = data.page.totalResult
        this.hasButton()
        this.pd = data.pd
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 变更次数
    ChangeNumber(ID) {
      this.dialogType = 'change'
      requestFN(
        '/enterpriselog/listAll',
        {
          ENTERPRISE_ID: ID
        }
      ).then((data) => {
        if (data.varList.length > 0) {
          this.changeList = data.varList
          this.dialogFormChange = true
        } else {
          this.$message.warning('没有找到相关变更')
        }
      }).catch((e) => {
        this.listLoading = false
      })
    },

    // 修改
    handleEdit(ID) {
      this.getDict()
      this.dialogType = 'edit'
      requestFN(
        '/enterprise/goEdit',
        {
          ENTERPRISE_ID: ID
        }
      ).then((data) => {
        this.form = Object.assign({}, data.pd) // copy obj
      }).catch((e) => {
        this.listLoading = false
      })
      this.dialogFormEdit = true
    },

    // 添加
    handleAdd() {
      this.dialogType = 'add'
      this.resetForm()
      this.getDict()
      this.dialogFormEdit = true
    },
    resetForm() {
      this.form = {
        ENTERPRISE: '', // 被培训企业名称
        CODE: '', // 统一社会信用代码
        ISDELETE: 0,
        OPERATTIME: '',
        OPERATOR: '',
        OLDENTERPRISE: '',
        NEWENTERPRISE: '',
        OLDCODE: '',
        NEWCODE: ''

      }
    }

  }
}
</script>

<style>

</style>
