<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="物料名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物料名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月/周数据" prop="monthOrWeek">
        <el-radio-group v-model="queryParams.monthOrWeek">
          <el-radio-button label="1">月</el-radio-button>
          <el-radio-button label="2">周</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="周期" prop="recordDate">
        <div v-if="queryParams.monthOrWeek==='1'">
          <el-date-picker
            v-model="queryParams.recordDate"
            type="month"
            value-format="yyyy-MM"
            placeholder="选择月份"
            clearable>
          </el-date-picker>
        </div>
        <div v-if="queryParams.monthOrWeek==='2'">
          <el-date-picker
            v-model="queryParams.recordDate"
            type="week"
            format="yyyy 第 WW 周"
            placeholder="选择周"
            clearable>
          </el-date-picker>
        </div>
      </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" style="margin-bottom: 8px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['material:material:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['material:material:add']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table :data="materialList"
              border
              stripe
              size="mini">
      <el-table-column label="编号" prop="id"/>
      <el-table-column label="物料名称" prop="materialName" :show-overflow-tooltip="true"/>
      <el-table-column label="规格" prop="specification" :show-overflow-tooltip="true"/>
      <el-table-column label="品牌/货号" prop="articleNo" :show-overflow-tooltip="true"/>
      <el-table-column label="厂商" prop="factory"/>
      <el-table-column label="单位" prop="unit"/>
      <el-table-column label="领取量" prop="num"/>
      <el-table-column label="物料类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.materialType==='1'" type="warning" size="mini">生产</el-tag>
          <el-tag v-else type="info" size="mini">非生产</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="领用人" prop="receiveUserName"/>
      <el-table-column label="周期" prop="recordDate" width="150"/>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSplit(scope.row)"
            v-if="scope.row.monthOrWeek==='1'"
            v-hasPermi="['material:material:add']"
          >周数据
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['material:material:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['material:material:delete']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top: 10px;text-align: center">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pageNum"
        :page-sizes="[10, 20, 50]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!-- 添加或修改物料配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="数据周期">
          <div v-if="form.monthOrWeek==='1'">
            <el-date-picker
              v-model="form.recordDate"
              type="month"
              value-format="yyyy-MM"
              placeholder="选择月份"
              :picker-options="{ disabledDate: time => time.getTime() < Date.now() }"
              clearable>
            </el-date-picker>
          </div>
          <div v-else>
            <el-date-picker
              v-model="form.recordDate"
              type="week"
              format="yyyy 第 WW 周"
              placeholder="选择周"
              :picker-options="{ disabledDate: time => time.getTime() < Date.now() }"
              clearable>
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物料名称" :disabled="form.monthOrWeek==='2'"/>
        </el-form-item>
        <el-form-item label="物料规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入物料规格" :disabled="form.monthOrWeek==='2'"/>
        </el-form-item>
        <el-form-item label="品牌/货号" prop="articleNo">
          <el-input v-model="form.articleNo" placeholder="请输入品牌/货号" :disabled="form.monthOrWeek==='2'"/>
        </el-form-item>
        <el-form-item label="厂商" prop="factory">
          <el-input v-model="form.factory" placeholder="请输入厂商" :disabled="form.monthOrWeek==='2'"/>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" :disabled="form.monthOrWeek==='2'"/>
        </el-form-item>
        <el-form-item label="领取量" prop="num">
          <el-input-number v-model="form.num" controls-position="right" :min="0" placeholder="请输入领取量"/>
        </el-form-item>
        <el-form-item label="物料类型" prop="materialType">
          <el-radio-group v-model="form.materialType" :disabled="form.monthOrWeek==='2'">
            <el-radio label="1">生产</el-radio>
            <el-radio label="2">非生产</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea"/>
        </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 {
  listMaterial,
  getMaterial,
  delMaterial,
  addMaterial,
  updateMaterial,
  exportMaterial
} from "@/api/material/material";
import DateUtil from "@/utils/date"

export default {
  name: "Receive",
  data() {
    return {
      // 总条数
      total: 0,
      // 物料表格数据
      materialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialName: undefined,
        factory: undefined,
        monthOrWeek: undefined,
        recordDate: undefined
      },
      // 表单参数
      form: {},

      // 表单校验
      rules: {
        materialName: [
          {required: true, message: "物料名称不能为空", trigger: "blur"}
        ],
        specification: [
          {required: true, message: "物料规格不能为空", trigger: "blur"}
        ],
        articleNo: [
          {required: true, message: "品牌/货号不能为空", trigger: "blur"}
        ],
        factory: [
          {required: true, message: "厂商不能为空", trigger: "blur"}
        ],
        unit: [
          {required: true, message: "单位不能为空", trigger: "blur"}
        ],
        num: [
          {required: true, message: "领取量不能为空", trigger: "blur"}
        ],
        materialType: [
          {required: true, message: "物料类型不能为空", trigger: "change"}
        ],
        monthOrWeek: [
          {required: true, message: "月/周数据不能为空", trigger: "change"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料列表 */
    getList() {
      listMaterial(this.queryParams).then(res => {
        this.materialList = res.data.records;
        this.total = res.data.total;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        materialName: undefined,
        specification: undefined,
        articleNo: undefined,
        factory: undefined,
        unit: undefined,
        num: undefined,
        materialType: undefined,
        monthOrWeek: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.handleQuery();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.monthOrWeek = '1'
      this.title = "添加月物料计划";
      this.open = true;
    },
    handleExport() {
      exportMaterial(this.queryParams)
    },
    /** 周数据填报操作 */
    handleSplit(row) {
      this.form = row;
      this.form.parentId = row.id;
      this.form.id = undefined;
      this.form.num = undefined;
      this.form.monthOrWeek = '2'
      this.form.recordDate = DateUtil.day()
      this.title = "添加周物料计划";
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getMaterial(row.id).then(res => {
        this.form = res.data;
        this.open = true;
        this.title = "修改物料计划";
      });
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateMaterial(this.form).then(res => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaterial(this.form).then(res => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除编号为"' + row.id + '"的数据项？').then(() => {
        return delMaterial(row.id);
      }).then(() => {
        this.getList();
        this.$message.success("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>
