<template>
  <div>
  <el-dialog title="检验项目"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal= false
    width="1000px"
  >
    <el-row :gutter="20">

      <el-col :span="24" :xs="24">
        <el-table v-loading="loading" :data="itemList" ref="myTable" >
          <el-table-column width="50" align="center" type="selection">
          </el-table-column>
          <!-- 序号 -->
          <el-table-column label="检测规则Id"  align="left" prop="recordId" :show-overflow-tooltip="true" v-if="false"/>
          <el-table-column label="检测规则编码"  align="left" prop="projectNo" :show-overflow-tooltip="true" width="120"/>
          <el-table-column label="检验规则名称"  align="left" prop="ruleName" :show-overflow-tooltip="true" width="120"/>
          <el-table-column label="检验规则属性"  align="left" prop="propertyCode" :show-overflow-tooltip="true" width="120"/>
          <el-table-column label="检验方式"  align="left" prop="checkMode" :show-overflow-tooltip="true" /> 
          <el-table-column label="检验工具"  align="left" prop="checkTool" :show-overflow-tooltip="true" /> 
          <el-table-column label="检验标准"  align="left" prop="checkStandard" :show-overflow-tooltip="true" /> 
          <el-table-column label="上差值"  align="left" prop="upperDiff" :show-overflow-tooltip="true" /> 
          <el-table-column label="下差值"  align="left" prop="downDiff" :show-overflow-tooltip="true" /> 
          <el-table-column label="单位"  align="left"  prop="unitCode" :show-overflow-tooltip="true" width="80"/>
          <el-table-column label="检测结果" key="status" align="center" prop="status" width="90">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="Y"
                inactive-value="N"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
           <el-table-column label="测量值"  align="left"  prop="actualValue" :show-overflow-tooltip="true" width="120"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleShowValue(scope.row)"
                v-hasPermi="['quality:qcIncome:edit']"
              >检测值</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-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitBomForm">确 定</el-button>
              <el-button @click="showFlag=false">取 消</el-button>
       </div>
  </el-dialog>
  <!--测量值弹窗-->
	<el-dialog title="测量值" :visible.sync="valueOpen" width="600px" append-to-body>
      <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
          <el-form-item
            v-for="(domain, index) in dynamicValidateForm.domains"
            :label="'测量值' + index"
            :key="domain.key"
            :prop="'domains.' + index + '.value'"
            :rules="{
              required: true, message: '测量值不能为空', trigger: 'blur'
            }"
          >
            <el-input v-model="domain.value" style="width: 70%;"></el-input>
            <el-button @click.prevent="removeDomain(domain)" style="margin-left:30px">删除</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
            <el-button @click="addDomain">新增测量值</el-button>
            <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
          </el-form-item>
        </el-form>
		</el-dialog>	
    </div>
</template>
<script>

import { getCkeckProjectList,changeCheckDetailStatus,commitActualValue,commitCheckResult } from "@/api/quality/income";
export default {
  name: "itemSelectUser",
  dicts: ["check_result"],
  data() {
    return {
      showFlag:false,
      // 选中数组
      selectedRows: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // BOM产品表格数据
      itemList: null,

      //树名称
      bomCode: undefined,
      defaultProps: {
        id: "id",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        belongTo:''
        //,userCode: '',
        //userName : ''
      },
      data:{},
      dynamicValidateForm: {
          domains: [{
            value: ''
          }]
      },
      // 是否显示弹出层
			valueOpen: false, 
      //检测详情的id
      recordId:'',
      typeCode:'',//检测类型
    };
  },
  created() {
    //this.getList();
  },

  methods: {
    init(rowdata,typeCode){
      this.data=rowdata;
      this.queryParams.belongTo = this.data.recordId;
      this.typeCode = typeCode;
      this.checkType = this.data.checkType;
      this.getList();
    },

    /** 查询表格列表*/
    getList() {
      this.loading = true;
      getCkeckProjectList(this.queryParams).then(response => {
          this.itemList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    //提交整体检测结果
    submitBomForm() {
      commitCheckResult(this.queryParams.belongTo,this.typeCode,this.checkType).then(response => {
          this.$modal.msgSuccess("检测完成");
          this.$emit('saveCheck');
          this.showFlag = false;
      });
    },

    // 数据状态修改/Y合格N不合格
		handleStatusChange(row) {
			let text = row.status === "N" ? "不合格" : "合格";
			this.$modal.confirm('确认' + '"' + row.projectNo + '"'+text+'吗？').then(function() {
				return changeCheckDetailStatus(row.recordId, row.status);
			}).then(() => {
				this.$modal.msgSuccess(text + "操作成功");
			}).catch(function() {
				row.status = row.status === "N" ? "Y" : "N";
			});
		},

    //动态测量值
    /** 检测结果钮操作 */
		handleShowValue(row) {
			this.valueOpen = true;
      this.recordId = row.recordId;
		},
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let values = this.dynamicValidateForm.domains;
          var actualValues = values.map(item => item.value);
          commitActualValue(this.recordId, actualValues).then(response => {
              this.dynamicValidateForm.domains = [{ value: ''}];
							this.$modal.msgSuccess("提交成功");
							this.valueOpen = false;
              this.getList();
						});
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    removeDomain(item) {
      var index = this.dynamicValidateForm.domains.indexOf(item)
      if (index !== -1) {
        this.dynamicValidateForm.domains.splice(index, 1)
      }
    },
    addDomain() {
      this.dynamicValidateForm.domains.push({
        value: '',
        key: Date.now()
      });
    }
  }
};
</script>
