<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')"
            prop="start_point_code"
          >
            <el-select
              v-model="form.start_point_code"
              style="width: 370px;"
              filterable
              :placeholder="$t('task.select.Placeholder')"
              @change="showStartStorage"
            >
              <el-option
                v-for="item in deviceList"
                :key="item.device_code"
                :label="item.device_code"
                :value="item.device_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            :label="$t('angle.table.next_device_code')"
            prop="next_point_code"
          >
            <el-select
              v-model="form.next_point_code"
              style="width: 370px;"
              filterable
              :placeholder="$t('task.select.Placeholder')"
              @change="showEndStorage"
            >
              <el-option
                v-for="item in deviceList"
                :key="item.device_code"
                :label="item.device_code"
                :value="item.device_code"
              />
            </el-select>
          </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'
import deviceCrud from '@/api/acs/device/device'
import routeCurd from '@/api/acs/route/routePlan'
import { getDicts } from '@/views/system/dict/dict'

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']
      },
      form: {
        deviceList: [],
        task_id: null,
        vehicle_code: null,
        vehicle_type: null,
        task_type: '1',
        storage_task_type: '',
        task_status: null,
        priority: 1,
        start_point_code: null,
        start_point_code2: null,
        start_device_code: null,
        start_device_code2: null,
        next_point_code: null,
        next_point_code2: null,
        remark: null,
        material: null,
        route_plan_code: 'normal',
        from_x: null,
        from_y: null,
        from_z: null,
        to_x: null,
        to_y: null,
        to_z: null,
        from_x2: null,
        from_y2: null,
        from_z2: null,
        to_x2: null,
        to_y2: null,
        to_z2: null,
        agv_system_type: '1',
        interactionJson: null
      },
      rules: {
        start_point_code: [
          { required: true, message: '起点不能为空', trigger: 'change' }
        ],
        next_point_code: [
          { required: true, message: '终点不能为空', trigger: 'change' }
        ],
        is_active: [
          {
            required: true,
            message: '是否启用不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    deviceCrud.selectDeviceList().then(data => {
      this.deviceList = data
    })
    routeCurd.selectList().then(data => {
      this.routeList = data
    })
    getDicts().then(data => {
      this.dicts = data
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    showStartStorage(val) {
      let obj = {}
      obj = this.deviceList.find((item) => {
        return item.device_code === val
      })
      if (obj.device_type === 'storage') {
        this.start_flag = true
        let storage_obj = {}
        deviceCrud.queryStorageExtra(obj.device_code).then(data => {
          storage_obj = data
          const n1 = storage_obj.minY
          const n2 = storage_obj.maxY
          const m1 = storage_obj.minZ
          const m2 = storage_obj.maxZ
          const from_y = []
          const from_z = []
          for (let i = n1; i <= n2; i++) {
            const y = {}
            if (i < 10) {
              y.id = '0' + i
            } else {
              y.id = i
            }
            y.value = i + '列'
            from_y.push(y)
          }
          for (let i = m1; i <= m2; i++) {
            const z = {}
            if (i < 10) {
              z.id = '0' + i
            } else {
              z.id = i
            }
            z.value = i + '层'
            from_z.push(z)
          }
          this.fromYList = from_y
          this.fromZList = from_z
          this.form.from_x = storage_obj.tunnel
        })
      } else {
        this.start_flag = false
        this.form.from_x = ''
        this.form.from_y = ''
        this.form.from_z = ''
      }
      this.isDisabled = false
    },
    updateIsOn(id, is_active) {
      crudAcsPointAngle.updateOn(id, is_active).then(res => {
        this.notify('保存成功', 'success')
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    showEndStorage(val) {
      let obj = {}
      obj = this.deviceList.find((item) => {
        return item.device_code === val
      })
      if (obj.device_type === 'storage') {
        this.end_flag = true
        let storage_obj = {}
        deviceCrud.queryStorageExtra(obj.device_code).then(data => {
          storage_obj = data
          const n1 = storage_obj.minY
          const n2 = storage_obj.maxY
          const m1 = storage_obj.minZ
          const m2 = storage_obj.maxZ
          const to_y = []
          const to_z = []
          for (let i = n1; i <= n2; i++) {
            const y = {}
            if (i < 10) {
              y.id = '0' + i
            } else {
              y.id = i
            }
            y.value = i + '列'
            to_y.push(y)
          }
          for (let i = m1; i <= m2; i++) {
            const z = {}
            if (i < 10) {
              z.id = '0' + i
            } else {
              z.id = i
            }
            z.value = i + '层'
            to_z.push(z)
          }
          this.toYList = to_y
          this.toZList = to_z
          this.form.to_x = storage_obj.tunnel
        })
      } else {
        this.end_flag = false
        this.form.to_x = ''
        this.form.to_y = ''
        this.form.to_z = ''
      }
      this.isDisabled = false
    }
  }
}
</script>

<style scoped>

</style>
