<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <date-range-picker
          type="datetimerange"
          v-model="query.time"
          class="date-item"
          style="width: 360px !important"
        />
        <el-select
          v-model="query.type"
          filterable
          clearable
          size="small"
          placeholder="请选择分类"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item of options.typeArr"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.level"
          filterable
          clearable
          size="small"
          placeholder="请选择等级"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item of options.levelArr"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <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 of options.statusArr"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation />
      </div>
      <crudOperation :permission="permission"></crudOperation>
    </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="550px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
        <el-form-item label="分类/专业" prop="type">
          <el-select
            v-model="form.type"
            filterable
            size="small"
            placeholder="请选择分类/专业"
            class="filter-item"
            style="width: 370px"
          >
            <el-option
              v-for="item in options.typeArr"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="位置/设备/设施" prop="name">
          <el-input v-model="form.name" style="width: 370px" />
        </el-form-item>
        <el-form-item label="等级" prop="level">
          <el-select
            v-model="form.level"
            filterable
            size="small"
            placeholder="请选择类型"
            class="filter-item"
            style="width: 370px"
          >
            <el-option
              v-for="item in options.levelArr"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="缺陷内容" prop="version">
          <el-input type="textarea" rows="5" v-model="form.version" style="width: 370px" />
        </el-form-item>
        <el-form-item label="发现人" prop="person">
          <el-input v-model="form.person" style="width: 370px" />
        </el-form-item>
        <el-form-item label="发现时间" prop="updateTime">
          <el-date-picker
            v-model="form.updateTime"
            type="datetime"
            placeholder="选择日期"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 370px"
          >
          </el-date-picker>
        </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" 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 type="selection" width="55" />
      <el-table-column label="发现时间" prop="name" align="center"></el-table-column>
      <el-table-column
        label="分类/专业"
        prop="type"
        align="center"
        :filters="options.typeArr"
        :filter-method="filterHandler"
      ></el-table-column>
      <el-table-column
        label="位置/设备/设施"
        prop="level"
        align="center"
        :filters="options.levelArr"
      ></el-table-column>
      <el-table-column label="等级" prop="version" align="center"></el-table-column>
      <el-table-column label="缺陷内容" prop="version" align="center"></el-table-column>
      <el-table-column
        label="处理状态"
        prop="status"
        align="center"
        :filters="options.statusArr"
      ></el-table-column>
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['admin', 'register:edit', 'register:del'])"
        label="操作"
        width="240px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="table-opt-box">
            <div class="slot-btn">
              <el-button size="mini" icon="el-icon-upload2" type="success"></el-button>
              <el-button size="mini" icon="el-icon-download" type="success"></el-button>
            </div>
            <udOperation :data="scope.row" :permission="permission" />
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudRegister from '@/api/defect/register'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import { deleteFile } from '@/api/data'
import { registerList } from '../tableConfig'
import { getToken } from '@/utils/auth'
import DateRangePicker from '@/components/DateRangePicker'
const defaultForm = {
  id: '',
  type: '', // 分类/专业
  position: '', //位置
  equipment: '', // 设备/设施
  level: '', // 缺陷等级/优先级
  content: '', // 缺陷内容
  link: '', // 发现方式/环节
  person: '', //发现人
  findTime: '', //发现时间
  status: '', // 缺陷状态
  startTime: '', // 开始处理时间
  endTime: '', // 处理完成时间
  checkTime: '', // 验收时间
  timeConsuming: '', // 处理耗时
}

export default {
  name: 'register',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker },
  cruds() {
    return CRUD({
      title: '缺陷登记',
      url: 'defect/register/getList',
      listOrder: [],
      crudMethod: { ...crudRegister },
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    },
    token() {
      return getToken() ?? ''
    },
  },
  data() {
    return {
      permission: {
        add: ['admin', 'register:add'],
        edit: ['admin', 'register:edit'],
        del: ['admin', 'register:del'],
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        type: [{ required: true, message: '请选择类型', trigger: 'blur' }],
        mac: [{ required: true, message: '请输入MAC', trigger: 'blur' }],
      },
      options: {
        typeArr: [
          { label: '总部', text: '总部', value: '1' },
          { label: '合资公司', text: '合资公司', value: '2' },
          { label: '管理处', text: '管理处', value: '3' },
          { label: '维抢修中心', text: '维抢修中心', value: '4' },
        ],
        levelArr: [
          { label: '一级', text: '一级', value: '1' },
          { label: '二级', text: '二级', value: '2' },
          { label: '三级', text: '三级', value: '3' },
        ],
        statusArr: [
          { label: '已过期', text: '已过期', value: '1' },
          { label: '即将过期', text: '即将过期', value: '2' },
          { label: '正常', text: '正常', value: '3' },
        ],
      },
      fileList: [],
    }
  },
  methods: {
    [CRUD.HOOK.afterRefresh]() {
      // this.crud.page.total = registerList.length
      // this.crud.data = registerList
      // this.crud.resetDataStatus()
    },
    filterHandler(value, row, column) {
      const property = column['property']
      return row[property] === value
    },
    beforeUpload(file, type) {
      if (file.size > 1024 * 1024 * 10) {
        this.$message.warning('文件最大限制为 10MB')
        return false
      }
      let fileName = file.name
      let fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase()
      const whiteList = ['pdf']
      if (!whiteList.includes(fileExtension)) {
        this.$message.warning(`只能上传扩展名为 ${whiteList.join(' / ')} 的文件`)
        return false
      }
    },
    handleSuccess(res, file, fileList) {
      if (res.code === 20000) {
        console.log(res)
      } else {
        this.$message.error(res.msg)
      }
    },
    handleUploadError(err, file, fileList) {
      console.log(err)
      this.$message.error('上传失败')
    },
    handleUploadRemove(file, fileList) {
      console.log(file)
      const param = { fileModule: 50, filePath: file.response.data }
      deleteFile(param)
        .then((res) => {
          console.log(res)
        })
        .catch((err) => {
          console.log(err)
          this.$message.error('删除文件失败')
        })
    },
    handleUploadExceed(file, fileList) {
      this.$message.warning('超出限制！只能上传一个文件')
    },
    openPDF(row) {
      this.pdfConfig.show = true
    },
  },
}
</script>

<style lang="scss" scoped>
.table-opt-box {
  display: flex;
  justify-content: center;
  .slot-btn {
    .el-button {
      margin-left: 0;
      margin-right: 8px;
    }
  }
}
</style>
