<template>
  <div class="app-container">
    <el-form label-width="100px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="关键字搜索">
            <el-input v-model="KEYWORDS" placeholder="搜索相关方单位名称、负责人姓名" />
          </el-form-item>
        </el-col>
        <!--        <el-col :span="6">-->
        <!--          <el-form-item label="类型" label-width="50px">-->
        <!--            <el-select v-model="UNITS_TYPE" style="width: 100%;">-->
        <!--              <el-option v-for="item in typeList" :key="item.ID" :label="item.NAME" :value="item.NAME" />-->
        <!--            </el-select>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :span="6">
          <el-form-item label-width="10px">
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
              搜索
            </el-button>
            <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="goKeyReset">
              重置
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <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"
        :selectable="selectable"
        type="selection"
        width="55"
        align="center"/>
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="UNITS_NAME" label="相关方单位名称" show-overflow-tooltip />
      <!--<el-table-column prop="CODE" label="统一社会信用代码" width="200" />-->
      <el-table-column prop="CONTACTS" label="联系人" width="150" show-overflow-tooltip />
      <el-table-column prop="CONTACTS_PHONE" label="联系人电话" width="100" />
      <el-table-column prop="CORP_NAME" label="创建单位" show-overflow-tooltip />
      <el-table-column prop="CREATOR_NAME" label="创建人" show-overflow-tooltip />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="{row}">
          <el-button v-show="edit" :disabled="loginUserId !== row.CREATOR" type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(row.UNITS_ID)">编辑</el-button>
          <el-button v-show="del" :disabled="loginUserId !== row.CREATOR" type="danger" icon="el-icon-delete" size="mini" @click="isTrueDelete(row.UNITS_ID)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <el-button v-show="add" type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
        <el-button v-show="del" type="danger" icon="el-icon-delete" plain @click="isTrueDeleteAll">删除</el-button>
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
  </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 {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      loginUserId: JSON.parse(sessionStorage.getItem('user')).USER_ID,
      listQuery: {
        page: 1,
        limit: 20
      },
      config: config,
      bus_images: [],
      total: 0,
      KEYWORDS: '',
      UNITS_TYPE: '',
      varList: [],
      pd: [],
      areaList: [], // 属地
      form: {
        UNITS_NAME: '', // 相关方单位名称
        CODE: '', // 统一社会信用代码
        COMPANY_AREA: '', // 属地
        ADDRESS: '', // 企事业单位经营地址
        CONTACTS: '', // 主要负责人
        CONTACTS_PHONE: '', // 主要负责人电话
        LR_MOBILE: '', // 法人手机号
        EMPLOYEES: '', // 职工人数(人)
        CREATE_DATE: '', // 成立时间
        REGCAPITAL: '', // 注册资金(万元)
        TOTALASSETS: '', // 资产总额(万元)
        UNITS_TYPE: ''// 类型
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      dialogFormEdit: false,
      dialogType: 'add',
      typeList: [
        { ID: '0', NAME: '常驻' },
        { ID: '1', NAME: '临时' }
      ]
    }
  },
  created() {
    this.getList()
    this.getDict()
    // this.getData()
  },
  methods: {
    getData() {
      requestFN(
        '/units/goEdit',
        {}
      ).then((data) => {
        this.pd = data.pd
        this.bus_images = data.busImgs
      }).catch((e) => {
        this.listLoading = false
      })
    },
    getRowKey(row) {
      return row.UNITS_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.UNITS_TYPE = ''
      this.getQuery()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/units/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          UNITS_TYPE: this.UNITS_TYPE
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
        this.pd = data.pd
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.$parent.activeName = 'Edit'
      this.$parent.UNITS_ID = ''
    },
    selectable(row, index) {
      return row.CREATOR === this.loginUserId
    },
    // 修改
    handleEdit(ID) {
      this.$parent.activeName = 'Edit'
      this.$parent.UNITS_ID = ID
    },
    isTrueDelete(id) {
      requestFN(
        '/personnelmanagement/isTrueDelete',
        {
          UNITS_ID: id
        }
      ).then((data) => {
        if (data.result == 'success' && data.number === 0) {
          this.handleDelete(id)
        } else {
          this.$message({
            message: '相关方下还有人员不能进行删除',
            type: 'success'
          })
        }
      }).catch((e) => {
        this.listLoading = false
      })
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/units/delete',
          {
            UNITS_ID: id
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    isTrueDeleteAll() {
      const _selectData = this.$refs.multipleTable.selection
      const ids = _selectData.map((item, index) => {
        return item.UNITS_ID
      }).join(',')
      requestFN(
        '/personnelmanagement/isTrueDeleteAll',
        {
          UNITS_ID: ids
        }
      ).then((data) => {
        if (data.result == 'success' && data.number === 0) {
          this.batchDel()
        } else {
          this.$message({
            message: '相关方下还有人员不能进行删除',
            type: 'success'
          })
        }
      }).catch((e) => {
        this.listLoading = false
      })
    },
    batchDel() {
      const _selectData = this.$refs.multipleTable.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中要删除的项...',
          type: 'error'
        })
        return false
      }
      const ids = _selectData.map((item, index) => {
        return item.UNITS_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/units/deleteAll',
          {
            DATA_IDS: ids
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 判断按钮权限,用于是否显示按钮
    hasButton: function() {
      var keys = 'units:add,units:del,units:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.unitsfhadminadd		// 新增权限
        this.del = data.unitsfhadmindel		// 删除权限
        this.edit = data.unitsfhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 获取数据字典数据
    getDict: function() {
    },
    resetForm() {
      this.form = {
        UNITS_NAME: '', // 相关方单位名称
        CODE: '', // 统一社会信用代码
        POSSESSION: '', // 属地
        ADDRESS: '', // 企事业单位经营地址
        CONTACTS: '', // 主要负责人
        CONTACTS_PHONE: '', // 主要负责人电话
        LR_MOBILE: '', // 法人手机号
        EMPLOYEES: '', // 职工人数(人)
        CREATE_DATE: '', // 成立时间
        REGCAPITAL: '', // 注册资金(万元)
        TOTALASSETS: '', // 资产总额(万元)
        UNITS_TYPE: ''// 类型
      }
    }
  }
}

</script>