<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="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
          <el-option
            v-for="dict in dict.type.applet_pet_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出生年月" prop="birthday">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.birthday"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择出生年月">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="体重" prop="weight">
        <el-select v-model="queryParams.weight" placeholder="请选择体重" clearable size="small">
          <el-option
            v-for="dict in dict.type.applet_pet_weight"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <template v-if="showMoreCondition">
        <el-form-item label="疫苗" prop="vaccine">
          <el-select v-model="queryParams.vaccine" placeholder="请选择疫苗" clearable size="small">
            <el-option
              v-for="dict in dict.type.applet_pet_my"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="驱虫" prop="deworm">
          <el-select v-model="queryParams.deworm" placeholder="请选择驱虫" clearable size="small">
            <el-option
              v-for="dict in dict.type.applet_pet_qc"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="绝育" prop="neutered">
          <el-select v-model="queryParams.neutered" placeholder="请选择绝育" clearable size="small">
            <el-option
              v-for="dict in dict.type.applet_pet_jy"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="类别" prop="petType">
          <el-select v-model="queryParams.petType" placeholder="请选择类别" clearable size="small">
            <el-option
              v-for="dict in dict.type.applet_money_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="证件" prop="petCard">
          <el-select v-model="queryParams.petCard" placeholder="请选择证件" clearable size="small">
            <el-option
              v-for="dict in dict.type.applet_pet_bl"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="其他健康说明" prop="healthOther">
          <el-input
            v-model="queryParams.healthOther"
            placeholder="请输入其他健康说明"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="订单标识" prop="orderId">
          <el-input
            v-model="queryParams.orderId"
            placeholder="请输入订单标识"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </template>
      <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:AppletPet: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:AppletPet: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:AppletPet: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:AppletPet:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="AppletPetList" @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="headImage" v-if="columns[0].visible"/>-->
      <el-table-column label="用户头像" align="center" prop="headImage">
        <template slot-scope="{ row }">
          <el-image v-if="row.headImage" :src="row.headImage" :preview-src-list="[row.headImage]" class="small-img circle-img"/>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="性别" align="center" prop="sex" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.applet_pet_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="品种" align="center" prop="type" />
      <el-table-column label="出生年月" align="center" prop="birthday" width="180" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="体重" align="center" prop="weight" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.applet_pet_weight" :value="scope.row.weight"/>
        </template>
      </el-table-column>
      <el-table-column label="性格" align="center" prop="personality" />
      <el-table-column label="疫苗" align="center" prop="vaccine">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.applet_pet_my" :value="scope.row.vaccine"/>
        </template>
      </el-table-column>
      <el-table-column label="驱虫" align="center" prop="deworm" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.applet_pet_qc" :value="scope.row.deworm"/>
        </template>
      </el-table-column>
      <el-table-column label="绝育" align="center" prop="neutered">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.applet_pet_jy" :value="scope.row.neutered"/>
        </template>
      </el-table-column>
      <el-table-column label="类别" align="center" prop="petType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.applet_money_type" :value="scope.row.petType"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="健康状况" align="center" prop="health" v-if="columns[11].visible">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.applet_pet_jk" :value="scope.row.health"/>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column label="健康状况" align="center" prop="health" />

      <el-table-column label="证件" align="center" prop="petCard" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.applet_pet_bl" :value="scope.row.petCard"/>
        </template>
      </el-table-column>
      <el-table-column label="其他健康说明" align="center" prop="healthOther" />
      <el-table-column label="删除标识" align="center" prop="delFlag" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.applet_yes_no" :value="scope.row.delFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单标识" align="center" prop="orderId" />
      <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:AppletPet:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['model:AppletPet: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="宠物头像">
<!--          <imageUpload v-model="form.headImage"/>-->
          <oss-image-upload v-model="form.headImage" :limit="1"></oss-image-upload>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.applet_pet_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品种" prop="type">
          <el-input v-model="form.type" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="出生年月" prop="birthday">
          <el-date-picker clearable size="small"
                          v-model="form.birthday"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择出生年月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="体重" prop="weight">
          <el-select v-model="form.weight" placeholder="请选择体重">
            <el-option
              v-for="dict in dict.type.applet_pet_weight"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性格" prop="personality">
          <el-input v-model="form.personality" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="疫苗" prop="vaccine">
          <el-select v-model="form.vaccine" placeholder="请选择疫苗">
            <el-option
              v-for="dict in dict.type.applet_pet_my"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="驱虫" prop="deworm">
          <el-select v-model="form.deworm" placeholder="请选择驱虫">
            <el-option
              v-for="dict in dict.type.applet_pet_qc"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="绝育" prop="neutered">
          <el-select v-model="form.neutered" placeholder="请选择绝育">
            <el-option
              v-for="dict in dict.type.applet_pet_jy"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类别" prop="petType">
          <el-select v-model="form.petType" placeholder="请选择类别">
            <el-option
              v-for="dict in dict.type.applet_money_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="健康状况">-->
<!--          <el-checkbox-group v-model="form.health">-->
<!--            <el-checkbox-->
<!--              v-for="dict in dict.type.applet_pet_jk"-->
<!--              :key="dict.value"-->
<!--              :label="dict.value">-->
<!--              {{dict.label}}-->
<!--            </el-checkbox>-->
<!--          </el-checkbox-group>-->
<!--        </el-form-item>-->
        <el-form-item label="健康状况" prop="healthOther">
          <el-input v-model="form.health" placeholder="请输入其他健康说明" />
        </el-form-item>
        <el-form-item label="证件" prop="petCard">
          <el-select v-model="form.petCard" placeholder="请选择证件">
            <el-option
              v-for="dict in dict.type.applet_pet_bl"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="其他健康说明" prop="healthOther">
          <el-input v-model="form.healthOther" placeholder="请输入其他健康说明" />
        </el-form-item>
        <el-form-item label="删除标识" prop="delFlag">
          <el-select v-model="form.delFlag" placeholder="请选择删除标识">
            <el-option
              v-for="dict in dict.type.applet_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户标识" prop="orderId">
          <el-input v-model="form.orderId" 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 { listAppletPet, getAppletPet, delAppletPet, addAppletPet, updateAppletPet, exportAppletPet } from "@/api/model/AppletPet";

export default {
  name: "AppletPet",
  dicts: ['applet_pet_sex', 'applet_pet_weight', 'applet_pet_my', 'applet_pet_qc', 'applet_pet_jy', 'applet_money_type', 'applet_pet_bl', 'applet_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 宠物信息表格数据
      AppletPetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        headImage: null,
        nickName: null,
        sex: null,
        type: null,
        birthday: null,
        weight: null,
        personality: null,
        vaccine: null,
        deworm: null,
        neutered: null,
        petType: null,
        health: null,
        petCard: null,
        healthOther: null,
        orderId: null

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

        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],

        type: [
          { required: true, message: "品种不能为空", trigger: "change" }
        ],

        delFlag: [
          { required: true, message: "删除标识不能为空", trigger: "blur" }
        ],

      },
      columns: [
        { key: 1, label: "宠物头像", visible:  false  },
        { key: 2, label: "昵称", visible:  true  },
        { key: 3, label: "性别", visible:  true  },
        { key: 4, label: "品种", visible:  true  },
        { key: 5, label: "出生年月", visible:  true  },
        { key: 6, label: "体重（kg）", visible:  true  },
        { key: 7, label: "性格", visible:  true  },
        { key: 8, label: "疫苗（是否已接种）", visible:  true  },
        { key: 9, label: "驱虫（是否已处理）", visible:  true  },
        { key: 10, label: "绝育（是否已绝育）", visible:  false  },
        { key: 11, label: "类别", visible:  false  },
        { key: 12, label: "健康状况", visible:  false  },
        { key: 13, label: "证件", visible:  false  },
        { key: 14, label: "其他健康说明", visible:  false  },
        { key: 15, label: "删除标识", visible:  false  },
        { key: 17, label: "创建时间", visible:  false  },
        { key: 20, label: "用户标识", visible:  false  },
     ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询宠物信息列表 */
    getList() {
      this.loading = true;
      listAppletPet(this.queryParams).then(response => {
        this.AppletPetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        headImage: null,
        nickName: null,
        sex: null,
        type: null,
        birthday: null,
        weight: null,
        personality: null,
        vaccine: null,
        deworm: null,
        neutered: null,
        petType: null,
        health: [],
        petCard: null,
        healthOther: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        orderId: 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
      getAppletPet(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) {
            updateAppletPet(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppletPet(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 delAppletPet(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有宠物信息数据项？').then(() => {
        this.exportLoading = true;
        return exportAppletPet(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
