<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.start_code"
          size="small"
          clearable
          placeholder="输入起点"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.next_code"
          size="small"
          clearable
          placeholder="输入终点"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
      </div>
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="650px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="160px"
        >
          <el-form-item
            v-if="false"
            label="id标识"
          >
            <el-input v-model="form.id"  style="width: 370px;" />
          </el-form-item>
          <el-form-item
            label="起始设备编码"
          >
            <el-input v-model="form.start_device_code" :disabled="crud.status.edit > 0" style="width: 370px;" />
          </el-form-item>
          <el-form-item
            label="目标设备编码"
          >
            <el-input v-model="form.next_device_code" :disabled="crud.status.edit > 0" style="width: 370px;" />
          </el-form-item>
          <el-form-item
            label="目标点位角度"
          >
            <el-input v-model="form.next_point_angle" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column
          v-if="false"
          prop="id"
          label="id标识"
        />
        <el-table-column
          prop="start_device_code"
          label="起始设备编码"
        />
        <el-table-column
          prop="next_device_code"
          label="目标设备编码"
        />
        <el-table-column
          prop="next_point_angle"
          label="旋转角度"
        />
        <el-table-column
          prop="is_active"
          label="是否启用"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_active"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
              @change="updateIsOn(scope.row.id,scope.row.is_active)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="create_by"
          label="创建者"
        />
        <el-table-column
          prop="create_time"
          label="创建时间"
        />
        <el-table-column
          prop="update_by"
          label="修改者"
        />
        <el-table-column
          prop="update_time"
          label="修改时间"
        />
        <el-table-column
          v-permission="['admin','acsPointAngle:edit','acsPointAngle:del']"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import crudAcsPointAngle from '@/api/acs/angle/acsPointAngle'

const defaultForm = {
  id: null,
  start_point_code: null,
  start_device_code: null,
  next_point_code: null,
  next_device_code: null,
  start_point_angle: null,
  next_point_angle: null,
  is_active: null,
  create_by: null,
  create_time: null,
  update_by: null,
  update_time: null
}
export default {
  name: 'AcsPointAngle',
  components: { pagination, crudOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '点位角度',
      url: 'api/acsPointAngle',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudAcsPointAngle }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'acsPointAngle:add'],
        edit: ['admin', 'acsPointAngle:edit'],
        del: ['admin', 'acsPointAngle:del']
      },
      rules: {
        is_active: [
          {
            required: true,
            message: '是否启用不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    updateIsOn(id, is_active) {
      crudAcsPointAngle.updateOn(id, is_active).then(res => {
        this.notify('保存成功', 'success')
      }).catch(err => {
        console.log(err.response.data.message)
      })
    }
  }
}
</script>

<style scoped>

</style>
