<template>
  <div class="meta-container">
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="输入名称"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.objectTypeId"
          clearable
          size="small"
          placeholder="输入巡检对象类型"
          class="filter-item"
          style="width: 180px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item of objectTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <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="name">
              <el-input v-model="form.name" 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 dataTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <div v-if="form.dataType == 2">
              <el-form-item prop="unit" label="单位" >
                <el-input v-model="form.unit" size="small" class="filter-item" style="width: 400px"/>
              </el-form-item>
              <el-form-item prop="attribute" label="属性" >
                <el-radio v-model="form.attribute" label="1">设备监测点</el-radio>
                  <el-radio v-model="form.attribute" label="2">环境监测点</el-radio>
              </el-form-item>
            </div>
            <div v-if="form.dataType == 1">
              <el-form-item label="单选/多选" prop="flag">
                <el-radio v-model="form.flag" label="1">单选</el-radio>
                <el-radio v-model="form.flag" label="2">多选</el-radio>
              </el-form-item>
                <el-table
                :data="form.optionItems"
                style="width: 100%; margin-top: 20px">
                  <el-table-column
                    label="选项名称">
                    <template slot-scope="scope">
                        <el-input type="text" v-model="scope.row.name"/>
                        <!-- <el-input type="text" v-model="scope.row.sort"/> -->
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop=""
                    label="操作"
                    type="index"
                    width="180px">
                    <template slot-scope="scope">
                      <el-button type="primary" icon="el-icon-top" size="mini" @click="handleUp(scope.$index, scope.row)"  :disabled="scope.$index == 0"></el-button>
                      <el-button type="primary" icon="el-icon-bottom" size="mini" @click="handleDown(scope.$index, scope.row)" :disabled="scope.$index == form.optionItems.length-1"></el-button>
                      <el-button type="danger" icon="el-icon-minus" size="mini" @click="handleDelete(scope.$index, scope.row)" plain></el-button>
                    </template>
                  </el-table-column>
                </el-table>
                <el-button type="primary" @click="handleAdd">新增</el-button>
            </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
        v-for="item in patrolDataTypeOptions"
        :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', 'patrolDataType:edit', 'patrolDataType:del'])"
        label="操作"
        width="125"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation :data="scope.row" :permission="permission" />
          <el-button v-on="patrolDataTypeList">巡检项目</el-button>
        </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 DateRangePicker from '@/components/DateRangePicker'
import crudElement from '@/api/visual/element'
import { patrolDataTypeOptions } from '../tableConfig'
//import VisualMap from '@/components/RelativeMap'
import { initData } from '@/api/data'

const defaultForm = {
  //主要参数
  id: '',
  name: '',
  dataType:'1',
  flag: '1',
  attribute: '1',
  unit: '',
  optionItems:[
    {name:'1231',sort:'1'},
    {name:'dew',sort:'2'},
  ],

  symbol: '',
  content: '',
  orderNo: '',
}
export default {
  name: 'patrolDataType',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: {
    ...crudComps,
    DateRangePicker,
  },
  cruds() {
    return CRUD({
      title: '数据内容',
      url: 'visualization/element/getList',
      crudMethod: { ...crudElement },
      optShow: { add: true, edit: true, del: true, reset: true, },
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'patrolDataType:add'],
        edit: ['admin', 'patrolDataType:edit'],
        del: ['admin', 'patrolDataType:del']
      },
      patrolDataTypeOptions,
      rules: {
        name: [{ required: true, message: '请输入巡检项目', trigger: 'blur' }],
        dataType: [{ required: true, message: '请输入巡检标准', trigger: 'blur' }],
        flag: [{ required: true, trigger: 'blur' }],
        type: [{ required: true, message: '请输入巡检标准', trigger: 'blur' }],
      },
      dataTypeList:[
        {label:"状态",value:"1"},
        {label:"数值",value:"2"},
      ],
      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:[],
    }
  },
  created() {},
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    }
  },
  methods: {
    // 新增与编辑前做的操作
    [CRUD.HOOK.beforeToAdd](crud, form) {
      if (!crud.query.layerID) {
        this.$message.warning('请先选择类型！')
        return false
      } else {
        form.layerID = crud.query.layerID
      }
    },
    // 提交前做的操作
    [CRUD.HOOK.afterValidateCU](crud) {
      
    },
    checkboxT(row, rowIndex) {
      return true
      // return row.id !== this.userInfo.id
    },
    handleAdd(){
      this.form.optionItems.push({
        name:'',
        sort:this.form.optionItems.length+1
      })
    },
    handleUp(index, row) {
      if (index > 0) {
          let upData = this.form.optionItems[index - 1];
          this.form.optionItems.splice(index - 1, 1);
          this.form.optionItems.splice(index, 0, upData);
          this.form.optionItems[index-1].sort=index;
          this.form.optionItems[index].sort=index+1;
      } else {
          this.$message({
              message: '已经是第一条，上移失败',
              type: 'warning'
          });
      }
    },
    handleDown(index, row) {
      if ((index + 1) == this.form.optionItems.length) {
          this.$message({
              message: '已经是最后一条，下移失败',
              type: 'warning'
          });
      } else {
          let downData = this.form.optionItems[index + 1];
          this.form.optionItems.splice(index + 1, 1);
          this.form.optionItems.splice(index, 0, downData);
          this.form.optionItems[index].sort=index+1;
          this.form.optionItems[index+1].sort=index+2;
      }
    },
    handleDelete(index, row) {
      this.form.optionItems.splice(index, 1)
    },
  }
}
</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>
