<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
      @submit.native.prevent
    >
      <el-form-item label="物资柜" prop="loanFromId">
        <el-select
          v-model="queryParams.loanFromId"
          placeholder="请选择物资柜"
          clearable
        >
          <el-option
            v-for="dict in cabinets"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物资名称" prop="materialsName">
        <el-input
          v-model="queryParams.materialsName"
          placeholder="请输入物资名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归还人" prop="restitutionUserName">
        <el-input
          v-model="queryParams.restitutionUserName"
          placeholder="请输入归还人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.material_exception_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="异常发生时间">
        <el-date-picker
          style="width: 215px"
          :default-time="['00:00:00', '23:59:59']"
          v-model="createTime"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
          @change="handleClearTime"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="异常解除时间">
        <el-date-picker
          style="width: 215px"
          :default-time="['00:00:00', '23:59:59']"
          v-model="handleTime"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
          @change="handleClearTime2"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          v-no-more-click
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button
          v-no-more-click
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="ExceptionList"

    >
      <el-table-column label="编号" align="center" prop="misplaceId">
      </el-table-column>
      <el-table-column label="物资柜" align="center" prop="loanFromName" />

      <el-table-column label="物资名称" align="center" prop="materialsName">
      </el-table-column>
      <el-table-column label="归还柜" align="center" prop="restitutionToName">
      </el-table-column>
      <el-table-column label="归还人" align="center" prop="restitutionUserName">
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.material_exception_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column
        label="异常发生时间"
        align="center"
        prop="occurTime"
      >
      </el-table-column>
      <el-table-column label="异常解除时间" align="center" prop="handleTime">
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

  </div>
</template>

<script>

import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import Template from '@/views/print/printtemplate/list.vue'
import { MaterialsLoanExceptionPage } from '@/api/mes/material/exception'
import { getIsMaterialsCabinets } from '@/api/mes/material/information'
export default {
  name: 'Team',
  components: { Template, Treeselect },

  dicts: ['material_exception_status'],
  data() {
    return {
      //自动生成编码
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      codes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 班组表格数据
      ExceptionList: [],
      // mars树选项
      marsOptions: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      createTime: '',
      handleTime:'',
      queryParams: {
        current: 1,
        size: 10,
        exceptionType:'0',
        loanFromId:'',
        materialsName:'',
        restitutionUserName:'',
        status:'',
        startTime:'',
        endTime:'',
        handleStartTime:"",
        handleEndTime:'',
      },
      title:'',
      // 表单参数
      form: {},
      cabinets: [], //物资所属柜
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },

    }
  },

  created() {
    this.getList()
    this.materialsCabinets()
  },

  methods: {
    formatDate(date) {
      const year = date.getFullYear().toString().padStart(2, "0");
      const month = (date.getMonth() + 1).toString().padStart(2, "0");
      const day = date.getDate().toString().padStart(2, "0");
      const hours = date.getHours().toString().padStart(2, "0");
      const minutes = date.getMinutes().toString().padStart(2, "0");
      const seconds = date.getSeconds().toString().padStart(2, "0");

      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    /** 查询物资信息列表 */
    getList() {
      this.loading = true
      if (Array.isArray(this.createTime) && this.createTime.length === 2) {
        this.queryParams.startTime = this.formatDate(this.createTime[0])
        this.queryParams.endTime = this.formatDate(this.createTime[1])
      }
      if(Array.isArray(this.handleTime) && this.handleTime.length === 2) {
        this.queryParams.handleStartTime=this.formatDate(this.handleTime[0])
        this.queryParams.handleEndTime=this.formatDate(this.handleTime[1])
      }
      const data={
        ...this.queryParams,
      }
      MaterialsLoanExceptionPage(data).then((response) => {
        console.log(response, '获取物资归还异常allList ')
        this.ExceptionList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    materialsCabinets() {
      const data1 = {
        pasge: 1,
        size: -1
      }
      getIsMaterialsCabinets(data1).then((response) => {
        if (response?.data?.records) {
          this.cabinets = response.data.records.map((item) => ({
            value: item.cabinetId,
            label: item.cabinetName
          }))
        }
      })
    },
// 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {

      this.resetForm('form')
      this.autoGenFlag = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.size = 10
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.startTime = ''
      this.queryParams.endTime = ''
      this.createTime = ''
      this.handleTime = ''
      this.queryParams.handleStartTime = ''
      this.queryParams.handleEndTime = ''
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleClearTime(value){
      if(value==null){
        this.queryParams.startTime=null
        this.queryParams.endTime=null
      }
    },
    handleClearTime2(value){
      if(value==null){
        this.queryParams.handleStartTime=null
        this.queryParams.handleEndTime=null
      }
    },



  }
}
</script>
<style lang="scss" src="@/assets/styles/dialog-title.scss" scoped>
.el-input-width {
  width: 380px !important;

}
</style>
