<template>
  <div class="app-container">
    <el-row :gutter="10" v-if="optType !='view'" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:qc:qctemplate:add']"
        >{{$t('新增')}}</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="['mes:qc:qctemplate:edit']"
        >{{$t('修改')}}</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="['mes:qc:qctemplate:remove']"
        >{{$t('删除')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="templateindexList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column :label="$t('检测项名称')" align="center" prop="indexName"/>
      <el-table-column :label="$t('检测项类型')" align="center" prop="indexType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_index_type" :value="scope.row.indexType"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('输入类型')" align="center" prop="inputType">
        <template slot-scope="scope">
          <span v-if="scope.row.inputType==1">{{$t('文本框')}}</span>
          <span v-if="scope.row.inputType==2">{{$t('选择框')}}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('检测工具')" align="center" prop="qcTool"/>
      <el-table-column :label="$t('检测方法')" align="center" prop="checkMethod" :show-overflow-tooltip="true"/>
      <!-- <el-table-column label="标准值" align="center" prop="standerVal" />
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="误差上限" align="center" prop="thresholdMax" />
      <el-table-column label="误差下限" align="center" prop="thresholdMin" /> -->
      <el-table-column :label="$t('common.option')" v-if="optType != 'view'" align="center" width="150px"
                       class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:qc:qctemplate:edit']"
          >{{$t('修改')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:qc:qctemplate:remove']"
          >{{$t('删除')}}</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="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('检测项')" prop="indexId">
              <el-input v-model="form.indexName">
                <el-button slot="append" @click="handleSelectIndex" icon="el-icon-search"></el-button>
              </el-input>
              <IndexSelect ref="indexSelect" @onSelected="onIndexSelected"></IndexSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('检测工具')" prop="qcTool">
              <el-input v-model="form.qcTool"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('检测要求')" prop="checkMethod">
              <el-input v-model="form.checkMethod" type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.inputType == 2" align="center">
          <el-col :span="24" align="right">
            <!-- <el-form-item label="选择框值" prop="selectValues">
              <el-input v-model="form.selectValues" type="textarea" placeholder="请输入下拉选择框内容，格式为：[{&quot;label&quot;:&quot;显式值&quot;,&quot;value&quot;:&quot;存储值&quot;}]" />
            </el-form-item> -->
            <el-table :data="form.selectValues" style="width: 89%"  >
              <el-table-column :label="$t('参数值')" align="center" style="width: 100%" min-width="100">
                <!--                <el-input v-model="scope.row.name">{{ scope }}</el-input>-->

                <template v-slot="scope">
                  <el-input v-model="scope.row.name"></el-input>
                </template>
              </el-table-column>
              <el-table-column :label="$t('common.option')" align="center" min-width="200">
                <template v-slot="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="form.selectValues.splice(scope.$index, 1)"
                  >{{$t('删除')}}</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-plus"
                    v-if="scope.$index === form.selectValues.length -1"
                    @click="form.selectValues.push({name: ''})"
                  >{{$t('新增')}}</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <!-- <el-row>
            <el-col :span="12">
                <el-form-item label="标准值" prop="standerVal">
                    <el-input v-model="form.standerVal" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="单位" prop="unitOfMeasure">
                    <el-select v-model="form.unitOfMeasure">
                        <el-option
                            v-for="item in measureOptions"
                            :key="item.measureCode"
                            :label="item.measureName"
                            :value="item.measureCode"
                            :disabled="item.enableFlag == 'N'"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-col>
        </el-row> -->
        <!-- <el-row>
            <el-col :span="12">
                <el-form-item label="误差上限" prop="thresholdMax">
                    <el-input-number :min="0" v-model="form.thresholdMax" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="误差下限" prop="thresholdMin">
                    <el-input-number :max="0" v-model="form.thresholdMin" />
                </el-form-item>
            </el-col>
        </el-row> -->
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('说明图')" prop="docUrl">
              <el-input v-model="form.docUrl"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('common.remark')" prop="remark">
              <el-input v-model="form.remark" type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel" v-if="optType =='view'">{{ $t('common.back') }}</el-button>
        <el-button type="primary" @click="submitForm" v-else>{{ $t('common.confirm') }}</el-button>
        <el-button @click="cancel">{{ $t('common.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import {
  listTemplateindex,
  getTemplateindex,
  delTemplateindex,
  addTemplateindex,
  updateTemplateindex
} from "@/api/mes/qc/templateindex";
import IndexSelect from "@/components/qcindexSelect/single.vue";
import {listAllUnitmeasure} from "@/api/mes/md/unitmeasure";

export default {
  name: "Templateindex",
  dicts: ['mes_index_type'],
  props: {
    optType: null,
    templateId: null
  },
  components: {IndexSelect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检测模板-检测项表格数据
      templateindexList: [],
      //单位选择
      measureOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      inputTypeOptions: [{"label": this.$t('输入框'), "value": 1}, {"label": this.$t('选择框'), "value": 2}],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templateId: this.templateId,
        indexId: null,
        indexCode: null,
        indexName: null,
        indexType: null,
        selectValues: null,
        qcTool: null,
        checkMethod: null,
        standerVal: null,
        unitOfMeasure: null,
        thresholdMax: null,
        thresholdMin: null,
        docUrl: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        templateId: [
          {required: true, message: this.$t('检测模板ID不能为空'), trigger: "blur"}
        ],
        indexId: [
          {required: true, message: this.$t('检测项不能为空'), trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getUnits();
  },
  methods: {
    /** 查询检测模板-检测项列表 */
    getList() {
      this.loading = true;
      listTemplateindex(this.queryParams).then(response => {
        this.templateindexList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getUnits() {
      listAllUnitmeasure().then(response => {
        this.measureOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        templateId: this.templateId,
        indexId: null,
        indexCode: null,
        indexName: null,
        indexType: null,
        qcTool: null,
        checkMethod: null,
        standerVal: null,
        unitOfMeasure: null,
        thresholdMax: null,
        thresholdMin: null,
        docUrl: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: 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.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t('添加检测模板-检测项');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.id || this.ids
      getTemplateindex(recordId).then(response => {
        this.form = response.data;
        if (response.data.selectValues) this.form.selectValues = JSON.parse(response.data.selectValues)
        this.open = true;
        this.title = this.$t('修改检测模板-检测项');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const params = Object.assign({}, this.form)
          params.selectValues = JSON.stringify(this.form.selectValues)
          // this.form.templateId = this.templateId;
          console.log(this.form)
          if (this.form.id != null) {
            updateTemplateindex(params).then(response => {
              this.$modal.msgSuccess(this.$t('common.edit_success'));
              this.open = false;
              this.getList();
            });
          } else {
            addTemplateindex(params).then(response => {
              this.$modal.msgSuccess(this.$t('common.add_success'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.id || this.ids;
      this.$modal.confirm(this.$t('common.confirm_del_data')).then(function () {
        return delTemplateindex(recordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('common.del_success'));
      }).catch(() => {
      });
    },
    //选择检测项弹出框
    handleSelectIndex() {
      this.$refs.indexSelect.showFlag = true;
    },
    //弹框返回值
    onIndexSelected(obj) {
      if (obj != undefined && obj != null) {
        this.form.indexId = obj.indexId;
        this.form.indexCode = obj.indexCode;
        this.form.indexName = obj.indexName;
        this.form.indexType = obj.indexType;
        this.form.qcTool = obj.qcTool;
        this.form.inputType = obj.inputType;
        if (this.form.inputType == 2) {
          this.form.selectValues = [{name: ''}]
        } else {
          this.form.selectValues = null
        }
      }
    }
  }
};
</script>
