<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="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入宠物名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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.pet_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出生年月" prop="birthDate">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.birthDate"
          type="month"
          value-format="yyyy-MM"
          placeholder="选择出生年月">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="体重" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入体重"
          clearable
          size="small"
          type="number" step="0.01"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="品种" prop="breed">
        <el-input
          v-model="queryParams.breed"
          placeholder="请输入品种"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体型" prop="bodyType">
        <el-input v-model="queryParams.bodyType" placeholder="请输入体型" />
      </el-form-item>
      <template v-if="showMoreCondition">
        <el-form-item label="疫苗情况" prop="vaccineStatus">
          <el-select v-model="queryParams.vaccineStatus" placeholder="请选择疫苗情况" clearable size="small">
            <el-option
              v-for="dict in dict.type.pet_vaccine_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="绝育情况" prop="sterilization">
          <el-select v-model="queryParams.sterilization" placeholder="请选择绝育情况" clearable size="small">
            <el-option
              v-for="dict in dict.type.pet_sterilization"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="驱虫情况" prop="dewormingStatus">
          <el-select v-model="queryParams.dewormingStatus" placeholder="请选择驱虫情况" clearable size="small">
            <el-option
              v-for="dict in dict.type.pet_deworming_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="狗证情况" prop="doglicenseStatus">
          <el-select v-model="queryParams.doglicenseStatus" placeholder="请选择狗证情况" clearable size="small">
            <el-option
              v-for="dict in dict.type.pet_dog_license_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="健康情况" prop="healthStatus">
          <el-select v-model="queryParams.healthStatus" placeholder="请选择健康情况" clearable size="small">
            <el-option
              v-for="dict in dict.type.pet_health_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="宠物主人" prop="owner" >
          <el-input
            v-model="queryParams.owner"
            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-button :icon="showMoreCondition ? 'el-icon-arrow-up' : 'el-icon-arrow-down'" size="mini" @click="showMoreCondition = !showMoreCondition">{{showMoreCondition ? '收起条件' : '展开条件'}}</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="['mall:pmsPet: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-show=false
          v-hasPermi="['mall:pmsPet: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-show=false
          v-hasPermi="['mall:pmsPet: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="['mall:pmsPet:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pmsPetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="宠物ID" align="center" prop="id" v-if="columns[0].visible"/>
      <el-table-column label="宠物名称" align="center" prop="name" v-if="columns[1].visible"/>
      <el-table-column label="宠物类型" align="center" prop="petType" v-if="columns[2].visible"/>
      <el-table-column label="宠物性别" align="center" prop="gender" v-if="columns[3].visible"/>
      <el-table-column label="出生年月" align="center" prop="birthDate" width="180" v-if="columns[4].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthDate, '{y}-{m}')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="体重" align="center" prop="weight" v-if="columns[5].visible"/>
      <el-table-column label="照片" align="center" prop="photo" v-if="columns[6].visible">
      <template slot-scope="{ row }">
        <el-image v-if="row.photo" :src="row.photo" :preview-src-list="[row.photo]" class="small-img"/>
      </template>
      </el-table-column>
      <el-table-column label="品种" align="center" prop="breed" v-if="columns[7].visible"/>
      <el-table-column label="体型" align="center" prop="bodyType" v-if="columns[8].visible"/>
      <el-table-column label="性格特征" align="center" prop="personality" v-if="columns[9].visible"/>
      <el-table-column label="疫苗情况" align="center" prop="vaccineStatus" v-if="columns[10].visible"/>
      <el-table-column label="驱虫情况" align="center" prop="dewormingStatus" v-if="columns[11].visible"/>
      <el-table-column label="狗证情况" align="center" prop="doglicenseStatus" v-if="columns[12].visible"/>
      <el-table-column label="健康情况" align="center" prop="healthStatus" v-if="columns[13].visible"/>
      <el-table-column label="宠物主人" align="center" prop="owner" v-if="columns[14].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="['mall:pmsPet:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-show=false
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:pmsPet: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="name">
          <el-input v-model="form.name" placeholder="请输入宠物名称" />
        </el-form-item>
        <el-form-item label="宠物类型" prop="petType">
          <el-select v-model="form.petType" placeholder="请选择宠物类型" clearable size="small">
            <el-option
              v-for="dict in dict.type.pet_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="宠物性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio label="男生">男生</el-radio>
            <el-radio label="女生">女生</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="出生年月" prop="birthDate">
          <el-date-picker clearable size="small"
                          v-model="form.birthDate"
                          type="month"
                          value-format="yyyy-MM"
                          placeholder="选择出生年月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="体重" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入体重" type="number" step="0.01" />
        </el-form-item>
        <el-form-item label="宠物照片" prop="pic">
          <oss-image-upload v-model="form.photo" :limit="1"></oss-image-upload>
        </el-form-item>

        <el-form-item label="品种" prop="breed">
          <el-input v-model="form.breed" placeholder="请输入品种" />
        </el-form-item>
        <el-form-item label="体型" prop="bodyType">
          <!--          <el-select v-model="form.bodyType" placeholder="请选择体型">-->
          <!--            <el-option label="请选择字典生成" value="" />-->
          <!--          </el-select>-->
          <el-input v-model="form.bodyType" placeholder="请输入体型" />
        </el-form-item>
        <el-form-item label="性格特征" prop="personality">
          <el-select v-model="form.personality" multiple  placeholder="请选择性格特征">
            <el-option
              v-for="dict in dict.type.pet_personality"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="疫苗情况">
          <el-select v-model="form.vaccineStatus" placeholder="请选择疫苗情况">
            <el-option
              v-for="dict in dict.type.pet_vaccine_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="绝育情况">
          <el-select v-model="form.sterilization" placeholder="请选择绝育情况">
            <el-option
              v-for="dict in dict.type.pet_sterilization"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="驱虫情况">
          <el-select v-model="form.dewormingStatus" placeholder="请选择驱虫情况">
            <el-option
              v-for="dict in dict.type.pet_deworming_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="狗证情况">
          <el-select v-model="form.doglicenseStatus" placeholder="请选择狗证情况">
            <el-option
              v-for="dict in dict.type.pet_dog_license_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="健康情况">
          <el-select v-model="form.healthStatus" multiple  placeholder="请选择驱虫情况">
            <el-option
              v-for="dict in dict.type.pet_health_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="宠物主人" prop="owner"  v-show=false>
          <el-input v-model="form.owner" 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 { listPmsPet, getPmsPet, delPmsPet, addPmsPet, updatePmsPet, exportPmsPet } from "@/api/ums/pet";
import { listPmsProduct } from '@/api/pms/product'

export default {
  name: "pet",
  dicts: ['pet_type','pet_vaccine_status','pet_deworming_status','pet_dog_license_status','pet_health_status','pet_sterilization','pet_personality'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 宠物档案表格数据
      pmsPetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        petType: null,
        gender: null,
        birthDate: null,
        weight: null,
        photo: null,
        breed: null,
        bodyType: null,
        personality: null,
        vaccineStatus: null,
        dewormingStatus: null,
        sterilization: null,
        doglicenseStatus: null,
        healthStatus: null,
        owner: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "宠物名称不能为空", trigger: "blur" }
        ],
      },
      columns: [
        { key: 1, label: "宠物ID", visible:  true  },
        { key: 2, label: "宠物名称", visible:  true  },
        { key: 3, label: "宠物类型", visible:  true  },
        { key: 4, label: "宠物性别", visible:  true  },
        { key: 5, label: "出生年月", visible:  true  },
        { key: 6, label: "体重", visible:  true  },
        { key: 7, label: "照片路径", visible:  true  },
        { key: 8, label: "品种", visible:  true  },
        { key: 9, label: "体型", visible:  true  },
        { key: 10, label: "性格特征", visible:  true  },
        { key: 11, label: "疫苗情况", visible:  true  },
        { key: 12, label: "驱虫情况", visible:  true  },
        { key: 13, label: "狗证情况（只针对狗狗）", visible:  true  },
        { key: 14, label: "健康情况", visible:  true  },
        { key: 15, label: "宠物主人", visible:  true  },
      ],
      showMoreCondition: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询宠物档案列表 */
    getList() {
      // this.loading = true;
      // listPmsPet(this.queryParams).then(response => {
      //   this.pmsPetList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
      this.loading = true;
      const {pageNum, pageSize} = this.queryParams;
      const query = {...this.queryParams, pageNum: undefined, pageSize: undefined};
      const pageReq = {page: pageNum - 1, size: pageSize};
      debugger
      listPmsPet(query, pageReq).then(response => {
        const { content, totalElements } = response
        this.pmsPetList = content;
        this.total = totalElements;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        petType: null,
        name: null,
        gender: null,
        birthDate: null,
        weight: null,
        photo: null,
        breed: null,
        bodyType: null,
        personality: [],
        vaccineStatus: "",
        dewormingStatus: "",
        doglicenseStatus: "",
        sterilization: "",
        healthStatus: [],
        owner: 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.open = true;
      this.title = "添加宠物档案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPmsPet(id).then(response => {
        this.form = response;
        this.open = true;
        this.title = "修改宠物档案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePmsPet(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            debugger
            addPmsPet(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 delPmsPet(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ums/pet/export', {
        ...this.queryParams
      }, `pet_${new Date().getTime()}.xlsx`)
      // const queryParams = this.queryParams;
      // this.$modal.confirm('是否确认导出所有宠物档案数据项？').then(() => {
      //   this.exportLoading = true;
      //   return exportPmsPet(queryParams);
      // }).then(response => {
      //   this.download(response.msg);
      //   this.exportLoading = false;
      // }).catch(() => {});
    }
  }
};
</script>
