<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="资讯分类编码" prop="informationCategoryCode">
        <el-input
          v-model="queryParams.informationCategoryCode"
          placeholder="请输入资讯分类编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资讯分类名称" prop="informationCategoryName">
        <el-input
          v-model="queryParams.informationCategoryName"
          placeholder="请输入资讯分类名称"
          clearable
          maxlength="100"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资讯分类类型" prop="informationCategoryType">
        <el-select
          v-model="queryParams.informationCategoryType"
          placeholder="请选择"
          style="width: 250px"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="资讯分类排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入资讯分类排序"
          clearable
          @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"
          v-hasPermi="['system:InformationCategory: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="['system:InformationCategory: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="['system:InformationCategory: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="['system:InformationCategory:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="InformationCategoryList"
      @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="informationCategoryCode"
      />
      <el-table-column
        label="资讯分类名称"
        align="center"
        prop="informationCategoryName"
      />
      <el-table-column
        label="资讯分类类型"
        align="center"
        prop="informationCategoryType"
      >
        <template slot-scope="scope">
          {{
            scope.row.informationCategoryType == "HEALTH_NOUS" ? "健康常识" : ""
          }}
          {{
            scope.row.informationCategoryType == "NURSING_NEWS"
              ? "护理新闻"
              : ""
          }}
        </template>
      </el-table-column>

      <el-table-column label="资讯分类排序" align="center" prop="sort" />
      <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="['system:InformationCategory:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:InformationCategory:remove']"
            >删除</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"
    />

    <!-- 添加资讯分类对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="addopen"
      width="1100px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="110px"
        :inline="true"
      >
        <div
          v-for="(item, index) in form.informationCategoryList"
          :key="index"
          style="margin-top: 20px; border-bottom: 1px solid #f3f3f3"
        >
          <el-form-item
            label="资讯分类名称"
            :rules="rules.informationCategoryList.informationCategoryName"
            :prop="`informationCategoryList.${index}.informationCategoryName`"
          >
            <el-input
              maxlength="100"
              style="width: 200px"
              v-model="item.informationCategoryName"
              placeholder="请输入资讯分类名称"
            />
          </el-form-item>
          <el-form-item
            label="资讯分类排序"
            :rules="rules.informationCategoryList.sort"
            :prop="`informationCategoryList.${index}.sort`"
          >
            <el-input
              v-model.number="item.sort"
              maxlength="5"
              oninput="value=value.replace(/[^\d]/g,'')"
              style="width: 200px"
              placeholder="请输入资讯分类排序"
            />
          </el-form-item>
          <el-form-item
            label="资讯分类类型"
            :rules="rules.informationCategoryList.informationCategoryType"
            :prop="`informationCategoryList.${index}.informationCategoryType`"
          >
            <el-select
              v-model="item.informationCategoryType"
              placeholder="请选择"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </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="informationCategoryName">
          <el-input
            v-model="form.informationCategoryName"
            placeholder="请输入资讯分类名称"
            style="width: 250px"
            maxlength="100"
          />
        </el-form-item>

        <el-form-item label="资讯分类排序" prop="sort">
          <el-input
            oninput="value=value.replace(/[^\d]/g,'')"
            v-model.number="form.sort"
            placeholder="请输入分类排序"
            style="width: 250px"
            maxlength="5"
          />
        </el-form-item>
        <el-form-item label="资讯分类类型" prop="informationCategoryType">
          <el-select
            v-model="form.informationCategoryType"
            placeholder="请选择"
            style="width: 250px"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <!-- <el-radio v-model="form.informationCategoryType" label="HEALTH_NOUS"
            >健康咨询</el-radio
          >
          <el-radio v-model="form.informationCategoryType" label="OTHER"
            >其他</el-radio
          > -->
        </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 {
  listInformationCategory,
  getInformationCategory,
  delInformationCategory,
  addInformationCategory,
  updateInformationCategory,
} from "@/api/system/InformationCategory";

export default {
  name: "InformationCategory",
  data() {
    return {
      options: [
        {
          value: "HEALTH_NOUS",
          label: "健康常识",
        },
        {
          value: "NURSING_NEWS",
          label: "护理新闻",
        },
      ],
      value: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资讯分类表格数据
      InformationCategoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      addopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        informationCategoryCode: null,
        informationCategoryName: null,
        informationCategoryType: null,
        sort: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        informationCategoryList: {
          informationCategoryName: [
            {
              required: true,
              message: "请选择资讯分类名称",
              trigger: "blur",
            },
          ],

          informationCategoryType: [
            {
              required: true,
              message: "请选择资讯分类类型",
              trigger: "blur",
            },
          ],
          sort: [
            {
              required: true,
              message: "请选择资讯分类排序",
              trigger: "blur",
            },
          ],
        },
        informationCategoryName: [
          {
            required: true,
            message: "请选择资讯分类名称",
            trigger: "blur",
          },
        ],

        informationCategoryType: [
          {
            required: true,
            message: "请选择资讯分类类型",
            trigger: "blur",
          },
        ],
        sort: [
          {
            required: true,
            message: "请选择资讯分类排序",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //删除deldisease
    deldisease(index) {
      this.form.informationCategoryList.splice(index, 1);
    },
    adddisease() {
      var obj = {
        informationCategoryName: "",
        informationCategoryType: "",
        sort: "",
      };
      if (this.form.informationCategoryList.length == 5) {
        this.$message.error("最多只能5条");
      } else {
        this.form.informationCategoryList.push(obj);
      }
    },
    /** 查询资讯分类列表 */
    getList() {
      this.loading = true;
      listInformationCategory(this.queryParams).then((response) => {
        this.InformationCategoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.addopen = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    // reset() {
    //   this.form = {

    //     id: null,
    //     informationCategoryCode: null,
    //     informationCategoryName: null,
    //     informationCategoryType: null,
    //     sort: null,
    //     createBy: null,
    //     createTime: null,
    //     updateBy: null,
    //     updateTime: null
    //   };

    //   this.resetForm("form");
    // },
    // 表单重置
    reset() {
      this.form = {
        informationCategoryList: [
          {
            // id: null,
            // informationCategoryCode: null,
            informationCategoryName: null,
            informationCategoryType: null,
            sort: null,
            // createBy: null,
            // createTime: null,
            // updateBy: null,
            // updateTime: 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;
      getInformationCategory(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资讯分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInformationCategory(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addInformationCategory(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("新增成功");
                this.addopen = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除资讯分类编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInformationCategory(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/InformationCategory/export",
        {
          ...this.queryParams,
        },
        `InformationCategory_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
