<template>
  <div class="app-container" ref="layout">
    <div ref="topform" class="form">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="资讯分类编码" prop="infoCategoryCode">
          <el-input v-model="queryParams.infoCategoryCode" placeholder="请输入资讯分类编码" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="资讯分类名称" prop="infoCategoryName">
          <el-input v-model="queryParams.infoCategoryName" placeholder="请输入资讯分类名称" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <!-- <el-form-item label="资讯分类排序" prop="infoCategorySort">
        <el-input
          v-model="queryParams.infoCategorySort"
          placeholder="请输入资讯分类排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资讯创建人" prop="infoCategoryCreator">
        <el-input
          v-model="queryParams.infoCategoryCreator"
          placeholder="请输入资讯创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资讯创建时间" prop="infoCategoryCreateTime">
        <el-date-picker clearable
          v-model="queryParams.infoCategoryCreateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择资讯创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改人" prop="infoCategoryReviser">
        <el-input
          v-model="queryParams.infoCategoryReviser"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改时间" prop="infoCategoryModifyTime">
        <el-date-picker clearable
          v-model="queryParams.infoCategoryModifyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择修改时间">
        </el-date-picker>
      </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>
    </div>
    <div ref="mb8" class="mb8">
      <el-row :gutter="10" class="">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['manage:category:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['manage:category:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['manage:category:remove']">删除</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['manage:category:export']"
        >导出</el-button>
      </el-col> -->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </div>
    <div ref="table">
      <el-table :max-height="maxTableHeight" v-loading="loading" :data="categoryList"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
        <el-table-column label="资讯分类编码" align="center" prop="infoCategoryCode" />
        <el-table-column label="资讯分类名称" align="center" prop="infoCategoryName" />
        <!-- <el-table-column
        label="资讯分类类型"
        align="center"
        prop="infoCategoryType"
      /> -->
        <el-table-column label="资讯分类排序" align="center" prop="infoCategorySort" />
        <!-- <el-table-column label="资讯创建人" align="center" prop="infoCategoryCreator" />
      <el-table-column label="资讯创建时间" align="center" prop="infoCategoryCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.infoCategoryCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
</el-table-column>
<el-table-column label="修改人" align="center" prop="infoCategoryReviser" />
<el-table-column label="修改时间" align="center" prop="infoCategoryModifyTime" width="180">
  <template slot-scope="scope">
          <span>{{ parseTime(scope.row.infoCategoryModifyTime, '{y}-{m}-{d}') }}</span>
        </template>
</el-table-column> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['manage:category:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['manage:category:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" /> -->
    <myPagination v-show="total > 0" :total="total" :pageSize="queryParams.pageSize"
      :indexFromWrap="queryParams.pageNum" @updateCPage="updateCPage"></myPagination>
    <!-- 添加或修改资讯分类对话框 -->
    <el-dialog :title="title" :visible.sync="addopen" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" :inline="true">
        <div v-for="(item, index) in form.infoCategoryList" :key="index"
          style="margin-top: 20px; border-bottom: 1px solid #f3f3f3">
          <el-form-item label="资讯分类名称" :rules="rules.infoCategoryList.infoCategoryName"
            :prop="`infoCategoryList.${index}.infoCategoryName`">
            <el-input maxlength="100" style="width: 200px" v-model="item.infoCategoryName" placeholder="请输入资讯分类名称" />
          </el-form-item>
          <el-form-item label="资讯分类排序" :rules="rules.infoCategoryList.infoCategorySort"
            :prop="`infoCategoryList.${index}.infoCategorySort`">
            <el-input v-model.number="item.infoCategorySort" maxlength="5" oninput="value=value.replace(/[^\d]/g,'')"
              style="width: 200px" placeholder="请输入资讯分类排序" />
          </el-form-item>

          <el-button type="primary" circle plain icon="el-icon-circle-plus-outline" @click="adddisease"
            v-if="index == 0"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle plain @click="deldisease(index)" v-if="index != 0">
          </el-button>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 修改 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="资讯分类名称" prop="infoCategoryName">
          <el-input v-model="form.infoCategoryName" placeholder="请输入资讯分类名称" style="width: 250px" maxlength="100" />
        </el-form-item>

        <el-form-item label="资讯分类排序" prop="infoCategorySort">
          <el-input oninput="value=value.replace(/[^\d]/g,'')" v-model.number="form.infoCategorySort"
            placeholder="请输入分类排序" style="width: 250px" maxlength="5" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCategory, getCategory, delCategory, addCategory, updateCategory } from "@/api/manage/category";

export default {
  name: "Category",
  data() {
    return {
      maxTableHeight: undefined,
      options: [
        {
          value: "HEALTH_NOUS",
          label: "健康常识",
        },
        {
          value: "NURSING_NEWS",
          label: "护理新闻",
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资讯分类表格数据
      categoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      addopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        infoCategoryCode: null,
        infoCategoryName: null,
        infoCatrgoryType: null,
        infoCategorySort: null,
        infoCategoryCreator: null,
        infoCategoryCreateTime: null,
        infoCategoryReviser: null,
        infoCategoryModifyTime: null
      },
      // 表单参数
      form: {
        infoCategoryList: [],

      },
      // 表单校验
      rules: {
        infoCategoryList: {
          infoCategoryName: [
            {
              required: true,
              message: "请选择资讯分类名称",
              trigger: "blur",
            },
          ],

          // infoCatrgoryType: [
          //   {
          //     required: true,
          //     message: "请选择资讯分类类型",
          //     trigger: "blur",
          //   },
          // ],
          infoCategorySort: [
            {
              required: true,
              message: "请选择资讯分类排序",
              trigger: "blur",
            },
          ],
        },
        infoCategoryName: [
          {
            required: true,
            message: "请选择资讯分类名称",
            trigger: "blur",
          },
        ],

        // infoCatrgoryType: [
        //   {
        //     required: true,
        //     message: "请选择资讯分类类型",
        //     trigger: "blur",
        //   },
        // ],
        infoCategorySort: [
          {
            required: true,
            message: "请选择资讯分类排序",
            trigger: "blur",
          },
        ],
      }
    };
  },
  mounted() {
    this.getMaxTableHeight()
    this.screenChange()
  },
  created() {
    this.getList();
  },
  methods: {
    updateCPage(index, size) {
      this.queryParams.pageNum = index
      this.queryParams.pageSize = size
      this.getList();
    },
    /** 查询资讯分类列表 */
    getList() {
      this.loading = true;
      listCategory(this.queryParams).then(response => {
        this.categoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.addopen = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        infoCategoryList: [
          {

            infoCategoryName: null,
            // infoCatrgoryType: null,
            infoCategorySort: null,

          },
        ],

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.addopen = true;
      this.title = "添加资讯分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCategory(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资讯分类";
      });
    },
    //删除deldisease
    deldisease(index) {
      this.form.infoCategoryList.splice(index, 1);
    },
    adddisease() {
      var obj = {
        infoCategoryName: "",
        // infoCatrgoryType: "",
        infoCategorySort: "",
      };
      if (this.form.infoCategoryList.length == 5) {
        this.$message.error("最多只能5条");
      } else {
        this.form.infoCategoryList.push(obj);
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCategory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCategory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.addopen = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资讯分类编号为"' + ids + '"的数据项？').then(function () {
        return delCategory(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manage/category/export', {
        ...this.queryParams
      }, `category_${new Date().getTime()}.xlsx`)
    },
    // 获取表格最高高度
    getMaxTableHeight() {
      const windowInnerHeight = window.innerHeight // 屏幕可视高度
      const layoutDiv = this.$refs.layout
      const formDiv = this.$refs.topform
      const mb8Div = this.$refs.mb8
      this.maxTableHeight =
        windowInnerHeight - 134 -
        this.getBoxPadding(layoutDiv) -
        this.getBoxHeight(mb8Div) -
        this.getBoxHeight(formDiv)
    },
    // 屏幕resize监听
    screenChange() {
      // 屏幕resize监听事件：一旦屏幕宽高发生变化，就会执行resize
      window.addEventListener('resize', this.getMaxTableHeight, true)
      // 将屏幕监听事件移除
      // 这步是必须的。离开页面时不移除，再返回，或者进入到别的有相同元素的页面会报错
      // 或者将这里的方法直接写在beforeDestroy函数中也可以
      this.$once('hook:beforeDestroy', () => {
        window.removeEventListener('resize', this.getMaxTableHeight, true)
      })
    },
  }
};
</script>
