<template>
  <div class="app-container container-flex">
    <el-card style="width: 350px; margin-right: 15px">
      <el-tree
        :data="optionsObj.EquipmentType"
        default-expand-all
        highlight-current
        @node-click="handleNodeClick"
      ></el-tree>
    </el-card>
    <!--工具栏-->

    <!-- 表单组件 -->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crudCU"
      :title="crud.status.title"
      width="900px"
    >
      <el-descriptions :column="2" style="margin: 20px;">
        <el-descriptions-item
          label="设备编号"
          label-class-name="descriptions-label"
          content-class-name="descriptions-content"
          >{{ form.code }}</el-descriptions-item
        >
        <el-descriptions-item
          label="设备名称"
          label-class-name="descriptions-label"
          content-class-name="descriptions-content"
          >{{ form.name }}</el-descriptions-item
        >
        <el-descriptions-item
          label="设备类型"
          label-class-name="descriptions-label"
          content-class-name="descriptions-content"
          >{{ form.typeStr }}</el-descriptions-item
        >
        <el-descriptions-item
          label="设备型号"
          label-class-name="descriptions-label"
          content-class-name="descriptions-content"
          >{{ form.model }}</el-descriptions-item
        >
      </el-descriptions>
      <calibration-record :equipmentId="form.id" :crudcu="crudCU" />
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU"> 取消 </el-button>
      </div>
    </el-dialog>
    <el-card style="flex-grow: 1">
      <div class="head-container">
        <div v-if="crud.props.searchToggle">
          <!-- 搜索 -->
          <el-input
            v-model="query.code"
            clearable
            size="small"
            placeholder="输入编号搜索"
            style="width: 200px"
            class="filter-item"
            @keyup.enter.native="crud.toQuery"
          />
          <el-input
            v-model="query.name"
            clearable
            size="small"
            placeholder="输入名称搜索"
            style="width: 200px"
            class="filter-item"
            @keyup.enter.native="crud.toQuery"
          />
          <el-input
            v-model="query.model"
            clearable
            size="small"
            placeholder="输入规格型号"
            style="width: 200px"
            class="filter-item"
            @keyup.enter.native="crud.toQuery"
          />
          <el-autocomplete
            v-model="query.calibrationLastManufacturer"
            class="filter-item"
            size="small"
            clearable
            :fetch-suggestions="manufacturerSearch"
            placeholder="输入最近检定厂家"
            @select="crud.toQuery"
            @change="crud.toQuery"
          ></el-autocomplete>
          <el-select
            v-model="query.status"
            filterable
            clearable
            size="small"
            placeholder="请选择运行状态"
            class="filter-item"
            style="width: 160px"
            @change="crud.toQuery"
          >
            <el-option
              v-for="item in optionsObj.EquipmentStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-cascader
            v-model="query.departmentId"
            :options="optionsObj.Department"
            :props="{ checkStrictly: true, emitPath: false }"
            filterable
            clearable
            size="small"
            placeholder="请选择所属部门"
            style="width: 160px"
            class="filter-item"
          />
          <el-select
            v-model="query.areaId"
            filterable
            clearable
            size="small"
            placeholder="请输入所属区域"
            class="filter-item"
            style="width: 160px"
            @change="crud.toQuery"
          >
            <el-option
              v-for="item in optionsObj.Area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <date-range-picker
            type="datetimerange"
            class="date-item"
            start-placeholder="最近检定开始时间"
            end-placeholder="最近检定结束时间"
            style="width: 400px !important"
            @change="handleDatetimeRange1"
          />
          <date-range-picker
            type="datetimerange"
            class="date-item"
            start-placeholder="下一次检定开始时间"
            end-placeholder="下一次检定结束时间"
            style="width: 400px !important"
            @change="handleDatetimeRange2"
          />
          <rrOperation />
        </div>
        <crudOperation :permission="permission">
          <el-button
            slot="left"
            class="filter-item"
            size="mini"
            type="info"
            icon="el-icon-view"
            @click="toEdit1"
            :disabled="crud.selections.length !== 1"
          >
            查看
          </el-button>
        </crudOperation>
      </div>
      <!--表格渲染-->
      <el-table
        v-loading="crud.loading"
        :data="crud.data"
        row-key="id"
        style="width: 100%"
        :tree-props="{ children: 'children', hasChildren: 'nohasChildren' }"
        @select="crud.selectChange"
        @select-all="crud.selectAllChange"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column
          v-for="item in calibrationTableOptions"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
          :width="item.width"
          :align="item.align"
          :fixed="item.fixed"
          :sortable="item.sortable"
        >
        </el-table-column>
        <!--   编辑与删除   -->
        <el-table-column
          v-if="checkPer(['admin', 'equipment:calibration:edit', 'equipment:calibration:del'])"
          label="操作"
          width="70px"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <div style="display: flex">
              <el-button
                :loading="crud.status.cu === 2"
                size="mini"
                type="info"
                icon="el-icon-view"
                @click="toEdit2(scope.row, $event)"
                style="margin-right: 5px"
              />
              <udOperation :data="scope.row" :permission="permission" />
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </el-card>
  </div>
</template>

<script>
import permission from '@/directive/permission'
import { getOptions } from '@/api/equipment/public'
import DateRangePicker from '@/components/DateRangePicker'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import { calibrationTableOptions } from '../tableConfig'
import CalibrationRecord from './calibrationRecord'

// vue-treeselect
import Treeselect from '@riophae/vue-treeselect'
import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

import validate from '@/utils/validate'
import dayjs from 'dayjs'

const defaultForm = {
  id: '',
  code: '',
  name: '',
  typeStr: '',
  model: '',
}

export default {
  name: 'EquipmentCalibration',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: {
    ...crudComps,
    Treeselect,
    DateRangePicker,
    CalibrationRecord,
  },
  cruds() {
    return CRUD({
      title: '设备检定',
      url: 'equipment/calibration/getList',
      listOrder: [],
      query: { status: '170001' },
      optShow: { add: false, edit: false, del: false, reset: false, import: false, export: false },
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    },
  },
  watch: {},
  data() {
    return {
      calibrationTableOptions,
      permission: {
        add: ['admin', 'equipment:calibration:add'],
        edit: ['admin', 'equipment:calibration:edit'],
        del: ['admin', 'equipment:calibration:del'],
      },
      rules: {},
      optionsObj: {
        Department: [],
        Area: [],
        EquipmentType: [],
        EquipmentStatus: [],
        CalibrationManufacturer: [],
      },
      currType: 0,
      attUploading: false,
      manufacturers: [],
    }
  },
  created() {
    this.getOptions()
  },
  methods: {
    [CRUD.HOOK.beforeToAdd]() {},
    [CRUD.HOOK.beforeToCU]() {},
    handleNodeClick(data) {
      this.currType = data.value
      this.query.typeId = data.value
      this.crud.toQuery()
    },
    normalizer(node) {
      return {
        id: node.value,
        label: node.label,
        children: node.children,
      }
    },
    handleDatetimeRange1(range) {
      if (range) {
        this.query.lastStartTime = range[0]
        this.query.lastEndTime = range[1]
      }
    },
    handleDatetimeRange2(range) {
      if (range) {
        this.query.nextStartTime = range[0]
        this.query.nextEndTime = range[1]
      }
    },
    manufacturerSearch(value, callback) {
      let options = this.optionsObj.CalibrationManufacturer
      let results = value
        ? options
            .filter((v) => v.value.toLowerCase().indexOf(value.toLowerCase()) >= 0)
            .map((a) => {
              value: a
            })
        : options
      callback(results)
    },
    toEdit1(evt) {
      this.crud.toEdit(this.crud.selections[0])
      let target = evt.target
      if (target.nodeName == 'SPAN') {
        target = evt.target.parentNode
      }
      target.blur()
    },
    toEdit2(row, evt) {
      this.crud.toEdit(row)
      let target = evt.target
      if (target.nodeName == 'SPAN') {
        target = evt.target.parentNode
      }
      target.blur()
    },
    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)
        }
      })
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.container-flex {
  display: flex;
}
::v-deep .descriptions-label {
  width: 80px;
  height: 30px;
  font-size: 15px;
  font-weight: bold
}
::v-deep .descriptions-content {
  height: 30px;
  font-size: 15px;
}
</style>