<template>
  <div class="app-container pageSediment">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="操作日期" prop="createdAt">
        <el-date-picker v-model="queryParams.createdAt" clearable placeholder="选择日期"
                        size="small"
                        style="width: 200px"
                        type="date"
                        value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="cyan" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          v-hasPermi="['resource:applyAction:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
        <el-button
          v-hasPermi="['resource:applyAction:export']"
          icon="el-icon-download"
          size="mini"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-form-item>
    </el-form>

    <el-table ref="multipleTable" v-loading="loading"
              :data="applyActionList" border stripe tooltip-effect="light" @row-click="handleRowClick"
              @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="left" fixed="left" label="申请单ID" min-width="160" prop="applyId" show-overflow-tooltip/>
      <el-table-column align="left" fixed="left" label="检查项目" min-width="260" prop="outExamItem" show-overflow-tooltip/>
      <el-table-column align="center" label="预约日期" prop="primaryDate" width="200"/>
      <el-table-column align="center" label="改约日期" prop="reservationDate" width="200"/>
      <el-table-column align="center" label="操作日期" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="left" label="备注" min-width="260" prop="note" show-overflow-tooltip/>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改预约操作记录对话框 -->
  </div>
</template>

<script>
import { listApplyAction, getApplyAction, delApplyAction, addApplyAction, updateApplyAction, exportApplyAction } from "@/api/resource/applyAction";

export default {
  name: "ApplyAction",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约操作记录表格数据
      applyActionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        createdAt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createdAt: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
     //初始化日期时间
    var myDate = new Date()
    var year = myDate.getFullYear()
    var month = myDate.getMonth() + 1
    var date=myDate.getDate();
    if(month < 10){
        month = "0"+month
    }
    if(date < 10){
        date = "0"+date
    }
    this.queryParams.createdAt = year+"-"+month+"-"+date;
    this.getList();
  },
  methods: {
    handleRowClick(row, column, event) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    /** 查询预约操作记录列表 */
    getList() {
      this.loading = true
      listApplyAction(this.queryParams).then(response => {
        this.applyActionList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        applyId: null,
        action: null,
        createdAt: null,
        createdById: null,
        createdByName: null,
        note: 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
      getApplyAction(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) {
            updateApplyAction(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApplyAction(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除预约操作记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delApplyAction(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有预约操作记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportApplyAction(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
