<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-form
          :inline="true"
          class="demo-form-inline"
          label-position="right"
          label-suffix=":"
        >
          <el-form-item :label="$t('route.table_title.type')">
            <el-select
              v-model="query.route_plan_id"
              :placeholder="$t('route.placeholder.please_type')"
              style="width: 250px;"
            >
              <el-option
                v-for="item in routePlanList"
                :key="item.plan_uuid"
                :label="item.plan_name"
                :value="item.plan_uuid"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('route.table_title.device_code')">
            <el-input
              v-model="query.device_code"
              clearable
              size="small"
              :placeholder="$t('route.placeholder.enter_code')"
              style="width: 250px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('route.table_title.next_device_code')">
            <el-input
              v-model="query.next_device_code"
              clearable
              size="small"
              :placeholder="$t('route.placeholder.enter_next_device_code')"
              style="width: 250px;"
              class="filter-item"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <rrOperation />
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <!-- <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-refresh"
          @click="reload"
        >
          {{ $t('auto.common.synchronous') }}
        </el-button> -->
      </crudOperation>
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu"
        :title="crud.status.title"
        width="600px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="170px">
          <el-form-item v-if="false" label="路线标识" prop="line_uuid">
            <el-input v-model="form.line_uuid" style="width: 370px;" />
          </el-form-item>

          <el-form-item :label="$t('route.table_title.route_plan_id')" prop="route_plan_id">
            <el-select
              v-model="form.route_plan_id"
              placeholder=""
              style="width: 370px;"
            >
              <el-option
                v-for="item in routePlanList"
                :key="item.plan_uuid"
                :label="item.plan_name"
                :value="item.plan_uuid"
              />
            </el-select>
          </el-form-item>

          <el-form-item :label="$t('route.table_title.device_code')" prop="device_code">
            <el-select
              ref="test"
              v-model="form.device_code"
              filterable
              clearable
              multiple
              reserve-keyword
              :placeholder="$t('auto.common.please')"
              style="width: 370px;"
            >
              <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('route.table_title.next_device_code')" prop="next_device_code">
            <el-select
              v-model="form.next_device_code"
              filterable
              clearable
              reserve-keyword
              multiple
              :placeholder="$t('auto.common.please')"
              style="width: 370px;"
            >
              <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('route.table_title.type')" prop="device_type">
            <el-select
              v-model="form.type"
              placeholder=""
              style="width: 370px;"
            >
              <el-option
                v-for="item in deviceType"
                :key="item.typeValue"
                :label="item.typeName"
                :value="item.typeValue"
              />
            </el-select>
          </el-form-item>

          <el-form-item :label="$t('route.table_title.weights')" prop="weights">
            <el-input-number v-model="form.weights" :min="1" :max="10" label="描述文字" />
          </el-form-item>
          <el-form-item :label="$t('route.table_title.remark')" prop="description">
            <el-input v-model="form.remark" style="width: 370px;" rows="1" type="textarea" />
          </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"
        fit="fit"
        size="mini"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="false" prop="line_uuid" label="路线标识" />
        <el-table-column
          prop="device_code"
          :label="$t('device.device_table_title.device_encoding')"
          :min-width="flexWidth('device_code',crud.data,$t('device.device_table_title.device_encoding'))"
        />
        <el-table-column prop="start_device_type" min-width="120" :label="$t('route.table_title.device_type')" />
        <el-table-column v-if="false" prop="device_code" :label="$t('device.device_table_title.device_name')" />
        <el-table-column prop="type" :label="$t('route.table_title.type')">
          <template slot-scope="scope">
            <span v-if="scope.row.type=='0' ">{{ $t('route.table_title.transport_route') }}</span>
            <span v-if="scope.row.type=='1' ">{{ $t('route.table_title.agv_route') }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="next_device_code" min-width="120" :label="$t('route.table_title.next_device_code')" />
        <el-table-column v-if="false" prop="next_device_code" min-width="120" label="后置设备名称" />
        <el-table-column prop="next_device_type" min-width="120" :label="$t('route.table_title.next_device_type')" />
        <el-table-column prop="weights" :label="$t('route.table_title.weights')" />

        <el-table-column
          :label="$t('auto.common.is_on')"
          align="center"
          prop="is_active"
          :min-width="flexWidth('is_active',crud.data,$t('auto.common.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="changeEnabled(scope.row)"
            />
          </template>
        </el-table-column>

        <el-table-column prop="update_by" :label="$t('auto.common.update_by')" />
        <el-table-column prop="update_time" min-width="160" :label="$t('auto.common.update_time')" />
        <el-table-column
          v-permission="['admin','routeLine:edit','routeLine:del']"
          :label="$t('auto.common.Operate')"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
              :is-visiable-edit="false"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudRouteLine from '@/api/acs/route/routeLine'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { selectList } from '@/api/acs/route/routePlan'
import deviceCrud from '@/api/acs/device/device'
import i18n from '@/i18n'

const defaultForm = {
  line_uuid: null,
  device_code: null,
  next_device_code: null,
  weights: 5,
  points: null,
  route_plan_id: '690e45b400d94185a7ad1eac24d43ef7',
  type: '0',
  remark: null,
  is_active: null,
  is_delete: null,
  create_by: null,
  create_time: null,
  update_by: null,
  update_time: null
}
export default {
  name: 'RouteLine',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: i18n.t('route.title1'),
      url: 'api/routeLine',
      idField: 'line_uuid',
      sort: 'line_uuid,desc',
      crudMethod: { ...crudRouteLine },
      optShow: {
        add: true,
        edit: true,
        del: true,
        download: false
      }
    })
  },
  data() {
    return {
      fit: false,
      routePlanList: [],
      deviceList: [],
      deviceType: [
        {
          typeName: '输送路由',
          typeValue: '0'
        },
        {
          typeName: 'agv路由',
          typeValue: '1'
        }],
      permission: {
        add: ['admin', 'routeLine:add'],
        edit: ['admin', 'routeLine:edit'],
        del: ['admin', 'routeLine:del']
      },
      rules: {
        route_plan_id: [
          { required: true, message: '路由类型不能为空', trigger: 'blur' }
        ],
        device_code: [
          { required: true, message: '设备编码不能为空', trigger: 'blur' }
        ],
        next_device_code: [
          { required: true, message: '下一设备不能为空', trigger: 'blur' }
        ],
        weights: [
          { required: true, message: '权值不能为空', trigger: 'blur' }
        ]
      }
    }
  },

  created() {
    this.$nextTick(() => {
      selectList().then(data => {
        this.routePlanList = data
      })
      deviceCrud.selectDeviceList().then(data => {
        this.deviceList = data
      })
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeToEdit](data) {
      this.form.device_code = Array.of(data.form.device_code.split('').join(''))
      this.form.next_device_code = Array.of(data.form.next_device_code.split('').join(''))
      return true
    },
    [CRUD.HOOK.beforeSubmit](data) {
      if (this.crud.status.edit === 1) {
        this.form.device_code = data.form.device_code.join('')
        this.form.next_device_code = data.form.next_device_code.join('')
      }
      return true
    },
    // 改变状态
    changeEnabled(data) {
      var msg = '停用'
      if (data.is_active === 1) {
        msg = '启用'
      }
      crudRouteLine.enabled(data).then(res => {
        this.crud.notify(msg + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      }).catch(() => {
        data.enabled = !data.enabled
      })
      /* this.$confirm('此操作将 "' + msg + '" ' + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

      }).catch(() => {
        data.enabled = !data.enabled
      })*/
    },
    reload() {
      crudRouteLine.reload().then(res => {
        this.crud.toQuery()
      }).catch(err => {
        console.log(err.response.data.message)
      })
    }
  }
}
</script>

<style scoped>

</style>
