<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-width="80px"
          label-suffix=":"
        >
          <el-form-item label="设备编码">
            <el-input
              v-model="query.device_code"
              clearable
              size="mini"
              placeholder="请输入设备编码、名称"
              style="width: 230px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>

          <el-form-item label="校验日期">
            <el-date-picker v-model="query.beginuse_date" type="date" placeholder="选择日期" style="width: 230px" value-format="yyyy-MM-dd" />
          </el-form-item>

          <el-form-item label="检定结果">
            <el-select
              v-model="query.docimasy_result"
              clearable
              size="mini"
              placeholder="请选择"
              class="filter-item"
              style="width: 200px;"
              @change="hand"
            >
              <el-option
                v-for="item in resultList"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <rrOperation />
          </el-form-item>
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0 || crud.status.view" :title="crud.status.title" width="1100px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-row>
            <el-col :span="8">
              <el-form-item label="量具编码:" prop="device_code">
                <el-input v-model="form.device_code" style="width: 200px;" :disabled="crud.status.view > 0" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="计量器具名称:" prop="device_name">
                <el-input v-model="form.device_name" style="width: 200px;" :disabled="crud.status.view > 0" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="规格型号:" prop="device_model">
                <el-input v-model="form.device_model" style="width: 200px;" :disabled="crud.status.view > 0" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="8">
              <el-form-item label="范围精度:" prop="range_scope">
                <el-input v-model="form.range_scope" style="width: 200px;" :disabled="crud.status.view > 0" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="量具厂家:" prop="manufacturer">
                <el-input v-model="form.manufacturer" style="width: 200px;" :disabled="crud.status.view > 0" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="出厂编号:" prop="leavefactory_number">
                <el-input v-model="form.leavefactory_number" style="width: 200px;" :disabled="crud.status.view > 0" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="8">
              <el-form-item label="检定周期:" prop="docimasy_period">
                <el-input v-model="form.docimasy_period" style="width: 200px;" placeholder="天" :disabled="crud.status.view > 0" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="下次检定日期:" prop="beginuse_date">
                <el-date-picker v-model="form.beginuse_date" type="date" placeholder="选择日期" style="width: 200px" value-format="yyyy-MM-dd" :disabled="crud.status.view > 0" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="管理状态:" prop="manage_status">
                <el-input v-model="form.manage_status" style="width: 200px;" :disabled="crud.status.view > 0" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="8">
              <el-form-item label="校准机构:" prop="check_org">
                <el-input v-model="form.check_org" style="width: 200px;" :disabled="crud.status.view > 0" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="检定结果:" prop="docimasy_result">
                <el-select
                  v-model="form.docimasy_result"
                  :disabled="crud.status.view > 0"
                  clearable
                  size="mini"
                  placeholder="请选择"
                  class="filter-item"
                  style="width: 200px;"
                >
                  <el-option
                    v-for="item in resultList"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="使用人:" prop="use_person">
                <el-input v-model="form.use_person" style="width: 200px;" :disabled="crud.status.view > 0" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="8">
              <el-form-item label="备注:" prop="remark">
                <el-input type="textarea" v-model="form.remark" style="width: 555px;" :disabled="crud.status.view > 0" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :disabled="crud.status.view > 0" :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" :row-style="tableRowClassName" v-loading="crud.loading" :data="crud.data" size="mini" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="false" prop="devicebill_id" label="设备标识" />
        <el-table-column show-overflow-tooltip prop="device_code" label="设备编码" width="130px">
          <template slot-scope="scope">
            <el-link type="warning" @click="crud.toView(scope.row)">{{ scope.row.device_code }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="device_name" label="设备名称" />
        <el-table-column prop="device_model" label="规格型号" />
        <el-table-column prop="range_scope" label="范围精度" /><el-table-column prop="manufacturer" label="量具厂家" />
        <el-table-column prop="leavefactory_number" label="出厂编号" />
        <el-table-column prop="docimasy_period" label="检定周期" />
        <el-table-column prop="beginuse_date" label="下次检定日期" width="150px"/>
        <el-table-column prop="manage_status" label="管理状态" />
        <el-table-column prop="check_org" label="校准机构" />
        <el-table-column prop="docimasy_result" label="检定结果" :formatter="formatResultName"/>
        <el-table-column prop="use_person" label="使用人" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column v-permission="[]" label="操作" width="120px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudInstrumentaccount from '@/api/wms/re/instrumentaccount'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { devicebill_id: null, device_code: null, device_name: null, device_model: null, range_scope: null, manufacturer: null, leavefactory_number: null, docimasy_period: null, beginuse_date: null, manage_status: null, check_org: null, docimasy_result: null, use_person: null, remark: null, update_optid: null, update_optname: null, update_time: null, is_delete: null }
export default {
  name: 'Instrumentaccount',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '量具台账',
      url: 'api/instrumentaccount',
      idField: 'devicebill_id',
      sort: 'devicebill_id,desc',
      crudMethod: { ...crudInstrumentaccount },
      optShow: {
        add: true,
        edit: false,
        del: false,
        download: true,
        reset: true
      }})
  },
  data() {
    return {
      permission: {
      },
      resultList: [
        { 'value': '01', 'label': '合格' },
        { 'value': '02', 'label': '不合格' }
      ],
      rules: {
        device_code: [
          { required: true, message: '设备代码不能为空', trigger: 'blur' }
        ],
        device_name: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ]
      }}
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    hand(value) {
      this.crud.toQuery()
    },
    formatResultName(row, column) {
      if (row.docimasy_result === '01') {
        return '合格'
      }
      if (row.docimasy_result === '02') {
        return '不合格'
      }
    },
    tableRowClassName({ row, rowIndex }) {
      // 将每行的检验有效期时间格式化
      const vDate = row.beginuse_date
      const vDateStr = vDate.replace(/-/g, '')
      // 获取当前时间并格式化
      var myDate = new Date()
      // 以下操作是为了补时间缺的0
      const month = myDate.getMonth() + 1
      const date = myDate.getDate()
      const  stryear = myDate.getFullYear().toString()
      const strmonth = (myDate.getMonth() + 1).toString()
      const strdate = myDate.getDate().toString()
      var c
      var x
      var b
      b = stryear
      if (month < 10) {
        c = ('0' + strmonth)
      } else {
        c = strmonth
      }
      if (date < 10) {
        x = ('0' + strdate)
      } else {
        x = strdate
      }
      const vnowDate = b + c + x
      // 将字符串转换成int类型进行相减
      const a = parseInt(vDateStr) - parseInt(vnowDate)
      // 进行比较符合条件表格就变化颜色
      const stylejson = {}
      if (a < 15) {
        stylejson.background = '#e0838f'
        stylejson.color = 'green'
        return stylejson
      }
    }
  }
}
</script>

<style scoped>

</style>
