<template>
  <div class="org-management">
    <div class="part-box-head">
      <div class="part-box-head-left"><h1>机构管理</h1></div>
    </div>
    <div class="part-filter">
      <div class="part-filter-form">
        <el-form :model="form" inline>
          <el-form-item label="中心代码">
            <el-input v-model.trim="form.code"></el-input>
          </el-form-item>
          <el-form-item label="中心名称">
            <el-input v-model.trim="form.name"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <StateSelect v-model="form.enableState"></StateSelect>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleCurrentChange(0)"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
        <div class="part-filter-form-action">
          <el-button type="primary" icon="icon icon-add" @click="add"
            >新增</el-button
          >
          <!-- <el-button>导入</el-button> -->
        </div>
      </div>
    </div>

    <el-table :data="tableData">
      <el-table-column type="selection" width="42" />
      <el-table-column width="100" label="ID">
        <span slot-scope="scope">{{ scope.row.id }}</span>
      </el-table-column>
      <el-table-column width="200" label="机构名称">
        <span slot-scope="scope">{{ scope.row.name }}</span>
      </el-table-column>
      <el-table-column width="100" label="机构代码">
        <span slot-scope="scope">{{ scope.row.code }}</span>
      </el-table-column>
      <el-table-column label="状态">
        <span slot-scope="scope">{{
          scope.row.enable | zeroOneEnableDisableFilter
        }}</span>
      </el-table-column>
      <el-table-column width="100" label="负责人">
        <span slot-scope="scope">{{ scope.row.contactName }}</span>
      </el-table-column>
      <el-table-column width="120" label="联系方式">
        <span slot-scope="scope">{{ scope.row.contactPhone }}</span>
      </el-table-column>
      <el-table-column width="120" label="更新人">
        <span slot-scope="scope">{{ scope.row.updateName }}</span>
      </el-table-column>
      <el-table-column width="100" label="更新时间">
        <span slot-scope="scope">{{
          scope.row.updateTime | datetimeFilter
        }}</span>
      </el-table-column>
      <el-table-column :context="_self" label="操作" width="210" fixed="right">
        <div slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="edit(scope.row)">
            <i class="el-icon-edit"></i> 编辑
          </el-button>
          <el-button
            size="mini"
            type="primary"
            plain
            @click="toggleEnableOrg(scope.row)"
          >
            {{ !scope.row.enable | booleanEnableDisableFilter }}
          </el-button>
        </div>
      </el-table-column>
    </el-table>
    <div class="part-page">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100, 200, 300]"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </div>

    <OrgManagementDialog
      ref="theDialog"
      :org="selectedOrg"
      @reload="searchForm"
    />
  </div>
</template>

<script>
import { searchOrgs, toggleEnableOrg } from "../../../api/system-org";
import OrgManagementDialog from "./OrgManagementDialog";

export default {
  name: "OrgManagement",
  components: {
    OrgManagementDialog,
  },
  data() {
    return {
      form: {
        code: "",
        name: "",
        enableState: null,
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      selectedOrg: {},
    };
  },
  async created() {
    this.searchForm();
  },
  methods: {
    async searchForm() {
      this.tableData = (
        await searchOrgs({
          enable: this.form.enableState,
          code: this.form.code,
          name: this.form.name,
          pageNumber: this.currentPage,
          pageSize: this.pageSize,
        })
      ).data.data.records;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.searchForm();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.searchForm();
    },
    add() {
      this.selectedOrg = {};
      this.$refs.theDialog.openDialog();
    },
    edit(org) {
      this.selectedOrg = org;
      this.$refs.theDialog.openDialog();
    },
    async toggleEnableOrg(org) {
      await toggleEnableOrg({
        id: org.id,
        enable: org.enable === 0 ? 1 : 0,
      });
      this.searchForm();
    },
  },
};
</script>

<style></style>
