<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="人群名称" prop="crowdName">
        <el-input
          v-model="queryParams.crowdName"
          placeholder="请输入人群名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人群类型" prop="crowdType">
        <el-select
          v-model="queryParams.crowdType"
          placeholder="请选择人群类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.crowd_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否禁用" prop="whetherDisable">
        <el-select
          v-model="queryParams.whetherDisable"
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <!-- <el-input
          v-model="queryParams.whetherDisable"
          placeholder="请输入是否禁用，0：否，1：是"
          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="['manage:crowdInfo: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:crowdInfo: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:crowdInfo: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:crowdInfo:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="crowdInfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="人群名称" align="center" prop="crowdName" />
      <el-table-column
        label="最小适合年龄"
        align="center"
        prop="minSuitableAge"
      />
      <el-table-column
        label="最大适合年龄"
        align="center"
        prop="maxSuitableAge"
      />
      <el-table-column label="适合性别" align="center" prop="suitableGender">
        <template slot-scope="scope">
          {{ scope.row.suitableGender == "ALL" ? "全部" : "" }}
          {{ scope.row.suitableGender == "MALE" ? "男" : "" }}
          {{ scope.row.suitableGender == "FEMALE" ? "女" : "" }}
          {{ scope.row.suitableGender == "UNKNOWN" ? "未知" : "" }}
        </template>
      </el-table-column>

      <el-table-column label="人群类型" align="center" prop="crowdType">
        <template slot-scope="scope">
          {{ scope.row.crowdType == "DEFAULT" ? "默认" : "" }}
          {{ scope.row.crowdType == "CHRONIC_DISEASE" ? "慢病" : "" }}
          {{ scope.row.crowdType == "SPECIAL_POPULATIONS" ? "特殊人群" : "" }}
          {{ scope.row.crowdType == "KEY_GROUPS" ? "重点人群" : "" }}
        </template>
      </el-table-column>
      <el-table-column label="特殊人群" align="center" prop="specialType">
        <template slot-scope="scope">
          {{ scope.row.specialType == "1" ? "65岁及以上老年人" : "" }}
          {{ scope.row.specialType == "2" ? "60-64岁老年人" : "" }}
          {{ scope.row.specialType == "3" ? "60-0-6岁儿童" : "" }}
          {{ scope.row.specialType == "4" ? "孕产妇" : "" }}
          {{ scope.row.specialType == "5" ? "残疾人" : "" }}
          {{ scope.row.specialType == "6" ? "优抚对象" : "" }}
          {{ scope.row.specialType == "20" ? "高血压" : "" }}
          {{ scope.row.specialType == "21" ? "二型糖尿病" : "" }}
          {{ scope.row.specialType == "22" ? "高血压合并糖尿病" : "" }}
          {{ scope.row.specialType == "23" ? "脑卒中康复期" : "" }}
          {{ scope.row.specialType == "24" ? "冠心病" : "" }}
          {{ scope.row.specialType == "40" ? "严重精神障碍" : "" }}
          {{ scope.row.specialType == "41" ? "结核病" : "" }}
          {{ scope.row.specialType == "42" ? "肿瘤病" : "" }}
          {{ scope.row.specialType == "43" ? "贫困人口" : "" }}
          {{ scope.row.specialType == "44" ? "计划生育特殊家庭" : "" }}

        </template>
      </el-table-column>
      <el-table-column label="是否禁用" align="center" prop="whetherDisable">
        <template slot-scope="scope">
          {{ scope.row.whetherDisable == "0" ? "否" : "" }}
          {{ scope.row.whetherDisable == "1" ? "是" : "" }}
        </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="['manage:crowdInfo:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manage:crowdInfo: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="open" width="750px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="110px"
        :inline="true"
      >
        <el-form-item label="人群名称" prop="crowdName">
          <el-input v-model="form.crowdName" placeholder="请输入人群名称" />
        </el-form-item>
        <el-form-item label="最小适合年龄" prop="minSuitableAge">
          <el-input
          oninput="value=value.replace(/[^\d.]/g,'')"
            v-model="form.minSuitableAge"
            placeholder="请输入最小适合年龄"
          />
        </el-form-item>
        <el-form-item label="最大适合年龄" prop="maxSuitableAge">
          <el-input
          oninput="value=value.replace(/[^\d.]/g,'')"
            v-model="form.maxSuitableAge"
            placeholder="请输入最大适合年龄"
          />
        </el-form-item>
        <el-form-item label="适合性别" prop="suitableGender">
          <el-select
          style="width:206px"
            v-model="form.suitableGender"
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in optionssex"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="人群类型" prop="crowdType">
          <el-select v-model="form.crowdType" placeholder="请选择人群类型" style="width:206px">
            <el-option
              v-for="dict in dict.type.crowd_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="特殊人群" prop="specialType">
          <el-select v-model="form.specialType" placeholder="请选择特殊人群" style="width:206px">
            <el-option
              v-for="dict in dict.type.special_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否禁用" prop="whetherDisable">
          <el-select
          style="width:206px"
            v-model="form.whetherDisable"
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in optionswhether"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number
            v-model="form.sort"
            controls-position="right"
            :min="0"
            placeholder="请输入排序"
            style="width: 206px"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" style="width:206px" maxlength="100"/>
        </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 { listCrowdInfo, getCrowdInfo, delCrowdInfo, addCrowdInfo, updateCrowdInfo } from "@/api/manage/crowdInfo";

export default {
  name: "CrowdInfo",
  dicts: ['crowd_type','special_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人群信息表格数据
      crowdInfoList: [],
      optionssex: [{
        value: 'MALE',
        label: '男'
      },
      {
        value: 'FEMALE',
        label: '女'
      },
        {
          value: 'UNKNOWN',
          label: '未知'
        },
        {
          value: 'ALL',
          label: '全部'
        } 

      ],

      optionswhether: [{
        value: 0,
        label: '否'
      }, {
        value: 1,
        label: '是'
      }],
      options: [{
        value: 0,
        label: '否'
      }, {
        value: 1,
        label: '是'
      }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        crowdName: null,
        crowdType: null,
        specialType: null,
        whetherDisable: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        crowdName: [
          { required: true, message: "人群名称不能为空", trigger: "blur" }
        ],
        minSuitableAge: [
          { required: true, message: "最小适合年龄不能为空", trigger: "blur" }
        ],
        maxSuitableAge: [
          { required: true, message: "最大适合年龄不能为空", trigger: "blur" }
        ],
        suitableGender: [
          { required: true, message: "适合性别不能为空", trigger: "blur" }
        ],
        crowdType: [
          { required: true, message: "人群类型不能为空", trigger: "blur" }
        ],
        specialType:[
        { required: true, message: "特殊人群不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询人群信息列表 */
    getList() {
      this.loading = true;
      listCrowdInfo(this.queryParams).then(response => {
        this.crowdInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        crowdName: null,
        minSuitableAge: null,
        maxSuitableAge: null,
        suitableGender: null,
        crowdType: null,
        specialType: null,
        whetherDisable: 0,
        remark: null,
        sort: undefined,
        delFlag: null,
        createBy: null,
        createDate: null,
        updateBy: null,
        updateDate: 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.open = true;
      this.title = "添加人群信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCrowdInfo(id).then(response => {
        this.form = response.data;
        if(response.data.whetherDisable==0){
          this.form.whetherDisable="否"
        }else if(response.data.whetherDisable==1){
          this.form.whetherDisable="是"

        }
        this.open = true;
        this.title = "修改人群信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            if(this.form.whetherDisable=="否"){
          this.form.whetherDisable=0
        }else if(this.form.whetherDisable=="是"){
          this.form.whetherDisable=1

        }
        console.log(this.form)
            updateCrowdInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCrowdInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除人群信息编号为"' + ids + '"的数据项？').then(function () {
        return delCrowdInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manage/crowdInfo/export', {
        ...this.queryParams
      }, `人群信息_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss"  scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}

</style>
