<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="medium" class="ry_form">
      <el-form-item label="删除标记" prop="deiFlag">
        <el-input
          v-model="queryParams.deiFlag"
          placeholder="请输入删除标记"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宠物年龄" prop="petAge">
        <el-input
          v-model="queryParams.petAge"
          placeholder="请输入宠物年龄"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出生日期" prop="petBirthday">
        <el-date-picker
            clearable
            size="small"
            v-model="queryParams.petBirthday"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择出生日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="宠物品种编号" prop="petBread">
        <el-input
          v-model="queryParams.petBread"
          placeholder="请输入宠物品种编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宠物名称" prop="petName">
        <el-input
          v-model="queryParams.petName"
          placeholder="请输入宠物名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宠物性格" prop="petPersonality">
        <el-input
          v-model="queryParams.petPersonality"
          placeholder="请输入宠物性格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <template v-if="showMoreCondition">-->
      <el-form-item label="宠物性别" prop="petSex">
        <el-select v-model="queryParams.petSex" placeholder="请选择宠物性别" clearable size="small">
        </el-select>
      </el-form-item>
      <el-form-item label="宠物种类编号" prop="petSpecies">
        <el-input
          v-model="queryParams.petSpecies"
          placeholder="请输入宠物种类编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宠物体重,1=小型,2=中型,3=大型" prop="petWeight">
        <el-input
          v-model="queryParams.petWeight"
          placeholder="请输入宠物体重,1=小型,2=中型,3=大型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户编号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="flex_one tr">
        <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="['model:AppPets: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="['model:AppPets: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="['model:AppPets:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['model:AppPets:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="AppPetsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="宠物编号" align="center" prop="petId" />
      <el-table-column label="删除标记" align="center" prop="deiFlag" v-if="columns[0].visible"/>
      <el-table-column label="宠物年龄" align="center" prop="petAge" v-if="columns[1].visible"/>
      <el-table-column label="出生日期" align="center" prop="petBirthday" width="180" v-if="columns[2].visible">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.petBirthday, '')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="宠物品种编号" align="center" prop="petBread" v-if="columns[3].visible"/>
      <el-table-column label="宠物头像" align="center" prop="petImage" v-if="columns[4].visible"/>
      <el-table-column label="宠物名称" align="center" prop="petName" v-if="columns[5].visible"/>
      <el-table-column label="宠物性格" align="center" prop="petPersonality" v-if="columns[6].visible"/>
      <el-table-column label="宠物性别" align="center" prop="petSex" v-if="columns[7].visible"/>
      <el-table-column label="宠物种类编号" align="center" prop="petSpecies" v-if="columns[8].visible"/>
      <el-table-column label="宠物体重,1=小型,2=中型,3=大型" align="center" prop="petWeight" v-if="columns[9].visible"/>
      <el-table-column label="备注" align="center" prop="remark" v-if="columns[10].visible"/>
      <el-table-column label="用户编号" align="center" prop="userId" v-if="columns[11].visible"/>
      <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="['model:AppPets:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['model:AppPets: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="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="108px" inline class="dialog-form-two">
        <el-form-item label="删除标记" prop="deiFlag">
          <el-input v-model="form.deiFlag" placeholder="请输入删除标记" />
        </el-form-item>
        <el-form-item label="宠物年龄" prop="petAge">
          <el-input v-model="form.petAge" placeholder="请输入宠物年龄" />
        </el-form-item>
        <el-form-item label="出生日期" prop="petBirthday">
          <el-date-picker clearable size="small"
                        v-model="form.petBirthday"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="宠物品种编号" prop="petBread">
          <el-input v-model="form.petBread" placeholder="请输入宠物品种编号" />
        </el-form-item>
        <el-form-item label="宠物头像">
          <imageUpload v-model="form.petImage"/>
        </el-form-item>
        <el-form-item label="宠物名称" prop="petName">
          <el-input v-model="form.petName" placeholder="请输入宠物名称" />
        </el-form-item>
        <el-form-item label="宠物性格" prop="petPersonality">
          <el-input v-model="form.petPersonality" placeholder="请输入宠物性格" />
        </el-form-item>
        <el-form-item label="宠物种类编号" prop="petSpecies">
          <el-input v-model="form.petSpecies" placeholder="请输入宠物种类编号" />
        </el-form-item>
        <el-form-item label="宠物体重,1=小型,2=中型,3=大型" prop="petWeight">
          <el-input v-model="form.petWeight" placeholder="请输入宠物体重,1=小型,2=中型,3=大型" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="用户编号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户编号" />
        </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 { listAppPets, getAppPets, delAppPets, addAppPets, updateAppPets, exportAppPets } from "@/api/model/AppPets";

export default {
  name: "AppPets",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 宠物信息表格数据
      AppPetsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deiFlag: null,

        petAge: null,

        petBirthday: null,

        petBread: null,

        petImage: null,

        petName: null,

        petPersonality: null,

        petSex: null,

        petSpecies: null,

        petWeight: null,

        userId: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        petName: [
          { required: true, message: "宠物名称不能为空", trigger: "blur" }
        ],

        userId: [
          { required: true, message: "用户编号不能为空", trigger: "blur" }
        ],

      },
      columns: [
    { key: 3, label: "删除标记", visible:  true  },
    { key: 4, label: "宠物年龄", visible:  true  },
    { key: 5, label: "出生日期", visible:  true  },
    { key: 6, label: "宠物品种编号", visible:  true  },
    { key: 7, label: "宠物头像", visible:  false  },
    { key: 8, label: "宠物名称", visible:  true  },
    { key: 9, label: "宠物性格", visible:  true  },
    { key: 10, label: "宠物性别", visible:  false  },
    { key: 11, label: "宠物种类编号", visible:  false  },
    { key: 12, label: "宠物体重,1=小型,2=中型,3=大型", visible:  false  },
    { key: 13, label: "备注", visible:  false  },
    { key: 16, label: "用户编号", visible:  false  },
     ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询宠物信息列表 */
    getList() {
      this.loading = true;
      listAppPets(this.queryParams).then(response => {
        this.AppPetsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        petId: null,

        createBy: null,

        createTime: null,

        deiFlag: null,

        petAge: null,

        petBirthday: null,

        petBread: null,

        petImage: null,

        petName: null,

        petPersonality: null,

        petSex: null,

        petSpecies: null,

        petWeight: null,

        remark: null,

        updateBy: null,

        updateTime: null,

        userId: 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.petId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加宠物信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const petId = row.petId || this.ids
      getAppPets(petId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改宠物信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.petId != null) {
            updateAppPets(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppPets(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const petIds = row.petId || this.ids;
      this.$modal.confirm('是否确认删除宠物信息编号为"' + petIds + '"的数据项？').then(function() {
        return delAppPets(petIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有宠物信息数据项？').then(() => {
        this.exportLoading = true;
        return exportAppPets(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
