<template>
  <div class="app-container">
    <div class="app-container-search main-bgcolor main-radius">
      <h1 class="">检验检疫</h1>
      <br />
      <!-- 搜索工作栏 -->
      <SearchInfo
        ref="searchForm"
        :searchFormParam="{ queryParams , dataSource }"
        :showSearch.sync="showSearch"
        @handleQuery="handleQuery"
        @resetQuery="resetQuery"
      ></SearchInfo>
    </div>

    <div class="app-container-list main-bgcolor main-radius shopping">
      <div class="shopping-top">
        <div class="shopping-title">检验列表</div>
        <el-button class="shopping-add-btn" type="primary" plain @click="handleAdd"
                  v-hasPermi="['product:inspection:create']">新增</el-button>
      </div>

      <!-- 列表 -->
      <el-table v-loading="loading" :data="list">
        <el-table-column label="编号" align="center" type="index" width="80" >
          <template v-slot="{ $index }">
            <span>{{($index+1)+ (queryParams.pageNo-1)*queryParams.pageSize }}</span>
          </template>
        </el-table-column>
        <el-table-column label="入库批次" align="center" prop="harvestBatchId" >
          <template slot-scope="{ row }">
            <el-tooltip class="item" effect="dark" :content="row.harvestBatchId" placement="top">
              <div class="flex-acenter">
                <span class="more-width-20">{{row.harvestBatchId}}</span>
                <el-button type="text" icon="el-icon-copy-document" @click.stop="copyContent(row.harvestBatchId)"></el-button>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" align="center" prop="goodsName" />
        <el-table-column label="商品编码" align="center" prop="goodsId" >
          <template slot-scope="{ row }">
            <el-tooltip class="item" effect="dark" :content="row.goodsId" placement="top">
              <div class="flex-acenter">
                <span class="more-width-20">{{row.goodsId}}</span>
                <el-button type="text" icon="el-icon-copy-document" @click.stop="copyContent(row.goodsId)"></el-button>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="所属商户" align="center" prop="belongBusinessName" />
        <el-table-column label="生产日期" align="center" prop="harvestTime" width="170">
          <template v-slot="{ row }">
            <span>{{parseTime(row.harvestTime)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="检验人" align="center" prop="inspectionUser" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="170">
          <template v-slot="{ row }">
            <span>{{parseTime(row.createTime)}}</span>
          </template>
        </el-table-column>
        <!--
        <el-table-column label="上传状态" align="center" prop="blockState" >
          <template v-slot="{ row }">
            <el-tag
              :type="row.blockState==0?'info':row.blockState == 1?'success':'danger'">
              {{optionsBlockState[row.blockState].label}}
            </el-tag>
          </template>
        </el-table-column>
        -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template v-slot="{ row }">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(row,'update')"
                       v-hasPermi="['product:inspection:update']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)"
                       v-hasPermi="['product:inspection:delete']">删除</el-button>
                       <!--
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpChain(row)"
                       v-hasPermi="['product:inspection:update']" v-if="row.blockState == 0">上传</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleFreeze(row)"
                       v-hasPermi="['product:inspection:update']" v-if="row.blockState == 1">冻结</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleActive(row)"
                       v-hasPermi="['product:inspection:update']" v-if="row.blockState == 2">激活</el-button>
                      -->
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                  @pagination="getList"/>
    </div>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="900px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" label-position="top">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所属商户" prop="belongBusinessId">
              <el-select ref="business" class="w100" v-model="form.belongBusinessId" :disabled="isDisable" placeholder="请选择所属商户" style="flex: 1;"  @change="changeBusinessId">
                <el-option v-for="businessInfo in businessNameList"
                :key="businessInfo.id" :label="businessInfo.businessName" :value="businessInfo.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品名称" prop="goodsId">
              <el-select ref="goods" class="w100" v-model="form.goodsId" :disabled="isDisable" placeholder="请先选择所属商户" style="flex: 1;"  @change="changeGoods">
                <el-option v-for="item in goodsNameList"
                :key="item.goodsId" :label="item.goodsName" :value="item.goodsId"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库批次" prop="harvestBatchId">
              <el-select ref="batchId" class="w100" v-model="form.harvestBatchId" @change="changeHarvestBatchId" :disabled="isDisable" placeholder="请先选择所属商户和商品名称" style="flex: 1;">
                <el-option v-for="item in batchNameList"
                :key="item" :label="item" :value="item"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产时间" prop="harvestTime">
              <el-date-picker style="width: 100%;" clearable v-model="form.harvestTime" type="datetime" value-format="timestamp" placeholder="选择生产时间" :disabled="status=='watch'?true:false" />
            </el-form-item>
          </el-col>
          <el-col :span="24"><span>检验信息</span></el-col>
          <el-col :span="12">
            <el-form-item label="执行标准" prop="inspectionStandard">
              <el-input v-model="form.inspectionStandard" placeholder="请输入执行标准" :disabled="status=='watch'?true:false" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验结果" prop="inspectionResult">
              <el-select class="w100" v-model="form.inspectionResult" placeholder="请选择检验结果" style="flex: 1;" :disabled="status=='watch'?true:false">
                <el-option v-for="item in inspectionResultList" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="自检检验项目" prop="inspectionOwnItem">
              <el-input v-model="form.inspectionOwnItem" placeholder="请输入自检检验项目" :disabled="status=='watch'?true:false" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验人" prop="inspectionUser">
              <el-input v-model="form.inspectionUser" placeholder="请输入检验人" :disabled="status=='watch'?true:false" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="委托检验项目" prop="inspectionEntrustItem">
              <el-input v-model="form.inspectionEntrustItem" placeholder="请输入委托检验项目" :disabled="status=='watch'?true:false" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验机构" prop="inspectionOrganization">
              <el-input v-model="form.inspectionOrganization" placeholder="请输入检验机构" :disabled="status=='watch'?true:false" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检验日期" prop="inspectionDate">
              <el-date-picker style="width: 100%;" clearable v-model="form.inspectionDate" type="datetime" value-format="timestamp" placeholder="选择检验日期" :disabled="status=='watch'?true:false" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="等级" prop="inspectionLevel">
              <el-select class="w100" v-model="form.inspectionLevel" placeholder="请输入等级" style="flex: 1;" :disabled="status=='watch'?true:false" >
                <el-option v-for="item in inspectionLevelList" :key="item.value" :label="item.label" :value="item.label"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="签发日期" prop="issuanceDate">
              <el-date-picker style="width: 100%;" clearable v-model="form.issuanceDate" type="datetime" value-format="timestamp" placeholder="选择签发日期" :disabled="status=='watch'?true:false" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片" prop="inspectionImgUrl">
              <imageUpload v-model="form.inspectionImgUrl" :limit="6" :disabled="status=='watch'?true:false" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="视频" prop="inspectionVideoUrl">
              <VideoUpload v-model="form.inspectionVideoUrl" :value="form.inspectionVideoUrl" :disabled="status=='watch'?true:false" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="flex-jcenter" style="margin: 20px 0;">
        <div></div>
        <div>
          <el-select
            v-model="templateId"
            class="mr-10"
            @change="changeTempId"
            placeholder="请先选择所属商户">
            <el-option :value="index" :label="item.templateName" v-for="(item,index) in tempList" :key="index"></el-option>
          </el-select>
          <el-button type="info" plain :disabled="templateId!=undefined?false:true" @click="importModel">导入模板</el-button>
          <el-button class="mr-10" type="primary" @click="saveModel">保存为模板</el-button>
        </div>
      </div>
      <el-table
       :data="form.inspectionItems"
       style="width: 100%">
         <el-table-column
          prop="inspectionItem"
          label="检验项目">
          <template v-slot="{ row }">
            <el-input placeholder="请输入" v-model="row.inspectionItem"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          prop="inspectionUnit"
          label="单位">
          <template v-slot="{ row }">
            <el-input placeholder="请输入" v-model="row.inspectionUnit"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          prop="inspectionRequire"
          label="技术要求">
          <template v-slot="{ row }">
            <el-input placeholder="请输入" v-model="row.inspectionRequire"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          prop="inspectionResult"
          label="实测结果">
          <template v-slot="{ row }">
            <el-input placeholder="请输入" v-model="row.inspectionResult"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          prop="inspectionConclusion"
          label="单项结论">
          <template v-slot="{ row }">
            <el-input placeholder="请输入" v-model="row.inspectionConclusion"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          label="操作">
          <template v-slot="{ row , $index }">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDeleteItems(row,$index)"
                       v-hasPermi="['product:inspection:delete']">删除</el-button>
          </template>
        </el-table-column>
        <div slot="empty">
            <EmptyTable />
        </div>
      </el-table>
      <div class="setting-btn-area" @click="form.inspectionItems.push({})">
        <i style="font-size: 14px;" class="el-icon-plus"></i>
        <div class="buttonText">添加一行数据</div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
    title="保存为模板"
    :visible.sync="dialogTemp"
    width="500px">
      <el-form
      ref="formTemp"
      :model="formTemp"
      :rules="rulesTemp"
      label-width="80px">
        <el-form-item label="模板名称" prop="templateName">
          <el-input v-model="formTemp.templateName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogTemp = false">取 消</el-button>
        <el-button type="primary" @click="submitTemp">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  createInspection,
  updateInspection,
  deleteInspection,
  getInspection,
  getInspectionPage,
  exportInspectionExcel,
  inspectionActive,
  inspectionFreeze,
  inspectionUpChain,
} from "@/api/production/inspection";
import { getBusinessNameList } from '@/api/basic/businessInfo';
import { getGoodsListByBelongBusinessId } from '@/api/basic/goodsInfo';
import { getBatchIdsByGoodsId } from '@/api/product/harvestBatch';
import { getByBusinessId , createTemplate } from '@/api/configure/inspectionTemplate';
import { getHarvestBatchByHarvesId } from '@/api/production/harvestBatch';

import SearchInfo from '@/components/Customer/SearchForm'
import EmptyTable from '@/components/EmptyTable'
import ImageUpload from '@/components/ImageUpload';
import VideoUpload from '@/components/VideoUpload';
export default {
  name: "Inspection",
  components: {
    SearchInfo,
    EmptyTable,
    ImageUpload,
    VideoUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检验检疫列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        inspectionId: null,
        goodsId: null,
        goodsName: null,
        harvestBatchId: null,
        harvestTime: [],
        inspectionStandard: null,
        inspectionResult: null,
        inspectionOwnItem: null,
        inspectionUser: null,
        inspectionEntrustItem: null,
        inspectionOrganization: null,
        inspectionDate: [],
        inspectionLevel: null,
        issuanceDate: [],
        blockState: null,
        blockChainId: null,
        blockChainBlockIdentity: null,
        belongBusinessId: null,
        belongBusinessName: null,
        createTime: [],
      },
      dataSource:[
        {
          key:'harvestBatchId',
          itemType:'input',
          label:'入库批次',
          placeholder:'请输入',
          onEnterKeyUp:this.handleQuery
        },
        {
          key:'goodsName',
          itemType:'input',
          label:'商品名称',
          placeholder:'请输入',
          onEnterKeyUp:this.handleQuery
        },
        {
          key:'goodsId',
          itemType:'input',
          label:'商品编号',
          placeholder:'请输入',
          onEnterKeyUp:this.handleQuery
        },
        {
          key:'belongBusinessName',
          itemType:'input',
          label:'所属商户',
          placeholder:'请输入',
          onEnterKeyUp:this.handleQuery
        },
        {
          key:'productData',
          itemType:'datePicker',
          label:'生产日期',
          placeholder:'请选择',
          onEnterKeyUp:this.handleQuery
        },
        {
          key:'createDate',
          itemType:'datePicker',
          label:'创建日期',
          placeholder:'请选择',
          onEnterKeyUp:this.handleQuery
        },
        // {
        //   key:'blockState',
        //   itemType:'select',
        //   label:'状态',
        //   placeholder:'请选择',
        //   onEnterKeyUp:this.handleQuery,
        //   options: this.optionsBlockState,
        // },
      ],
      // 表单参数
      form: {
        inspectionItems: [],
      },
      // 表单校验
      rules: {
        inspectionId: [{ required: true, message: "检验批次id不能为空", trigger: "change" }],
        goodsId: [{ required: true, message: "商品id不能为空", trigger: "change" }],
        goodsName: [{ required: true, message: "商品名称不能为空", trigger: "change" }],
        harvestBatchId: [{ required: true, message: "入库批次id不能为空", trigger: "change" }],
        harvestTime: [{ required: true, message: "入库时间不能为空", trigger: "change" }],
        inspectionStandard: [{ required: true, message: "执行标准不能为空", trigger: "change" }],
        inspectionResult: [{ required: true, message: "检验结果不能为空", trigger: "change" }],
        inspectionOwnItem: [{ required: true, message: "自检检验项目不能为空", trigger: "change" }],
        inspectionUser: [{ required: true, message: "检验人不能为空", trigger: "change" }],
        inspectionDate: [{ required: true, message: "检验日期不能为空", trigger: "change" }],
        // blockState: [{ required: true, message: "上传状态不能为空", trigger: "change" }],
        belongBusinessId: [{ required: true, message: "所属商户id不能为空", trigger: "change" }],
        belongBusinessName: [{ required: true, message: "所属商户名称不能为空", trigger: "change" }],
      },
      // 上传状态列表
      optionsBlockState: [
        {
          label: '未上传',
          value: 0,
        },
        {
          label: '激活',
          value: 1,
        },
        {
          label: '冻结',
          value: 2,
        }
      ],
      inspectionResultList: [
        {
          label: '合格',
          value: '0',
        },
        {
          label: '不合格',
          value: '1',
        },
      ],
      inspectionLevelList: [
        {
          label: '合格',
          value: '合格',
        },
        {
          label: '暂未定级',
          value: '暂未定级',
        },
      ],
      // 选择模板id
      templateId: undefined,
      // 模板可选列表
      tempList: [],
      // 可选商户列表
      businessNameList: [],
      // 可选商品列表
      goodsNameList: [],
      // 可选入库批次列表
      batchNameList: [],
      // 是否禁用选择
      isDisable: false,
      // status为update修改,为watch查看
      status: '',
      // 模板表单
      formTemp: {},
      // 是否展示模板弹窗
      dialogTemp: false,
      // 模板规则
      rulesTemp: {
        templateName: [{ required: true, message: "模板名称不能为空", trigger: "change" }],
      },
    };
  },
  created() {
    this.getList();
    this.initDate();
  },
  methods: {
    // 初始化参数
    async initDate(){
      let res = await getBusinessNameList();
      this.businessNameList = res.data;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getInspectionPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        inspectionId: undefined,
        goodsId: undefined,
        goodsName: undefined,
        harvestBatchId: undefined,
        harvestTime: undefined,
        inspectionStandard: undefined,
        inspectionResult: undefined,
        inspectionOwnItem: undefined,
        inspectionUser: undefined,
        inspectionEntrustItem: undefined,
        inspectionOrganization: undefined,
        inspectionDate: undefined,
        inspectionLevel: undefined,
        issuanceDate: undefined,
        // blockState: undefined,
        blockChainId: undefined,
        blockChainBlockIdentity: undefined,
        belongBusinessId: undefined,
        belongBusinessName: undefined,
        inspectionItems: [],
      };
      this.resetForm("form");
    },
    /** 表单重置 */
    resetTemp() {
      this.formTemp = {
        templateName: undefined,
      };
      this.resetForm("formTemp");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm","searchForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检验检疫";
      this.isDisable = false;
      this.status = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row,status) {
      this.reset();
      const id = row.id;
      getInspection(id).then(res => {
        res.data.inspectionResult = String(res.data.inspectionResult);
        this.form = res.data;
        this.changeBusinessId(res.data.belongBusinessId);
        this.changeGoods(res.data.goodsId,true);
        this.open = true;
        this.title = "修改检验检疫";
        this.isDisable = true;
        this.status = status
      });
    },
    // 验证数据 type=true,创建模板时，不需要验证实测结果和单项结论是否为空
    checkData(type=false){
      let inspectionItems = this.form.inspectionItems;
      if(!this.form.inspectionItems.length){
        this.$message.error("检验项目列表不能为空");
        return false;
      }
      let isHasNull = false;//检验项目列表 是否存在数据项为空
      inspectionItems.map(item=>{
        if(type){
          if(!item.inspectionItem || !item.inspectionUnit || !item.inspectionRequire  ) isHasNull = true;
        }else{
          if(!item.inspectionItem || !item.inspectionUnit || !item.inspectionRequire || !item.inspectionResult || !item.inspectionConclusion  ) isHasNull = true;
        }
      });
      if(isHasNull){
        this.$message.error("检验项目数据项不能为空");
        return false;
      }
      return true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(async valid => {
        if (!valid) {
          return;
        }
       let end = await this.checkData();
       if(!end) return false;
        // 修改的提交
        if (this.form.id != null) {
          updateInspection(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createInspection(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除检验检疫编号为"' + id + '"的数据项?').then(function() {
          return deleteInspection(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 上传按钮操作 */
    handleUpChain(row) {
      const id = row.id;
      this.$modal.confirm('是否确认上传检验检疫编号为"' + id + '"的数据项?').then(function() {
          return inspectionUpChain(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("上传成功");
        }).catch(() => {});
    },
    /** 冻结按钮操作 */
    handleFreeze(row) {
      const id = row.id;
      this.$modal.confirm('是否确认冻结检验检疫编号为"' + id + '"的数据项?').then(function() {
          return inspectionFreeze(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("冻结成功");
        }).catch(() => {});
    },
    /** 激活按钮操作 */
    handleActive(row) {
      const id = row.id;
      this.$modal.confirm('是否确认激活检验检疫编号为"' + id + '"的数据项?').then(function() {
          return inspectionActive(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("激活成功");
        }).catch(() => {});
    },
    // 弹窗-删除按钮操作
    handleDeleteItems(row,index){
      this.form.inspectionItems.splice(index,1);
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有检验检疫数据项?').then(() => {
          this.exportLoading = true;
          return exportInspectionExcel(params);
        }).then(response => {
          this.$download.excel(response, '检验检疫.xls');
          this.exportLoading = false;
        }).catch(() => {});
    },
    // 模板确认导入
    importModel() {
      this.form.inspectionItems = this.tempList[this.templateId].inspectionItems.map(item => {
        return {
          ...item,
          id: null,
        }
      });
    },
    // 当前选择保存为模板
    saveModel(){
      this.resetTemp();
      this.dialogTemp = true;
    },
    // 改变所属商户
    changeBusinessId(e){
      this.$nextTick(async ()=>{
        this.form.belongBusinessName = this.$refs.business.selected.label;
        let res = await getGoodsListByBelongBusinessId({belongBusinessId:e});
        this.goodsNameList = res.data;
        let rest = await getByBusinessId({businessId:e});
        this.tempList = rest.data;
      })
    },
    // 改变所属商品
    changeGoods(e,status){
      this.$nextTick(async ()=>{
        this.form.goodsName = this.$refs.goods.selected.label;
        if(!status) this.form.harvestBatchId = null;
        let form = {
          businessId: this.form.belongBusinessId,
          goodsIds: e,
        };
        let res = await getBatchIdsByGoodsId(form);
        if( res.data ){
          let list = [];
          for(let i in res.data[e]){
            list.push(res.data[e][i]);
          }
          this.batchNameList = list;
        }
      })
    },
    // 改变入库批次
    changeHarvestBatchId(e){
      this.$nextTick(async ()=>{
        let res = await getHarvestBatchByHarvesId(e);
        console.log('获取生产时间',res);
        if(res.data) this.form.harvestTime = res.data.harvestDate;
      });
    },
    // 新建模板提交
    async submitTemp(){
      this.$refs["formTemp"].validate(async valid => {
        if (!valid) {
          return;
        }
        let end = await this.checkData(true);
        if(!end) return false;
        let inspectionItems = JSON.parse(JSON.stringify(this.form.inspectionItems));
        let belongBusinessId = JSON.parse(JSON.stringify(this.form.belongBusinessId));
        inspectionItems.map(item=>{
          item.templateName = this.formTemp.templateName;
        });
        let form = {
          belongBusinessId,
          inspectionItems,
          templateName: this.formTemp.templateName,
        };
        let res = await createTemplate(form);
        if(res.data){
          this.dialogTemp = false;
          this.resetTemp();
          let rest = await getByBusinessId({businessId:belongBusinessId});
          this.tempList = rest.data;
        }
      });
    },
    changeTempId(e){
      console.log(e);
    }
  }
};
</script>
