<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-autocomplete
          v-model="query.calibrationLastManufacturer"
          class="filter-item"
          size="small"
          clearable
          :fetch-suggestions="manufacturerSearch"
          placeholder="输入最近检定厂家"
          @select="crud.toQuery"
          @change="crud.toQuery"
        ></el-autocomplete>
        <date-range-picker
          type="datetimerange"
          class="date-item"
          v-model="customs.timeRange"
          start-placeholder="检定开始时间"
          end-placeholder="检定结束时间"
          style="width: 400px !important"
          @change="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation :permission="permission" />
    </div>
    <!-- 表单组件 -->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible="crud.status.cu > 0"
      :title="crud.status.title"
      width="600px"
      top="25vh"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
        <el-form-item label="检定日期" prop="calibrationTime">
          <el-date-picker
            type="date"
            v-model="form.calibrationTime"
            value-format="yyyy-MM-dd"
            size="small"
            style="width: 280px"
            @change="handleDateChange"
          />
        </el-form-item>
        <el-form-item label="检定厂家" prop="calibrationManufacturer">
          <el-autocomplete
            v-model="form.calibrationManufacturer"
            size="small"
            :fetch-suggestions="allManufacturerSearch"
          ></el-autocomplete>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" autosize style="width: 280px" />
        </el-form-item>
        <el-form-item label="附件" prop="calibrationAttachment">
          <upload-attachment
            v-model="form.calibrationAttachment"
            :loading.sync="attUploading"
            :maxSize="1024 * 1024 * 1024"
            :limit="10"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU"> 取消 </el-button>
        <el-button
          :loading="crud.status.cu === 2 || attUploading"
          type="primary"
          @click="crud.submitCU"
        >
          确认
        </el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        v-for="item in calibrationRecordTableOptions"
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
        :width="item.width"
        :align="item.align"
        :fixed="item.fixed"
        :sortable="item.sortable"
        show-overflow-tooltip
      >
      </el-table-column>
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['admin', 'equipment:calibration:edit', 'equipment:calibration:del'])"
        label="操作"
        width="130px"
        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 crudCRecord from '@/api/equipment/calibrationRecord'
import DateRangePicker from '@/components/DateRangePicker'
import UploadAttachment from '@/components/UploadAttachment'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudComps from '@crud'
import { getOptions } from '@/api/equipment/public'

// 表头配置
import { calibrationRecordTableOptions } from '../tableConfig'

const defaultForm = {}

export default {
  name: 'EquipmentCalibrationRecord',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker, UploadAttachment },
  cruds() {
    return CRUD({
      title: '设备检定记录',
      url: 'equipment/calibrationRecord/getList',
      crudMethod: { ...crudCRecord },
      queryOnPresenterCreated: false,
      optShow: {
        add: true,
        edit: true,
        del: true,
        reset: false,
        import: false,
        export: false,
      },
    })
  },
  props: {
    equipmentId: {
      type: String,
      require: true,
    },
    crudcu: {
      type: Boolean,
      require: true,
    },
  },
  watch: {
    crudcu(newValue, oldValue) {
      if (!oldValue && newValue) {
        this.query = {}
        this.crud.params.equipmentId = this.equipmentId
        //this.getManufacturers(this.equipmentId)
        this.$refs.table.clearSelection()
        this.crud.toQuery()
      }
    },
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    },
  },
  data() {
    return {
      calibrationRecordTableOptions,
      rules: {
        calibrationTime: [
          {
            required: true,
            message: '请输入检定日期',
            trigger: 'blur',
          },
        ],
        calibrationManufacturer: [
          {
            required: true,
            message: '请输入检定厂家',
            trigger: 'blur',
          },
        ],
      },
      permission: {
        add: ['admin', 'equipment:calibration:add'],
        edit: ['admin', 'equipment:calibration:edit'],
        del: ['admin', 'equipment:calibration:del'],
      },
      optionsObj: {
        Manufacturer: [],
        AllManufacturer: [],
      },
      attUploading: false,
    }
  },
  computed: {
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    },
  },
  created() {
    //this.getManufacturers(this.equipmentId)
    this.crud.params.equipmentId = this.equipmentId
    this.crud.toQuery()
    this.getAllManufacturers()
  },
  methods: {
    [CRUD.HOOK.beforeValidateCU]() {
      this.form.equipmentId = this.equipmentId
    },
    [CRUD.HOOK.afterRefresh]() {
      this.getManufacturers(this.equipmentId)
    },
    manufacturerSearch(value, callback) {
      let options = this.optionsObj.Manufacturer
      let results = value
        ? options
            .filter((v) => v.value.toLowerCase().indexOf(value.toLowerCase()) >= 0)
            .map((a) => {
              value: a
            })
        : options
      callback(results)
    },
    allManufacturerSearch(value, callback) {
      let options = this.optionsObj.AllManufacturer
      let results = value
        ? options
            .filter((v) => v.value.toLowerCase().indexOf(value.toLowerCase()) >= 0)
            .map((a) => {
              value: a
            })
        : options
      callback(results)
    },
    getManufacturers(id) {
      getOptions('CalibrationManufacturer', { id }).then((res) => {
        if (res.code === 20000) this.optionsObj.Manufacturer = res.data.content
      })
    },
    getAllManufacturers() {
      getOptions('CalibrationManufacturer', {}).then((res) => {
        if (res.code === 20000) this.optionsObj.AllManufacturer = res.data.content
      })
    },
    handleDateChange(v) {
      console.log(v)
    }
  },
}
</script>
  
<style rel="stylesheet/scss" lang="scss" scoped>
</style>