<template>
  <div class="app-content">
    <a-form :form="form" :model="searchForm" layout="inline" class="search_form">
      <a-form-item>
        <a-input v-model="searchForm.unitName" placeholder="请输入单位名称" style="width: 200px" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" icon="search" @click="search">搜索</a-button>
      </a-form-item>
    </a-form>
    <a-divider style="height: 1px; background-color: #e8e8e8;" />
    <div class="submit-btn">
      <a-button type="primary" class="search_input" style="margin-right: 10px;" @click="createUnit">添加单位</a-button>
      <a-button type="primary" @click="exportData">导出Excel</a-button>
    </div>
    <a-table :dataSource="tableData" :columns="columns" rowKey="id" :pagination="false" :loading="loading">
      <template slot="stateSwitch" slot-scope="record">
        <a-switch checked-children="正常" un-checked-children="注销" :checked="checkedState(record)" @change="switchChange($event,record)" />
      </template>
      <template slot="option" slot-scope="record">
        <a-button type="link" size="small" @click="recordClick(record,'manager')">管理员设置</a-button>
        <a-button type="link" size="small" @click="recordClick(record,'edit')">修改</a-button>
        <a-button type="link" size="small" @click="recordClick(record,'delete')">删除</a-button>
      </template>
    </a-table>
    <a-pagination v-if="pagination.total > 0" :total="pagination.total" show-size-changer show-quick-jumper v-model="pagination.pageIndex" :page-size="pagination.pageSize" :page-size-options="pagination.pageSizeOptions" @showSizeChange="showSizeChange" @change="change" :showTotal="() => `共 ${pagination.total} 条`" />
    <a-modal v-model="visibleEdit" :title="'创建/修改'" width="1000px" :dialog-style="{ top: '15%' }" :maskClosable="false" destroyOnClose>
      <template slot="footer">
        <a-button type="primary" @click="() => this.$refs.childEdit.submit()">
          提交
        </a-button>
      </template>
      <unit-edit v-model="id" @close="closeWindow" ref="childEdit"></unit-edit>
    </a-modal>
    <a-modal v-model="visibleView" :title="'查看'" width="800px" :dialog-style="{ top: '15%' }" destroyOnClose>
      <template slot="footer">
        <a-button type="primary" @click="() => visibleView = false">
          确定
        </a-button>
      </template>
      <unit-view v-model="id"></unit-view>
    </a-modal>
    <a-modal v-model="visibleManager" :title="'管理员设置'" width="1000px" :footer="null" :dialog-style="{ top: '15%' }" destroyOnClose>
      <manager-setting :unitInfo="unitInfo" @close="closeWindow" ref="childManager"></manager-setting>
    </a-modal>
  </div>
</template>

<script>
import { isEmptyParams, filterExportExcelData, tableColumnsName } from "@/views/utils/common"
import unitView from '@/views/unit/components/unitView'
import unitEdit from '@/views/unit/components/unitEdit'
import managerSetting from '@/views/unit/components/managerSetting'
import UnitTreeSelect from '@/views/components/common/unitTreeSelect'

export default {
  name: 'unitManager',
  components: {
    unitView, unitEdit, UnitTreeSelect, managerSetting
  },
  data () {
    return {
      form: this.$form.createForm(this, { name: 'advanced_search' }),
      searchForm: { unitName: '', treeCode: '', username: '' },
      tableData: [],
      columns: [
        { title: '单位名称', dataIndex: 'unitName', align: 'center' },
        { title: '单位类型', dataIndex: 'unitTypeName', align: 'center' },
        { title: '单位性质', dataIndex: 'natureName', align: 'center' },
        // { title: '上级单位', dataIndex: 'upUnitName', align: 'center' },
        { title: '单位地址', dataIndex: 'unitAddress', align: 'center' },
        { title: '状态', scopedSlots: { customRender: 'stateSwitch' }, align: 'center' },
        // { title: '用户名', dataIndex: 'username' },
        // { title: '密码', dataIndex: 'password' },
        { title: '操作', scopedSlots: { customRender: 'option' }, align: 'center', fixed: 'right', width: 120, },
      ],
      loading: false,
      // 弹窗
      pagination: { pageIndex: 1, pageSize: this.$defaultPageSize, total: 0, pageSizeOptions: this.$defaultPageSizeOptions, },
      loading: false,
      visibleView: false,
      visibleEdit: false,
      visibleManager: false,
      id: null,
      unitInfo: null
    }
  },
  created () {
    this.getListByPage()
  },
  methods: {
    getListByPage () {//加载单位列表
      this.loading = true
      let pars = isEmptyParams(this.searchForm)
      let par = { ...pars, pageIndex: this.pagination.pageIndex, pageSize: this.pagination.pageSize }
      this.$api.unit.getListByPage(par).then(({ data = {} }) => {
        if (data) {
          const { dataList = [], total = 0 } = data
          this.tableData = dataList
          this.pagination.total = total
        } this.loading = false
      }).catch(() => { this.loading = false })
    },
    search () {
      this.pagination.pageIndex = 1
      this.getListByPage()
    },
    reset () {
      this.searchForm.projName = null
      this.searchForm.projNo = null
      this.pagination.pageIndex = 1
      this.getListByPage()
    },
    change () {
      this.getListByPage()
    },
    showSizeChange (current, pageSize) {
      this.pagination.pageIndex = current
      this.pagination.pageSize = pageSize
      this.getListByPage()
    },
    createUnit () {//弹出单位信息表单
      this.id = null
      this.visibleEdit = true
    },
    closeWindow (value) {
      if (value === 'edit') {
        this.visibleEdit = false
        this.getListByPage()
      }
      else
        this.visibleView = false
    },
    checkedState (record) {//单位状态
      return record.unitStateName === "正常" ? true : false
    },
    switchChange (checked, record) {//启用/停用单位开关
      if (checked) {
        record.unitStateName = "正常";
        record.unitState = 1;
      } else {
        record.unitStateName = "注销";
        record.unitState = 2;
      }
      let par = { id: record.id, unitState: record.unitState }
      this.$api.unit.updateUnitState(par).then(({ data = {} }) => {
        if (data) {
          this.$message.success('修改成功！')
        }
      }).catch(() => { })
    },
    recordClick (record, type) {
      if (type === 'view') {
        this.id = record.id
        this.visibleView = true
      } else if (type === 'edit') {
        this.id = record.id
        this.visibleEdit = true
      }
      else if (type === 'manager') {
        this.unitInfo = record
        this.visibleManager = true
      } else if (type === 'delete') {
        this.IsdeleteUnitById(record)
      }
    },
    IsdeleteUnitById (record) {
      let self = this
      this.$confirm({
        title: '单位删除',
        content: '确定要删除该单位？',
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        onOk () {
          self.$api.unit.deleteUnitById({ id: record.id }).then(({ data = {} }) => {
            if (data) {
              self.$message.success('单位删除成功！')
              self.getListByPage()
            }
          })
        },
        onCancel () {
        },
      })
    },
    exportData () {
      this.$ToDoExcel(`单位列表`, tableColumnsName(this.columns), filterExportExcelData(this.columns, this.tableData))
    }
  }
}
</script>