<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="巡检点名称" prop="patrolName">
        <el-input
          v-model="queryParams.patrolName"
          placeholder="请输入巡检点名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡检点类型" prop="patrolType">
        <el-select v-model="queryParams.patrolType" placeholder="请选择巡检点类型" clearable size="small">
          <el-option
            v-for="dict in patrolTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="关联nfc" prop="patrolNfc">-->
        <!--<el-input-->
          <!--v-model="queryParams.patrolNfc"-->
          <!--placeholder="请输入关联nfc"-->
          <!--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:position: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:position:edit']"-->
        <!--&gt;修改</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:position:remove']"-->
        <!--&gt;删除</el-button>-->
      <!--</el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:position:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="positionList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <!--<el-table-column label="巡检点id" align="center" prop="patrolId" />-->
      <el-table-column label="巡检点名称" align="center" prop="patrolName" />
      <el-table-column label="巡检点类型" align="center" prop="patrolType" :formatter="patrolTypeFormat" />
      <el-table-column label="关联nfc" align="center" prop="patrolNfc" />
      <el-table-column label="巡检点位置" align="center" prop="position" />
      <!--<el-table-column label="巡检点参数" align="center" prop="patrolDetail" />-->
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <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="handleUpdate(scope.row)"
            v-hasPermi="['system:position:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:position: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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="95px">
        <el-form-item label="巡检点名称" prop="patrolName">
          <el-input v-model="form.patrolName" placeholder="请输入巡检点名称" />
        </el-form-item>
        <el-row :gutter="24" class="mb8">
          <el-col :span="12">
            <el-form-item label="巡检点类型" prop="patrolType">
              <el-select v-model="form.patrolType" placeholder="请选择巡检点类型">
                <el-option
                  v-for="dict in patrolTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10" >
            <el-form-item label="关联nfc" prop="patrolNfc">
              <el-input v-model="form.patrolNfc" placeholder="请输入关联nfc" />
            </el-form-item>
          </el-col>
          <el-col :span="2" style="padding: 0">
            <img style="width: 40px" src="@/assets/images/shuaka.png" @click="rollNfc"/>
          </el-col>
        </el-row>

        <el-form-item label="巡检点位置" prop="position">
          <el-input v-model="form.position" placeholder="请输入巡检点位置" />
        </el-form-item>
        <el-form-item label="巡检点参数" prop="patrolDetail">
          <!--<el-input v-model="form.patrolDetail" type="textarea" placeholder="请输入内容" />-->
          <div class="detail-div">
            <div class="button-div">
              <div class="button" @click="addParam()">新增</div>
            </div>
            <div class="roll-div">
              <div class="one-detail-div"  v-for="(item,index) in paramList" :key="index" >
              <el-row :gutter="24" class="mb8">
                <el-col :span="22">
                  <el-form-item label="参数名称" >
                    <input class="" v-model="item.paramName"/>
                  </el-form-item>
                  <el-form-item label="参数类型" >
                    <select style="width: 83px" v-model="item.paramType" @change="tplSelectChange($event.target.value,index)">
                      <option
                        v-for="dict in types"
                        :key="dict.dictValue"
                        :label="dict.dictLabel"
                        :value="parseInt(dict.dictValue)"
                      ></option>
                    </select>
                    <!--<input class="" v-model="item.paramType"/>-->
                  </el-form-item>
                  <el-form-item label="参数值" v-if="item.paramType==1">
                    <input v-model="item.paramNum1" class="inputstyle" /> - <input v-model="item.paramNum2" class="inputstyle"/>
                    <span class="tixing">（ 当超过最大值，或低于最小值时报警 ）</span>
                  </el-form-item>
                  <el-form-item label="参数值" v-if="item.paramType==2">
                    <el-radio v-model="item.paramNum1" :label="1">是</el-radio>
                    <el-radio v-model="item.paramNum1" :label="0">否</el-radio>
                  </el-form-item>
                </el-col>
                <el-col :span="2">
                  <img class="delete" src="@/assets/images/delete.png" @click="deleteParam(index)"/>
                </el-col>
              </el-row>
            </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="部门" prop="deptId">
          <!--<el-input v-model="form.deptId" placeholder="请选择部门" />-->
          <el-select  v-model="form.deptId" >
            <el-option
              v-for="dict in deptList"
              :key="dict.deptId"
              :label="dict.deptName"
              :value="parseInt(dict.deptId)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input type="textarea" v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPosition, getPosition, delPosition, addPosition, updatePosition, exportPosition } from "@/api/system/position";
import { listDept} from "@/api/system/dept";
export default {
  name: "Position",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      num:0,
      // 巡检点表格数据
      positionList: [],
      deptList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 巡检点类型字典
      patrolTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patrolName: null,
        patrolType: null,
        patrolNfc: null,
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 100
      },
      paramList:[

      ],
      detailType:1,
      types:[
        {
          dictValue:1,
          dictLabel:"数值范围"
        },
        {
          dictValue:2,
          dictLabel:"是否"
        }
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        patrolName: [
          { required: true, message: "巡检点名称不能为空", trigger: "change" }
        ],
        patrolType: [
          { required: true, message: "巡检点类型不能为空", trigger: "change" }
        ],
        patrolNfc: [
          { required: true, message: "巡检点NFC不能为空", trigger: "change" }
        ],
        deptId: [
          { required: true, message: "部门不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDeptList();
    this.getDicts("patrol_type").then(response => {
      this.patrolTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询巡检点列表 */
    getList() {
      this.loading = true;
      listPosition(this.queryParams).then(response => {
        this.positionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 部门列表 */
    getDeptList() {
      listDept(this.queryParams2).then(response => {
          this.deptList = response.data;
          console.log(this.deptList)
      });
    },
    // 巡检点类型字典翻译
    patrolTypeFormat(row, column) {
      return this.selectDictLabel(this.patrolTypeOptions, row.patrolType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        patrolId: null,
        patrolName: null,
        patrolType: null,
        patrolNfc: null,
        position: null,
        patrolDetail: null,
        deptId: null,
        remarks: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: 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.patrolId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.paramList=[];
      this.reset();
      this.open = true;
      this.title = "添加巡检点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const patrolId = row.patrolId || this.ids
      getPosition(patrolId).then(response => {
        this.form = response.data;
        if(this.form.patrolDetail!=null && this.form.patrolDetail!=""){
          this.paramList = JSON.parse(this.form.patrolDetail);
        }
        this.open = true;
        this.title = "修改巡检点";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.patrolDetail = JSON.stringify(this.paramList);
          if (this.form.patrolId != null) {
            updatePosition(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPosition(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      //const patrolIds = row.patrolId || this.ids;
      this.$confirm('是否确认删除该巡检点的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updatePosition({patrolId:row.patrolId,isDel:1});
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    //添加参数
    addParam(){
      var param =  {
          paramName:"",
          paramType:1,
          paramNum1:"",
          paramNum2:"",
          testingNum:""
      }
      this.paramList.push(param);
    },
    //删除参数
    deleteParam(index){
      this.paramList.splice(index,1);
      //this.paramList.splice(index,1);
    },
    //选择参数类型
    tplSelectChange(value,index){
      this.detailType=value;
      if(value==1){
        this.paramList[index].paramNum1="";
      }else {
        this.paramList[index].paramNum1=1;
      }
      console.log(value,index)
    },
    //更新nfc  patrolNfc
    rollNfc(){
      var nowDate = new Date();
      var year = nowDate.getFullYear();
      var month = nowDate.getMonth();
      var today = nowDate.getDate();
      var hours = nowDate.getHours();
      var minutes = nowDate.getMinutes();
      var seconds = nowDate.getSeconds();

      if(month >= 1 && month <= 9) {
        month = "0" + month;
      }
      if(today >= 1 && today <= 9){
        today = "0" + today
      }

      var currentdate = year + "-" + month + "-" + today + " " + hours + ":" + minutes + ":" + seconds;
      //console.log(currentdate)
      var currentDateLong = new Date(currentdate.replace(new RegExp("-","gm"),"/")).getTime();     //当前时间转换成long型
      //console.log(currentDateLong.toString())
      currentDateLong = "NFC"+ currentDateLong.toString().substring(0,10)+this.num;
      this.num = this.num+1;
      this.form.patrolNfc=currentDateLong;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有巡检点数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportPosition(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style>
  .detail-div{
    width: 100%;
    height: 300px;
    border: 1px solid #adafb7;
    border-radius: 5px;
  }
  .one-detail-div{
    width: 600px;
    height: 110px;
    border: 1px solid #adafb7;
    border-radius: 5px;
    margin-left: 5%;
    margin-top: 5px;
  }
  .button-div{
    height: 25px;
    border-radius: 0;
    width: 100%;
  }
  .button{
    width: 90px;
    background: #1c84c6;
    color: #fff;
    margin-left: 81%;
    border: 0;
    border-radius: 5px;
    text-align: center;
    height: 25px;
    line-height: 25px;
    margin-top: 5px;
  }
  .inputstyle{
    width: 83px;
  }
  .delete{
    width: 25px;
    margin-left: 3px;
  }
  .roll-div{
    width: 100%;
    height: 270px;
    overflow:auto;
    overflow-x: hidden;
  }
  .tixing{
    font-size: 12px;
    margin-left: 0px;
  }
</style>
<style lang="scss" scoped>
  ::v-deep .el-select{
    width: 100%;
  }
</style>
