<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <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="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </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="['his:packageSolarTerm: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="['his:packageSolarTerm: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="['his:packageSolarTerm:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table height="600" v-loading="loading" border :data="solarTermList" @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="name" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180"/>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180"/>
      <el-table-column label="节气简介" align="center" prop="desc" width="300">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.desc" placement="bottom-start">
            <div style="overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
              {{ scope.row.desc }}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="更改时间" align="center" prop="updateTime" width="180"/>
      <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="['his:packageSolarTerm:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['his:packageSolarTerm: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="addOrUpdateDialog.title" :visible.sync="addOrUpdateDialog.open" width="1000px" append-to-body>
      <el-form ref="form" :model="addOrUpdateDialog.form" :rules="addOrUpdateDialog.rules" label-width="110px">
        <el-form-item label="节气名称" prop="name">
          <el-input v-model="addOrUpdateDialog.form.name" placeholder="请输入节气名称" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            v-model="addOrUpdateDialog.form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="addOrUpdateDialog.form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="节气简介" prop="desc">
          <el-input
            type="textarea"
            :rows="5"
            v-model="addOrUpdateDialog.form.desc"
            placeholder="请输入节气简介" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number :min="0" v-model="addOrUpdateDialog.form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="addOrUpdateDialog.form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </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 {
  addPackageSolarTerm,
  delPackageSolarTerm,
  getPackageSolarTerm,
  listPackageSolarTerm,
  updatePackageSolarTerm
} from '@/api/his/packageSolarTerm'

export default {
  name: 'solarTerm',
  data() {
    return {
      showSearch: true,
      statusOptions: [],
      multiple: true,
      single: true,
      solarTermIds: [],
      loading: true,
      queryParams: {
        name: null,
        status: null
      },
      solarTermList: [],
      total: 0,
      addOrUpdateDialog: {
        open: false,
        title: null,
        form: {
          id: null,
          name: null,
          startTime: null,
          endTime: null,
          desc: null,
          sort: null,
          status: "1",
        },
        rules: {
          name: [
            { required: true, message: '请输入节气名称', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '请输入开始时间', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '请输入结束时间', trigger: 'blur' }
          ],
          desc: []
        }
      }
    }
  },
  created() {
    this.getDicts("solar_Term_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listPackageSolarTerm(this.queryParams).then(response => {
        const {code, rows, total, msg} = response
        if (code === 200) {
          this.solarTermList = rows
          this.total = total
          this.loading = false
          return
        }
        this.msgError(msg)
      });
    },
    handleAdd() {
      this.reset()
      this.addOrUpdateDialog.title = "添加节气";
      this.addOrUpdateDialog.open = true;
    },

    handleUpdate(row) {
      this.reset()
      const id = row.id || this.solarTermIds
      getPackageSolarTerm(id).then(response => {
        this.addOrUpdateDialog.form = response.data;
        this.addOrUpdateDialog.form.status = response.data.status?.toString();
        this.addOrUpdateDialog.title = "修改节气";
        this.addOrUpdateDialog.open = true;
      })
    },
    handleDelete(row) {
      const ids = row.id || this.solarTermIds;
      this.$confirm('是否确认删除节气编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delPackageSolarTerm(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleSelectionChange(selection) {
      this.solarTermIds = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return
        }

        if (this.addOrUpdateDialog.form.id != null) {
          updatePackageSolarTerm(this.addOrUpdateDialog.form).then(response => {
            const {code,msg} = response
            if (code !== 200) {
              this.msgError(msg)
              return
            }
            this.msgSuccess("修改成功");
            this.addOrUpdateDialog.open = false;
            this.getList();
          });
          return
        }

        addPackageSolarTerm(this.addOrUpdateDialog.form).then(response => {
          const {code,msg} = response
          if (code !== 200) {
            this.msgError(msg)
            return
          }
          this.msgSuccess("新增成功");
          this.addOrUpdateDialog.open = false;
          this.getList();
        });
      });
    },
    cancel() {
      this.addOrUpdateDialog.open = false;
      this.reset();
    },
    reset() {
      this.addOrUpdateDialog.form = {
        name: null,
        startTime: null,
        endTime: null,
        desc: null,
        sort: null,
        status: "1",
      };
      this.resetForm("form");
    },
  },
}
</script>

<style scoped>

</style>
