<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <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="$t('angle.table.start_device_code')"
          >
            <el-input v-model="form.start_device_code" :disabled="crud.status.edit > 0" style="width: 370px;" />
          </el-form-item>
          <el-form-item
            :label="$t('angle.table.next_device_code')"
          >
            <el-input v-model="form.next_device_code" :disabled="crud.status.edit > 0" style="width: 370px;" />
          </el-form-item>
          <el-form-item
            :label="$t('angle.table.next_point_angle')"
          >
            <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">{{ $t('task.select.Cancel') }}</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">{{ $t('task.select.Confirm') }}</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="$t('angle.table.start_device_code')"
          :min-width="flexWidth('start_device_code',crud.data,$t('angle.table.start_device_code'))"
        />
        <el-table-column
          prop="next_device_code"
          :label="$t('angle.table.next_device_code')"
          :min-width="flexWidth('next_device_code',crud.data,$t('angle.table.next_device_code'))"
        />
        <el-table-column
          prop="next_point_angle"
          :label="$t('angle.table.next_point_angle')"
          :min-width="flexWidth('next_point_angle',crud.data,$t('angle.table.next_point_angle'))"
        />
        <el-table-column
          prop="is_active"
          :label="$t('angle.table.is_on')"
          :min-width="flexWidth('is_active',crud.data,$t('angle.table.is_on'))"
        >
          <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="$t('angle.table.create_by')"
          :min-width="flexWidth('create_by',crud.data,$t('angle.table.create_by'))"
        />
        <el-table-column
          prop="create_time"
          :label="$t('angle.table.create_time')"
          :min-width="flexWidth('create_time',crud.data,$t('angle.table.create_time'))"
        />
        <el-table-column
          prop="update_by"
          :label="$t('angle.table.update_by')"
          :min-width="flexWidth('update_by',crud.data,$t('angle.table.update_by'))"
        />
        <el-table-column
          prop="update_time"
          :label="$t('angle.table.update_time')"
          :min-width="flexWidth('update_time',crud.data,$t('angle.table.update_time'))"
        />
        <el-table-column
          v-permission="['admin','acsPointAngle:edit','acsPointAngle:del']"
          :label="$t('angle.table.operate')"
          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>
