<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">

      <el-form-item label="样品名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入样品名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="工单号" prop="workOrderCodeSap">
        <el-input
          v-model="queryParams.workOrderCodeSap"
          placeholder="请输入工单号"
          clearable
        />
      </el-form-item>


      <el-form-item label="留样时间" prop="querySampleDate">
        <el-date-picker
          v-model="querySampleDate"
          format="yyyy-MM-dd"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport()"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['qct:sampleSign:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" >
      <el-table-column label="id" align="center" prop="id" v-if="false"/>

      <el-table-column label="样品名称" align="center" prop="materialName" />
      <el-table-column label="生产批号" align="center" prop="incomeBatchNo" />
      <el-table-column label="留样日期" align="center" prop="sampleTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sampleTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="留样数量" align="center" prop="sampleNum" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="取样人" align="center" prop="sampleUserName" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="取样地点" align="center" prop="gainSampleAddress" width="200" :show-overflow-tooltip="true" />


    </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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="样品名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入样品名称" />
        </el-form-item>
        <el-form-item label="工单号" prop="workOrderCodeSap">
          <el-input v-model="form.workOrderCodeSap" placeholder="请输入工单号" />
        </el-form-item>
        <el-form-item label="生产批号" prop="incomeBatchNo">
          <el-input v-model="form.incomeBatchNo" placeholder="请输入生产批号" />
        </el-form-item>
        <el-form-item label="留样日期" prop="sampleTime">
          <el-date-picker clearable
                          v-model="form.sampleTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择留样日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="留样数量" prop="sampleNum">
          <el-input v-model="form.sampleNum" placeholder="请输入留样数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = !open">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script >
import {parseTime} from "@/utils/ruoyi";
import {addQcCheckSampleSignTask, qcCheckSampleSignList} from "@/api/quality/qcSampleTask";

export default {
  data(){
    return{
      showSearch:true,
      queryParams:{
        pageNum: 1,
        pageSize: 10,
        materialName:undefined,
        workOrderCodeSap:undefined,
        startSampleDate:parseTime(new Date(),"{y}-{m}-{d}"),
        endSampleDate:parseTime(new Date(),"{y}-{m}-{d}")
      },
      loading: true,
      list:[],
      total:0,
      querySampleDate:[parseTime(new Date(),"{y}-{m}-{d}"),parseTime(new Date(),"{y}-{m}-{d}")],
      open: false,
      form:{

      },
      title:"",
      rules:[]
    }
  },
  methods : {
    parseTime,
    getList() {
      this.loading = true
      qcCheckSampleSignList(this.queryParams).then(res=>{
        this.list = res.rows;
        this.total = res.total;
        this.loading = false;
      })
    },
    handleQuery(){
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery(){
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('quality/qc-check-sample-task/sign/export', {
        ...this.queryParams
      }, `留样登记表${new Date().getTime()}.xlsx`)
    },

    handleAdd(){
      this.title="新增留样登记"
      this.open = true
      this.form = {}
    },
    submitForm(){
      addQcCheckSampleSignTask(this.form).then(()=>{
        this.$notify({
          title: '新增成功',
          type: 'success',
          duration: 1500
        })
        this.open = false
        this.getList()
      })
    }
  },
  created() {
    this.getList();
  },

  watch : {
    querySampleDate(dateArray){
      if (!dateArray){
        this.queryParams.startSampleDate = null
        this.queryParams.endSampleDate = null
      }else {
        this.queryParams.startSampleDate = parseTime(dateArray[0],"{y}-{m}-{d}")
        this.queryParams.endSampleDate = parseTime(dateArray[1],"{y}-{m}-{d}")
      }
    }
  }

}
</script>

<style scoped lang="scss">

</style>
