<template>
  <div class="meta-container">
    <div class="head-container">
      <!-- <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.code"
          clearable
          size="small"
          placeholder="输入检查项目编号"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="输入检查项目名称"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </div> -->
      <crudOperation :permission="permission" />
    </div>
    <!--表单渲染-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crudCU"
      :title="crud.status.title"
      width="570px"
    >
      <el-row :gutter="40">
        <el-col>
          <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
            <el-form-item label="检查对象分类" prop="objectName" v-if="form.objectName">
              <el-input v-model="form.objectName" size="small" class="filter-item" style="width: 400px" disabled/>
            </el-form-item>
            <el-form-item label="检查项目" prop="content">
              <el-input v-model="form.content" size="small" class="filter-item" style="width: 400px" />
            </el-form-item>
            <el-form-item label="检查标准" prop="standard">
              <el-input v-model="form.standard" size="small" class="filter-item" style="width: 400px" />
            </el-form-item>
            <el-form-item label="数据类型" prop="dataType">
              <el-select
                v-model="form.dataType"
                clearable
                size="small"
                placeholder="请选择数据类型"
                class="filter-item"
                style="width: 400px"
              >
                <el-option
                  v-for="item of optionsObj.ItemDataType"
                  :key="item.value"
                  :label="item.label"
                  :value="+item.value"
                />
              </el-select>
            </el-form-item>
            <div v-if="form.dataType == 1">
              <el-row>
                <el-col :span="8">
                  <el-form-item label-width="100px" label="范围">
                    <el-input v-model="form.unit" placeholder="单位" size="small" class="filter-item" style="width: 60px" />
                  </el-form-item>
                </el-col>
                <el-col :span="16">
                <template class="judgementConditions">
                  <el-row v-for="(it,i) in form.numberJudgment.judgementConditions" :key="i">
                    <el-col :span="6">
                      <el-form-item label-width="4px">
                        <el-select
                          v-model="it.logicalOperator"
                          clearable
                          size="small"
                          placeholder="逻辑符号"
                          class="filter-item"
                          style="width: 80px"
                        >
                          <el-option
                            v-for="item of optionsObj.LogicalOperator"
                            :key="item.value"
                            :label="item.label"
                            :value="+item.value"
                          />
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item label-width="4px">
                        <el-select
                          v-model="it.comparisonOperator"
                          clearable
                          size="small"
                          placeholder="运算符号"
                          class="filter-item"
                          style="width: 80px"
                        >
                          <el-option
                            v-for="item of optionsObj.ComparisonOperator"
                            :key="item.value"
                            :label="item.label"
                            :value="+item.value"
                          />
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="5">
                      <el-form-item  label-width="12px">
                        <el-input v-model="it.comparisonValue" size="small" class="filter-item" style="width: 60px" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-form-item  label-width="12px">
                        <el-button type="primary" size="mini"  icon="el-icon-minus" @click="handleDeleteJud(i,form.numberJudgment.judgementConditions)" circle></el-button>
                        <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleAddJud(i, form.numberJudgment.judgementConditions)"  circle></el-button>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </template>
                </el-col>
              </el-row>
            </div>
          </el-form>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU"
          >确认</el-button
        >
      </div>
    </el-dialog>
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%;"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column :selectable="checkboxT" type="selection" width="55" align="center" />
      <el-table-column
              type="index"
              label="序号"
              width="120"
              align="center">
            </el-table-column>
      <el-table-column
        v-for="item in patrolItemTableOptions"
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
        :width="item.width"
        :align="item.align"
        :fixed="item.fixed"
        :sortable="item.sortable"
      >
        <template slot-scope="scope">
          <span v-if="item.prop == 'shapeType'">
            {{ shapeOptions.find(t => t.value === scope.row.shapeType).label }}
          </span>
          <span v-else>{{ scope.row[item.prop] }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="checkPer(['admin', 'patrolItem:edit', 'patrolItem:del'])"
        label="操作"
        width="125"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation :data="scope.row" :permission="permission" />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import { getOptions } from '@/api/patrolManage/options'
import DateRangePicker from '@/components/DateRangePicker'
import crudPatrolItem from '@/api/patrolManage/patrolItem'
import { patrolItemTableOptions } from '../tableConfig'
import { initData } from '@/api/data'

const defaultForm = {
  //主要参数
  id: '',
  name: '',
  content: '',//项目
  standard: '',//标准
  dataType:'',//数据类型
  unit: '',//单位

  numberJudgment: {
    judgementConditions: [
      {
        logicalOperator: 1,
        comparisonOperator: 1,
        comparisonValue: 0
      }
    ]
  },
  statusOptionId: 0,
  normalStatus: 0,
  sort: 0,
  statusOption: '',
  tag: '',

  lowerLimit: '',
  upperLimit:'',
  orderNo: '',
  symbol: '',
  //关联巡检对象信息
  objectId: '',
  objectName: '',
  objectTypeId: '',
}
export default {
  name: 'patrolItem',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: {
    ...crudComps,
    DateRangePicker,
  },
  cruds() {
    return CRUD({
      title: '巡检项目',
      url: 'patrol/objectItem/getList',
      params: {"name":this.objectName },
      crudMethod: { ...crudPatrolItem },
      optShow: { add: true, edit: true, del: true, reset: true, import: true, export: true },
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'patrolItem:add'],
        edit: ['admin', 'patrolItem:edit'],
        del: ['admin', 'patrolItem:del']
      },
      patrolItemTableOptions,
      rules: {
        content: [{ required: true, message: '请输入巡检项目', trigger: 'blur' }],
        standard: [{ required: true, message: '请输入巡检标准', trigger: 'blur' }],
      },
      optionsObj: {
        ComparisonOperator:[],//比较运算符
        LogicalOperator:[],//逻辑运算符
        ItemDataType:[],//巡检项目数据类型
      },
      dataTypeList:[
        {label:"文本",value:"1"},
        {label:"状态",value:"2"},
        {label:"数值",value:"3"},
        {label:"位号",value:"4"},
      ],
      unitList:[
        {label:"pH值",value:"1"},
        {label:"液位(mm)",value:"2"},
        {label:"压力(KPa)",value:"3"},
        {label:"气体(ppm)",value:"4"},
        {label:"浓度(%LEL)",value:"5"},
        {label:"液位(%)",value:"6"},
      ],
      symbolList:[
        {label:"><",value:"1"},
        {label:"≥≤",value:"2"},
        {label:">",value:"3"},
        {label:"<",value:"4"},
        {label:"≥",value:"5"},
        {label:"≤",value:"6"},
      ],
      objectTypeList:[],
      objectId:'',
      objectName:'',
      objectTypeId:'',
    }
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    }
  },
  created() {
    this.getOptions()
  },
  methods: {
    // 新增与编辑前做的操作
    [CRUD.HOOK.beforeToAdd](crud, form) {
      this.form.objectId = this.objectId
      this.form.objectName = this.objectName
      this.form.objectTypeId = this.objectTypeId
    },
    // 提交前做的操作
    [CRUD.HOOK.afterValidateCU](crud) {
      
    },
    getOptions() {
      Object.keys(this.optionsObj).forEach(async (key) => {
        try {
          let res = await getOptions(key)
          if (res.code === 20000) this.optionsObj[key] = res.data.content
        } catch (error) {
          console.log(error)
        }
      })
    },
    checkboxT(row, rowIndex) {
      return true
      // return row.id !== this.userInfo.id
    },
    handleDeleteJud(i,row) {
      console.log(row);
      if (row && row.length > 1
      ) {
        row.splice(i,1)
      }else{
        this.$message({
          showClose: true,
          message: '不能再减少运算式',
          type: 'warning'
        })
      }
    },
    handleAddJud(i,row){
      console.log(row)
      row.splice(row.length,0,{
        logicalOperator:1,
        comparisonOperator:1,
        comparisonValue:0
      })
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .vue-treeselect__control,
::v-deep .vue-treeselect__placeholder,
::v-deep .vue-treeselect__single-value {
  height: 30px;
  line-height: 30px;
}
</style>
