<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="院区名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入院区名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">
          新增院区
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="pageList" border>
      <el-table-column
        align="center"
        label="序号"
        type="index"
        :index="(index) => (queryParams.pageNum - 1) * queryParams.pageSize + index + 1"
        width="120"
      />
      <el-table-column align="center" label="院区名称" prop="name" :show-overflow-tooltip="true" />
      <el-table-column
        align="center"
        label="是否为本院区"
        prop="isHead"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ IS_MY_HOSPITAL.getLabelByValue(scope.row.isHead) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" prop="status" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ WARD_STATUS.getLabelByValue(scope.row.status) }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleSwitchEnable(scope.row)">
            {{ scope.row.status === WARD_STATUS.getValueByName('enabled') ? WARD_STATUS.getLabelByName('disabled') : WARD_STATUS.getLabelByName('enabled') }}
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.row)"
            :disabled="scope.row.status === WARD_STATUS.getValueByName('enabled')"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            :disabled="scope.row.status === WARD_STATUS.getValueByName('enabled')"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <CrudPopup ref="crud" />
  </div>
</template>

<script>
import ApiButton from "@/components/ApiButton/index.vue";
import { hospitalPageList, deleteHospital, updateHospital } from "@/api/admin";
import CrudPopup from "./components/CrudPopup/index.vue";
import { WARD_STATUS, IS_MY_HOSPITAL } from '@/enums/index'

export default {
  name: "HospManage",
  components: {
    ApiButton,
    CrudPopup,
  },
  data() {
    return {
      WARD_STATUS,
      IS_MY_HOSPITAL,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: "",
      },
      pageList: [],
    };
  },
  created() {
    this.loadData();
  },
  activated() {
    this.loadData();
  },
  methods: {
    loadData() {
      this.getList();
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      hospitalPageList(this.addDateRange(this.queryParams, this.dateRange)).then((res) => {
        const { list, page } = res.data;
        this.pageList = list || [];
        this.total = page?.total || 0;
        this.loading = false;
      });
    },

    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增 */
    handleAdd() {
      this.$refs.crud.open().then(this.getList);
    },
    handleSwitchEnable(item) {
      const isEnabled = item.status === this.WARD_STATUS.getValueByName('enabled');
      const fetch = async () => {
        await updateHospital({
          ...item,
          status: isEnabled ? this.WARD_STATUS.getValueByName('disabled') : this.WARD_STATUS.getValueByName('enabled'),
        });
        this.$message.success("操作成功");
        this.getList();
      };
      if (isEnabled) {
        this.$modal
          .confirm(`是否确认禁用？`)
          .then(fetch)
          .catch(() => {});
      } else {
        fetch();
      }
    },
    /** 编辑 */
    handleEdit(item) {
      this.$refs.crud.open(item).then(this.getList);
    },
    handleDelete(item) {
      this.$modal
        .confirm("是否确认删除？")
        .then(async () => {
          await deleteHospital(item.id);
          this.$message.success("操作成功");
          this.getList();
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped></style>
