<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
        <el-form-item label="领用日期" prop="timeRange">
          <el-date-picker
            v-model="queryParams.timeRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入库批次号" prop="materialBatch">
          <el-input
            v-model="queryParams.materialBatch"
            placeholder="请输入入库批次号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input
            v-model="queryParams.materialName"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!--      <el-form-item label="物料类型" prop="materialType">-->
        <!--        <el-select v-model="queryParams.materialType" placeholder="请选择物料类型">-->
        <!--          <el-option v-for="item in materialTypeList" :key="item.id" :value="item.id" :label="item.label"></el-option>-->
        <!--        </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="['app:hazardousOut:add']"
          >新增</el-button>
        </el-col>
        <!--      <el-col :span="1.5">-->
        <!--        <el-button-->
        <!--          type="danger"-->
        <!--          plain-->
        <!--          icon="el-icon-delete"-->
        <!--          size="mini"-->
        <!--          :disabled="multiple"-->
        <!--          @click="handleDeleteAll"-->
        <!--          v-hasPermi="['app:receive:remove']"-->
        <!--        >删除</el-button>-->
        <!--      </el-col>-->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-card>
    <br>
    <el-card>
      <el-table border stripe  v-loading="loading" ref="multipleTable" :data="disposalList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"  />
        <!--  <el-table-column label="ID"  prop="id" width="80" />  -->
        <el-table-column label="领用日期"  prop="outTime" >
          <template slot-scope="scope">
            {{changeDate(scope.row.outTime)}}
          </template>
        </el-table-column>
        <el-table-column label="物料批次号"  prop="materialBatch" />
        <el-table-column label="物料编号"  prop="materialNum" />
        <el-table-column label="物料名称"  prop="materialName" />
        <el-table-column label="物料类型"  prop="materialType" >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.materialType === 0">原材料</el-tag>
            <el-tag v-else-if="scope.row.materialType === 1" type="success">产品</el-tag>
            <el-tag v-else-if="scope.row.materialType === 2" type="info">包材</el-tag>
            <el-tag v-else-if="scope.row.materialType === 3" type="danger">危险化学品</el-tag>
            <el-tag v-else-if="scope.row.materialType === 4" type="warning">检验检疫试剂</el-tag>
            <el-tag v-else-if="scope.row.materialType === 5">消毒药剂</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="领用数量"  prop="stockAmount" >
          <template slot-scope="scope">
            <span>{{scope.row.stockAmount}} {{scope.row.unit}}</span>
          </template>
        </el-table-column>
        <el-table-column label="领用人"  prop="saveUser" />
        <el-table-column label="库管"  prop="keeper" />
        <el-table-column label="操作" width="180"  class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
            >查看</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['app:hazardousOut:edit']"
            >修改</el-button>
            <!--          <el-popconfirm-->
            <!--            v-hasPermi="['app:receive:remove']"-->
            <!--            :title="'是否确认删除所选数据项？'"-->
            <!--            @confirm="handleDelete(scope.row)"-->
            <!--          >-->
            <!--            <el-button-->
            <!--              size="mini"-->
            <!--              type="text"-->
            <!--              slot="reference"-->
            <!--              icon="el-icon-delete"-->
            <!--            >删除</el-button>-->
            <!--          </el-popconfirm>-->
          </template>
        </el-table-column>
      </el-table>

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

    <!-- 添加或修改死淘处理方式对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" label-position="left" :model="form" :rules="rules" :disabled="isView" label-width="100px">
        <el-form-item label="领用日期" prop="outTime">
          <el-date-picker
            v-model="form.outTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="物料批次号" prop="materialBatch">
          <el-select v-model="form.materialBatch" :disabled="form.id != null" placeholder="请选择物料批次号">
            <el-option v-for="item in materialList" :key="item.id" :value="item.materialBatch" :label="item.materialBatch+'('+ item.materialName +')'"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" disabled placeholder="选择物料自动填充"></el-input>
        </el-form-item>
        <el-form-item label="物料类型" prop="materialType">
          <el-select v-model="form.materialType" disabled placeholder="选择物料自动填充">
            <el-option v-for="item in materialTypeList" :key="item.id" :value="item.id" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="领用数量" prop="stockAmount">
          <el-input v-model="form.stockAmount" placeholder="请输入领用数量"></el-input>
        </el-form-item>
        <el-form-item label="计量单位" prop="unit">
          <el-input v-model="form.unit" disabled placeholder="选择物料自动填充"></el-input>
        </el-form-item>
        <el-form-item label="领用人" prop="saveUser">
          <el-input v-model="form.saveUser" show-word-limit maxlength="10" placeholder="请输入领用人"></el-input>
        </el-form-item>
        <el-form-item label="库管" prop="keeper">
          <el-input v-model="form.keeper" show-word-limit maxlength="10" placeholder="请输入库管"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!isView">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {changeDate, getFindByKey} from "../../../utils";
// import { getStockList } from "../../../api/app/stock";
// import { getReceivePage, getReceiveDetail, addReceivePage, editReceivePage, delReceivePage } from "../../../api/app/receive";
import { required, rules } from "../../../utils/formRules";
// import {getUserProfile} from "../../../api/system/user";


export default {
  name: "",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      disposalList: [],
      // 弹出层标题
      title: "",
      isView:false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        timeRange: null,
        materialBatch: null,
        materialName: null,
        materialType: 3,
        startTime: null,
        endTime: null,
        keeper: null
      },
      pickerOptions: {
        disabledDate(time) {
          // 获取当前日期
          const today = new Date();
          today.setHours(0, 0, 0, 0); // 将时间设置为当天的 00:00:00

          // 禁用今天之后的日期
          return time.getTime() > today.getTime();
        }
      },
      // 表单参数
      form: {
        id: null,
        outTime: changeDate(new Date()),
        materialBatch: null,
        materialName: null,
        materialType: null,
        saveUser: null,
        stockAmount: null,
        unit: null,
      },
      rules: {
        outTime: [required('领用日期不能为空')],
        materialBatch: [required('物料批次号不能为空')],
        materialName: [required('物料名称不能为空')],
        stockAmount: [
          required('领用数量不能为空'),
          rules.decimals
        ],
        unit: [required('计量单位不能为空')],
        saveUser: [required('领用人不能为空')],
        keeper: [required('库管不能为空')],
      },
      // 物料列表
      materialList: [],
      materialTypeList: [
        {
          id: 0,
          label: '原材料'
        },
        {
          id: 1,
          label: '产品'
        },
        {
          id: 2,
          label: '包材'
        },
        {
          id: 3,
          label: '危险化学品'
        },
        {
          id: 4,
          label: '检验检疫试剂'
        },
        {
          id: 5,
          label: '消毒药剂'
        },
      ],
      maxRules: 0,
      user: {}
    }
  },
  watch: {
    "queryParams.timeRange": {
      handler(newVal) {
        if (newVal) {
          this.queryParams.startTime = newVal[0]
          this.queryParams.endTime = newVal[1]
        } else {
          this.queryParams.startTime = null
          this.queryParams.endTime = null
        }
      },
      deep: true
    },
    "form.materialBatch": {
      handler(newVal) {
        if(newVal) {
          let item = getFindByKey(this.materialList, newVal, 'materialBatch')
          if(item) {
            this.form.materialName = item.materialName
            this.form.materialType = item.materialType
            this.form.unit = item.unit
          }
        } else {
          this.form.materialName = null
          this.form.materialType = null
          this.form.unit = null
        }
      },
      deep: true
    },
  },
  created() {
    this.loading = false
    // this.getUser()
    // this.getMaterial()
    // this.getList();
  },
  methods: {
    /** getUser **/
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
        this.form.saveUser = response.data.nickName
      });
    },
    validateNumber(rule, value, callback){
      let zhengshu = /^[1-9]\d*$/;
      if(!zhengshu.test(value)) {
        return callback(new Error('必须大于0，并且是整数'));
      }
      callback()
    },
    /** 物料列表 **/
    getMaterial() {
      let params = {
        materialBatch: this.form.materialBatch
      }
      getStockList(params).then(res => {
        let arr = []
        res.data.forEach(item => {
          if(item.materialType === 3) {
            arr.push(item)
          }
        })
        this.materialList = arr
      })
    },
    /** 查询检验项目列表 */
    getList() {
      getReceivePage(this.queryParams).then(res => {
        this.disposalList = res.data.records;
        this.total = res.data.total;
        this.loading = false;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        timeRange: null,
        materialBatch: null,
        materialName: null,
        materialType: 3,
        startTime: null,
        endTime: null
      }
      // 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.isView = false;
      this.title = "添加危化品领用登记";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      let params = {
        id: id
      }
      getReceiveDetail(params).then(res => {
        this.form = res.data;
        this.open = true;
        this.isView = false;
        this.title = "修改危化品领用登记";
        this.getMaterial()
      })
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id;
      let params = {
        id: id
      }
      getReceiveDetail(params).then(res => {
        this.form = res.data;
        this.open = true;
        this.isView = true;
        this.title = "查看危化品领用登记";
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        outTime: changeDate(new Date()),
        materialBatch: null,
        materialName: null,
        materialType: null,
        saveUser: this.user.nickName,
        stockAmount: null,
        unit: null,
      };
      this.resetForm("form");
      this.getMaterial()
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            // 编辑
            editReceivePage(this.form).then(res => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          } else {
            // 新增
            addReceivePage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      let params = {
        ids: ids
      }
      delReceivePage(params).then(res => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      })
    },
    /** 删除按钮操作 */
    handleDeleteAll() {
      const ids = this.ids;
      this.$modal.confirm('是否确认删除所选数据项？').then(() => {
        let params = {
          ids: ids.join(',')
        }
        delReceivePage(params).then(res => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
          this.$refs.multipleTable.clearSelection();
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>

