<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="样品名称" prop="sampleName">
        <el-input
          v-model="queryParams.sampleName"
          placeholder="请输入样品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="normsModel">
        <el-input
          v-model="queryParams.normsModel"
          placeholder="请输入规格型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验项目" prop="testItems">
        <el-select v-model="queryParams.testItems" placeholder="请选择检验项目" clearable size="small">
          <el-option
            v-for="dict in dict.type.Inspection_item"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="提供方式" prop="provideType">
        <el-select v-model="queryParams.provideType" placeholder="请选择提供方式" clearable size="small">
          <el-option
            v-for="dict in dict.type.provision_mode"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="送检单位名称" prop="manufacturerName">
        <el-input
          v-model="queryParams.manufacturerName"
          placeholder="请输入送检单位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="委托单位名称" prop="inspectedName">
        <el-input
          v-model="queryParams.inspectedName"
          placeholder="请输入委托单位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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="['system:inspectInfor:add']"
        >新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdategy"
          v-hasPermi="['system:reportDetail:add']"
        >生成报告</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:inspectInfor:query']"
        >查看</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:inspectInfor:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:inspectInfor:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  v-loading="loading" :data="inspectInforList" @selection-change="handleSelectionChange" :max-height="tableMaxHeight">
      <el-table-column fixed type="selection" width="55" align="center" />
      <el-table-column fixed label="样品名称" align="center" prop="sampleName" width="180px"/>
      <el-table-column label="样品数量" align="center" prop="sampleNum" width="80px"/>
      <el-table-column label="规格型号" align="center" prop="normsModel" width="250px"/>
      <el-table-column label="检验项目" align="center" prop="testItems" width="160px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.Inspection_item" :value="scope.row.testItems"/>
        </template>
      </el-table-column>
      <el-table-column label="提供方式" align="center" prop="provideType" width="100px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.provision_mode" :value="scope.row.provideType"/>
        </template>
      </el-table-column>
      <el-table-column label="送检单位名称" align="center" prop="manufacturerName" width="160px" />
      <el-table-column label="送检单位电话" align="center" prop="manufacturerPhone" width="140px"/>
      <el-table-column label="委托单位名称" align="center" prop="inspectedName" width="180px" />
      <el-table-column label="委托单位电话" align="center" prop="inspectedPhone" width="140px"/>
      <el-table-column label="提交时间" align="center" prop="createTime" width="160px"/>
      <el-table-column label="备注" align="center" prop="remark" width="80px"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:inspectInfor:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:inspectInfor:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </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="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="样品名称" prop="sampleName" >
              <el-input v-model="form.sampleName" placeholder="请输入样品名称" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="样品数量" prop="sampleNum" >
              <el-input v-model="form.sampleNum" placeholder="请输入样品数量" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格型号" prop="normsModel" >
              <el-input v-model="form.normsModel" placeholder="请输入规格型号" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检验项目" prop="testItems" disabled>
                      <el-select v-model="form.testItems" placeholder="请选择检验项目" disabled>
                        <el-option
                          v-for="dict in dict.type.Inspection_item"
                          :key="dict.value"
                          :label="dict.label"
            :value="dict.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="提供方式" prop="provideType">
                      <el-select v-model="form.provideType" placeholder="请选择提供方式" disabled>
                        <el-option
                          v-for="dict in dict.type.provision_mode"
                          :key="dict.value"
                          :label="dict.label"
            :value="dict.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="送检单位名称" prop="manufacturerName">
              <el-input v-model="form.manufacturerName" placeholder="请输入送检单位名称" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="送检单位电话" prop="manufacturerPhone">
              <el-input v-model="form.manufacturerPhone" placeholder="请输入送检单位电话" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="委托单位名称" prop="inspectedName">
              <el-input v-model="form.inspectedName" placeholder="请输入委托单位名称" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="委托单位电话" prop="inspectedPhone">
              <el-input v-model="form.inspectedPhone" placeholder="请输入委托单位电话" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确 定</el-button>
               <!-- <el-button @click="cancel">取 消</el-button> -->
      </div>
    </el-dialog>

    <el-dialog :title="titles" :visible.sync="opens" width="800px" append-to-body>
      <el-form ref="forms" :model="forms" :rules="ruless" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="报告编号" prop="reportNumber" >
              <div style="display: flex;">
                <span class="bgt" style="margin-right: 10px;">NO：ASJ(</span>
                <el-input v-model="fore" placeholder="输入四位数信息" style="width: 150px; margin-right: 10px;" class="bhdyei"  maxlength="4" minlength="4" />
                <span class="bgt" style="margin-right: 10px;">QG —)</span>
                 <el-input v-model="thre" placeholder="输入三位数信息"  style="width: 150px; border-left: 0; margin-right: 10px;" maxlength="3" minlength="3" class="bhdyei"/>
                 <span class="bgt">号</span>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="样品名称" prop="sampleName" >
              <el-input v-model="forms.sampleName" placeholder="请输入样品名称" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="送检单位名称" prop="manufacturerName">
              <el-input v-model="forms.manufacturerName" placeholder="请输入送检单位名称" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="委托单位名称" prop="inspectedName">
              <el-input v-model="forms.inspectedName" placeholder="请输入委托单位名称" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否合格" prop="isQualify">
             <el-select v-model="forms.isQualify" placeholder="请选择" style="width: 100%;" @change="nhyese">
               <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
                 :value="dict.value"></el-option>
             </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="nshouw">
            <el-form-item label="详细报告" prop="inspectedName">
              <imageUpload   v-model="forms.reportUrl" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="hyese" > 生成报告</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInspectInfor, getInspectInfors, getInspectInfor, delInspectInfor, addInspectInfor, updateInspectInfor } from "@/api/system/inspectInfor";

export default {
  name: "InspectInfor",
  dicts: ['Inspection_item', 'provision_mode','sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 送检信息表格数据
      inspectInforList: [],
      // 弹出层标题
      title: "",
      titles:'',
      // 是否显示弹出层
      open: false,
      opens:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sampleName: null,
        sampleNum: null,
        normsModel: null,
        testItems: null,
        provideType: null,
        manufacturerName: null,
        manufacturerPhone: null,
        inspectedName: null,
        inspectedPhone: null,
      },
      // 表单参数
      form: {},
      forms:{},
      nshouw:true,
      // 表单校验
      ruless:{
        isQualify: [
                  { required: true, message: "是否合格必填", trigger: "blur" }
                ],
      },
      rules: {
/*        sampleName: [
          { required: true, message: "样品名称不能为空", trigger: "blur" }
        ],
        sampleNum: [
          { required: true, message: "样品数量不能为空", trigger: "blur" }
        ],
        normsModel: [
          { required: true, message: "规格型号不能为空", trigger: "blur" }
        ],
        testItems: [
          { required: true, message: "检验项目不能为空", trigger: "blur" }
        ],
        provideType: [
          { required: true, message: "提供方式不能为空", trigger: "blur" }
        ],
        manufacturerName: [
          { required: true, message: "送检单位名称不能为空", trigger: "blur" }
        ],
        manufacturerPhone: [
          { required: true, message: "送检单位电话不能为空", trigger: "blur" }
        ],
        inspectedName: [
          { required: true, message: "委托单位名称不能为空", trigger: "blur" }
        ],
        inspectedPhone: [
          { required: true, message: "委托单位电话不能为空", trigger: "blur" }
        ]*/
      },
      fore:'',
      thre:'',
      hyese:true,
      tableMaxHeight:300
    };
  },
  created() {
    this.getList();
    window.onresize = () => {
          this.changeTableMaxHeight()
        }
        this.changeTableMaxHeight()
  },
  destroyed () {
      window.onresize = null
    },
  mounted() {
    window.onresize = () => {
          this.changeTableMaxHeight()
        }
        this.changeTableMaxHeight()
  },
  methods: {
    /** 查询送检信息列表 */
    getList() {
      this.loading = true;
      listInspectInfor(this.queryParams).then(response => {
        this.inspectInforList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.opens = false
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        inspectId: null,
        sampleName: null,
        sampleNum: null,
        normsModel: null,
        testItems: null,
        provideType: null,
        manufacturerName: null,
        manufacturerPhone: null,
        inspectedName: null,
        inspectedPhone: null,
        isDel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.inspectId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加送检信息";
    },
    /** 查看按钮操作 */
    handleUpdate(row) {
      this.reset();
      const inspectId = row.inspectId || this.ids
      getInspectInfor(inspectId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看送检信息";
      });
    },
    handleUpdategy(row){
      // this.reset();
      const inspectId = row.inspectId || this.ids
      getInspectInfor(inspectId).then(response => {
        this.forms= response.data;
        this.fore = ''
        this.thre = ''
         console.log(response.data.isReport)
         if(response.data.isReport == "N"){
           console.log(response.data.isReport)
           this.$message.error("报告已生成")
           this.hyese = true
           // return false
         }else{
           this.hyese = false
           this.opens = true;
         }

        this.titles = "生成送检报告";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["forms"].validate(valid => {
        if (valid) {
          if(this.forms.isQualify == 'N'){
             this.forms.reportUrl = undefined
          }else{
            if(this.forms.reportUrl == undefined || this.forms.reportUrl== null || this.forms.reportUrl == ""){
                 this.$message.error("详细报告必填")
                 return false
            }
          }
          if(this.fore !== '' && this.thre !== ''){
            this.forms.reportNumber = 'NO:ASJ('+ this.fore+')QG —'+this.thre +'号'
          }else{
            this.$message.error("报告编号必填")
             return  false
          }
          this.forms.isReport = undefined
            addInspectInfor(this.forms).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.opens = false;
              this.getList();
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const inspectIds = row.inspectId || this.ids;
      this.$modal.confirm('是否确认删除送检信息编号为"' + inspectIds + '"的数据项？').then(function() {
        return delInspectInfor(inspectIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/inspectInfor/export', {
        ...this.queryParams
      }, `inspectInfor_${new Date().getTime()}.xlsx`)
    },
    nhyese(){
      console.log(this.forms.isQualify)
      if(this.forms.isQualify == 'Y'){
        // 是
        this.nshouw = true
      }else{
        this.nshouw = false
      }
    },
    // 获取屏幕高度
    showFilterForm () {
          this.filterActive = !this.filterActive
          this.changeTableMaxHeight()
        },

        changeTableMaxHeight () {
          let height = document.body.offsetHeight // 网页可视区域高度
          // if (this.filterActive) {
          //   this.tableMaxHeight = height - 320
          // } else {
            this.tableMaxHeight = height - 350
          // }
          console.log(height)
        }
  }
};
</script>
<style type="text/css" lang="scss">
  .bhdyei{
    .el-input__inner{
      border-left: 0;
      border-right: 0;
      border-top: 0;
      border-radius: 0;
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #343434;
      line-height: 34px
    }
  }
  .bgt{
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: bold;
    color: #343434;
    line-height: 34px
  }
</style>
