<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="productionType">
          <el-select style="width: 100%;" filterable v-model="queryParams.productionType">
            <el-option v-for="item in dict.type.inventory_production_type" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
        <!--        <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>
          <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:test: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:test: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" align="center" />
        <el-table-column label="生产类型" align="center" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.inventory_production_type" :value="scope.row.productionType"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="生产车间" align="center" prop="productionPlantName"></el-table-column>
        <el-table-column label="物料编码" align="center" prop="goodsNum"></el-table-column>
        <el-table-column label="物料名称" align="center" prop="goodsName"></el-table-column>
        <el-table-column label="品种" align="center" prop="varietyName"></el-table-column>
        <el-table-column label="级别" align="center" prop="levelName"></el-table-column>
        <el-table-column label="加工工艺" align="center" prop="process"></el-table-column>
        <el-table-column label="毛色" align="center" prop="coatName"></el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="大标小" align="center" prop="shape" >
          <template slot-scope="scope">
            <!-- 兼容 shape 为 number 或 string（1/2/3 或 '1'/'2'/'3'） -->
            <el-tag v-if="scope.row.shape == 1" type="primary">大</el-tag>
            <el-tag v-else-if="scope.row.shape == 2" type="success">标</el-tag>
            <el-tag v-else-if="scope.row.shape == 3" type="info">小</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center" 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:test:edit']"
              style="margin-right: 8px"
            >修改</el-button>
            <el-popconfirm
              v-hasPermi="['app:test: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" :model="form" :rules="rules" :disabled="isView" label-width="100px">
        <el-form-item label="生产类型" prop="productionType">
          <el-select style="width: 100%;" filterable v-model="form.productionType" placeholder="请选择生产类型">
            <el-option v-for="item in dict.type.inventory_production_type" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生产车间" prop="productionPlantNo">
          <el-select v-model="form.productionPlantNo" style="width: 100%;" filterable placeholder="请选择生产车间" @change="changePlant">
            <el-option
              v-for="item in productionPlantList"
              :key="item.productionPlantNo"
              :value="item.productionPlantNo"
              :label="item.productionPlantName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="物料编码" prop="goodsNum">
          <el-input
            v-model="form.goodsNum"
            placeholder="请选择物料编码"
            readonly
          >
            <template #append>
              <el-tooltip class="item" effect="dark" content="物料速查" placement="top">
                <el-button type="success" icon="el-icon-search" circle size="mini" :disabled="isView" @click="openQueryMaterial"></el-button>
              </el-tooltip>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="物料名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="选择物料后自动带出" readonly />
        </el-form-item>
        <el-form-item label="品种" prop="varietyNo">
          <el-select v-model="form.varietyNo" style="width: 100%;" filterable >
            <el-option v-for="item in varietyList" :key="item.varietyNo" :value="item.varietyNo" :label="item.varietyName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="级别" prop="levelNo">
          <el-select v-model="form.levelNo" style="width: 100%;" filterable>
            <el-option v-for="item in levelList" :key="item.levelNo" :value="item.levelNo" :label="item.levelName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="加工工艺" prop="process">
          <el-select v-model="form.process" style="width: 100%;" filterable >
            <el-option v-for="item in 12" :key="item" :value="item" :label="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="毛色" prop="coatNo">
          <el-select v-model="form.coatNo" style="width: 100%;" filterable >
            <el-option v-for="item in coatList" :key="item.coatNum" :value="item.coatNum" :label="item.coatName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark"  placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="大标小" prop="shape">
          <el-select  v-model="form.shape" style="width: 100%;" filterable >
            <el-option :value="1" label="大"></el-option>
            <el-option :value="2" label="标准"></el-option>
            <el-option :value="3" label="小"></el-option>
          </el-select>
        </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>
      <query-material ref="query-material" :is-multiple="false" @selectList="getSelectList"></query-material>
    </el-dialog>
  </div>
</template>
<script>
import {formatMoney, getFindByKey} from "../../../utils";
import queryMaterial from "../../components/queryMaterial.vue";
import { getVarietyList } from "../../../api/baseInfo/variety";
import { getLevelList } from "../../../api/baseInfo/level";
import { getCoatList } from "../../../api/baseInfo/coat";
import { getRelatedPage, addRelated, editRelated, delRelated, getRelatedById } from "../../../api/production/related";
import { getProductionPlantList } from "../../../api/production/productionPlant";


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

          // 禁用今天之后的日期
          return time.getTime() > today.getTime();
        }
      },
      // 表单参数
      form: {},
      /** 弹窗表单校验：生产类型、生产车间、物料编码必填；物料名称只读由模板控制 */
      rules: {
        productionType: [{ required: true, message: "请选择生产类型", trigger: "change" }],
        productionPlantNo: [{ required: true, message: "请选择生产车间", trigger: "change" }],
        goodsNum: [{ required: true, message: "请选择物料编码", trigger: "change" }],
      },
      varietyList: [],
      levelList: [],
      coatList: [],
      productionPlantList: [],
    }
  },
  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.varietyNo": {
      handler(newVal) {
        this.changeVariety(newVal)
      },
      deep: true
    },
    "form.levelNo": {
      handler(newVal) {
        this.changeLevel(newVal)
      },
      deep: true
    },
    "form.coatNo": {
      handler(newVal) {
        this.changeCoat(newVal)
      },
      deep: true
    }
  },
  created() {
    this.getCoat()
    this.getLevel()
    this.getVariety()
    this.getList();
    this.getProductionPlant();
  },
  methods: {
    getMoney(val) {
      return formatMoney(val)
    },
    goNext(event, nextRef) {
      event.preventDefault();
      this.$refs[nextRef].focus();
    },
    /** 生产车间 **/
    getProductionPlant() {
      getProductionPlantList({}).then(res => {
        this.productionPlantList = res.data
      })
    },
    changePlant(val) {
      let item = getFindByKey(this.productionPlantList, val, 'productionPlantNo')
      if(item) {
        this.form.productionPlantName = item.productionPlantName
      }
    },
    /** 级别 **/
    getLevel() {
      getLevelList({}).then(res => {
        this.levelList = res.data
      })
    },
    changeLevel(val) {
      let item = getFindByKey(this.levelList, val, 'levelNo')
      if(item) {
        this.form.levelName = item.levelName
      }
    },
    /** 品种 **/
    getVariety() {
      getVarietyList({}).then(res => {
        this.varietyList = res.data
      })
    },
    changeVariety(val) {
      let item = getFindByKey(this.varietyList, val, 'varietyNo')
      if(item) {
        this.form.varietyName = item.varietyName
      }
    },
    /** 毛色 **/
    getCoat() {
      getCoatList({}).then(res => {
        this.coatList = res.data.filter(item => item.coatStatus === 1)
      })
    },
    changeCoat(val) {
      let item = getFindByKey(this.coatList, val, 'coatNum')
      if(item) {
        this.form.coatName = item.coatName
      }
    },
    /** 查询检验项目列表 */
    getList() {
      getRelatedPage(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.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        productionType: null,
        timeRange: null,
        startTime: null,
        endTime: null,
      }
      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
      }
      getRelatedById(params).then(res => {
        this.form = res.data;
        this.open = true;
        this.isView = false;
        this.title = "编辑"
      })
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id;
      let params = {
        id: id
      }
      getRelatedById(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,
        productionType: null,
        productionPlantNo: null,
        productionPlantName: null,
        goodsNum: null,
        goodsName: null,
        varietyNo: null,
        varietyName: null,
        levelNo: null,
        levelName: null,
        process: null,
        coatName: null,
        coatNo: null,
        remark: null,
        shape: null
      };
      this.resetForm("form");
    },
    // 打开物料速查表
    openQueryMaterial(index) {
      this.$refs['query-material'].open()
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            // 编辑
            editRelated(this.form).then(res => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          } else {
            // 新增
            addRelated(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      let params = {
        ids: ids
      }
      delRelated(params).then(res => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      })
    },
    /** 删除按钮操作 */
    handleDeleteAll() {
      const ids = this.ids;
      this.$modal.confirm('确定要删除这条数据吗？').then(() => {
        let params = {
          ids: ids.join(',')
        }
        delRelated(params).then(res => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
          this.$refs.multipleTable.clearSelection();
        })
      })
    },
    getSelectList(list) {
      this.form.goodsNum = list[0].goodsNum
      this.form.goodsName = list[0].goodsName
      this.form.varietyNo = list[0].variety
      this.form.varietyName = list[0].varietyName
      this.form.levelNo = list[0].goodsLevel
    }
  }
}
</script>

<style scoped lang="scss">

</style>

