<template>
  <div class="app-container">

    <el-form :model="queryForm" inline size="small">
      <el-form-item label="公司名称">
        <el-input v-model="queryForm.name" placeholder="公司名称" />
      </el-form-item>
      <el-form-item label="统一社会信用编码">
        <el-input v-model="queryForm.socialCode" placeholder="统一社会信用编码" />
      </el-form-item>
      <el-form-item label="法人姓名">
        <el-input v-model="queryForm.legalPersonName" placeholder="法人姓名" />
      </el-form-item>
      <el-form-item label="联系人">
        <el-input v-model="queryForm.contacts" placeholder="联系人" />
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input v-model="queryForm.contactNumber" placeholder="联系电话" />
      </el-form-item>
    </el-form>
    <el-row class="action-bar-container" type="flex" justify="end">
      <el-button type="primary" size="small" @click.native="fetchData">查询</el-button>
      <el-button-group>
        <el-button v-permission="['category.organization.add']" size="small" @click.native="$refs.dialogForm.open({})">
          添加
        </el-button>
        <el-button v-permission="['category.organization.delete']" size="small" @click.native="deleteByIds(multipleSelection)">
          删除
        </el-button>
      </el-button-group>
    </el-row>
    <el-table
      v-loading="isLoading"
      :data="pageData.records"
      border
      fit
      highlight-current-row
      @selection-change="selectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="ID" prop="id" width="150" />
      <el-table-column label="公司名称" prop="name" width="200">
        <template slot-scope="scope">
          {{ scope.row.name }}
          <el-tooltip v-if="scope.row.remark && scope.row.remark!==''" placement="top">
            <div slot="content">{{ scope.row.remark }}</div>
            <i class="el-icon-info" />
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="统一社会信用编码" prop="socialCode" width="200" />
      <el-table-column label="法人姓名" prop="legalPersonName" width="150" />
      <el-table-column label="联系人" prop="contacts" width="200" />
      <el-table-column label="联系电话" prop="contactNumber" width="200" />
      <el-table-column label="联系地址" prop="contactAddress" width="300" />
      <!--      <el-table-column label="备注说明" prop="remark" min-width="300"/>-->
      <el-table-column label="创建时间" prop="createdTime" :formatter="dateTimeFormatter" width="180" />
      <el-table-column label="操作" width="70" fixed="right">
        <template slot-scope="scope">
          <el-button v-permission="['category.organization.edit']" type="text" @click.native="$refs.dialogForm.open(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination-container"
      background
      layout="prev, pager, next"
      :total="pageData.total"
      :page-size="pageData.pageSize"
      @current-change="paginationChange"
    />
    <dialog-form ref="dialogForm" @ok="fetchData" />
  </div>
</template>

<script>
import { findPage, deleteByIds } from '@/api/category/organization'
import { dateTimeFormatter } from '@/utils/formater'
import DialogForm from './DialogForm'

export default {
  components: { DialogForm },
  data() {
    return {
      queryForm: {},
      isLoading: true,
      pageData: {},
      currentPage: 1,
      multipleSelection: []
    }
  },
  computed: {},
  created() {
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    dateTimeFormatter,
    selectionChange(val) {
      const temp = []
      val.forEach(o => {
        temp.push(o.id)
      })
      this.multipleSelection = temp
    },
    paginationChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    deleteByIds(ids) {
      this.$confirm('确认要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteByIds(ids).then(() => {
          this.fetchData()
        })
      })
    },
    fetchData() {
      this.isLoading = true
      findPage({ ...this.queryForm, current: this.currentPage })
        .then((response) => {
          this.pageData = response
        })
        .finally(() => (this.isLoading = false))
    }
  }
}
</script>
