<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="输入生产数据名称"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <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.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="540px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
        <el-form-item label="生产数据名称" prop="name">
          <el-input v-model="form.name" style="width: 370px" />
        </el-form-item>
        <el-form-item label="生产数据编号" prop="code">
          <el-input v-model="form.code" style="width: 370px" />
        </el-form-item>
        <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="status">
          <el-select
            v-model="form.status"
            filterable
            size="small"
            placeholder="请选择生产数据状态"
            class="filter-item"
            style="width: 370px"
          >
            <el-option
              v-for="item in options.statusArr"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </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">
        <template slot-scope="scope">
          <span style="color: #409eff; cursor: pointer">
            {{ scope.row.name }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="生产数据编号" prop="code" 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="status"
        align="center"
        :filters="options.statusArr"
      ></el-table-column>
      <el-table-column
        label="更新时间"
        prop="updateTime"
        align="center"
        :sortable="true"
      ></el-table-column>
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['admin', 'production:edit', 'production:del'])"
        label="操作"
        width="240px"
        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 crudPreplan from '@/api/emergency/preplan'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
const defaultForm = {
  id: '',
  name: '', //生产数据内容
  code: '', //生产数据编号
  type: '', //生产数据类型
  updateTime: '', // 生产数据更新时间
  status: '', // 生产数据状态
}

export default {
  name: 'production',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps },
  cruds() {
    return CRUD({
      title: '生产数据数据',
      url: 'emergency/plan/getList',
      listOrder: [],
      crudMethod: { ...crudPreplan },
      queryOnPresenterCreated: true,
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
  },
  data() {
    return {
      permission: {
        add: ['admin', 'production:add'],
        edit: ['admin', 'production:edit'],
        del: ['admin', 'production:del'],
      },
      rules: {
        name: [{ required: true, message: '请输入生产数据名称', trigger: 'blur' }],
        type: [{ required: true, message: '请选择生产数据类型', trigger: 'blur' }],
      },
      options: {
        typeArr: [
          { label: '总部', text: '总部', value: '1' },
          { label: '合资公司', text: '合资公司', value: '2' },
          { label: '管理处', text: '管理处', value: '3' },
          { label: '维抢修中心', text: '维抢修中心', value: '4' },
        ],
        statusArr: [
          { label: '启用', text: '启用', value: '1' },
          { label: '停用', text: '停用', value: '0' },
        ],
      },
    }
  },
  methods: {
    [CRUD.HOOK.afterRefresh]() {
      const mockData = Array.from({ length: 5 }, (t, i) => {
        let sort = i + 1
        return {
          id: sort + '',
          name: `生产数据-${sort}`, //生产数据内容
          code: sort, //生产数据编号
          type: sort, //生产数据类型
          updateTime: '2023-02-14 16:29:12', // 生产数据更新时间
          status: '1', // 生产数据状态
        }
      })
      this.crud.data = mockData
      this.crud.resetDataStatus()
    },
    // 打开
    [CRUD.HOOK.beforeToEdit]() {},
    filterHandler(value, row, column) {
      const property = column['property']
      return row[property] === value
    },
  },
}
</script>

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