<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-width="80px"
          label-suffix=":"
        >
          <el-form-item label="模糊搜索">
            <el-input
              v-model="query.name"
              clearable
              size="mini"
              placeholder="编码名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="点位类型">
            <el-select
              v-model="query.point_type"
              clearable
              size="mini"
              placeholder="点位类型"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in dict.sch_point_type"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="区域类型">
            <el-select
              v-model="query.region_id"
              clearable
              filterable
              size="mini"
              placeholder="区域类型"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in reginoList"
                :label="item.region_name"
                :value="item.region_id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="点位状态">
            <el-select
              v-model="query.point_status"
              clearable
              filterable
              size="mini"
              placeholder="点位状态"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in dict.sch_point_status"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="锁定类型">
            <el-select
              v-model="query.lock_type"
              clearable
              size="mini"
              placeholder="锁定类型"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in dict.d_lock_type"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="是否启用">
            <el-select
              v-model="query.is_used"
              clearable
              size="mini"
              placeholder="是否启用"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in dict.is_used"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <rrOperation />
        </el-form>

      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
      </crudOperation>
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="520px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="100px">
          <el-form-item v-if="false" label="点位标识" prop="point_id">
            <el-input v-model="form.point_id" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="点位编码" prop="point_code">
            <el-input v-model="form.point_code" style="width: 370px;" :disabled="this.form.point_type==='01'" />
          </el-form-item>
          <el-form-item label="点位名称" prop="point_name">
            <el-input v-model="form.point_name" style="width: 370px;" :disabled="this.form.point_type==='01'" />
          </el-form-item>
          <el-form-item label="点位状态" prop="point_status">
            <el-select
              v-model="form.point_status"
              size="mini"
              placeholder="点位状态"
              class="filter-item"
              style="width: 370px;"
            >
              <el-option
                v-for="item in dict.sch_point_status"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="锁定类型" prop="lock_type">
            <el-select
              v-model="form.lock_type"
              size="mini"
              placeholder="锁定类型"
              class="filter-item"
              style="width: 370px;"
            >
              <el-option
                v-for="item in dict.d_lock_type"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="设备点位类型" prop="device_point_type">
            <el-select
              v-model="form.device_point_type"
              size="mini"
              placeholder="设备点位类型"
              class="filter-item"
              style="width: 370px;"
            >
              <el-option
                v-for="item in dict.device_point_type"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="载具编码" prop="vehicle_code">
            <el-input v-model="form.vehicle_code" clearable style="width: 370px;" />
          </el-form-item>
          <el-form-item label="是否专机" prop="is_host">
            <el-radio-group v-model="form.is_host">
              <el-radio
                v-for="item in dict.IS_HOST"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="点位类型" prop="point_type">
            <el-radio-group v-model="form.point_type">
              <el-radio
                v-for="item in dict.sch_point_type"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="所属区域" prop="region_id">
            <el-select
              v-model="form.region_id"
              placeholder="请选择"
              style="width: 370px;"
            >
              <el-option
                v-for="item in reginoList"
                :label="item.region_name"
                :value="item.region_id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;" rows="2" type="textarea" />
          </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="mini"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="point_code" label="点位编码" sortable width="100" />
        <el-table-column prop="point_name" label="点位名称" width="120" sortable show-overflow-tooltip />
        <el-table-column prop="point_type_name" label="点位类型" />
        <el-table-column prop="point_status_name" label="点位状态" />
        <el-table-column prop="lock_type_name" label="锁定类型" />
        <el-table-column prop="region_name" label="所属区域" width="110" />
        <el-table-column prop="vehicle_code" label="载具编码" />
        <el-table-column label="是否启用" align="center" prop="is_used">
          <template slot-scope="scope">
            <el-switch
              :value="format_is_used(scope.row.is_used)"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              @change="changeEnabled(scope.row, scope.row.is_used)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="taskdtl_type" label="锁定任务类型" width="110" :formatter="taskdtl_typeFormat" />
        <el-table-column prop="task_code" label="锁定任务编码" width="100" />
        <el-table-column prop="inv_code" label="锁定单据编码" width="130" />
        <el-table-column prop="inv_type" label="锁定单据类型" width="110" :formatter="invtypeFormat" />
        <el-table-column prop="vehicle_type" label="托盘类型">
          <template slot-scope="scope">
            {{ dict.label.storagevehicle_type[scope.row.vehicle_type] }}
          </template>
        </el-table-column>
        <el-table-column prop="vehicle_qty" label="载具数量" />
        <el-table-column prop="vehicle_type" label="是否专机">
          <template slot-scope="scope">
            {{ dict.label.IS_HOST[scope.row.is_host] }}
          </template>
        </el-table-column>
        <el-table-column prop="update_optname" label="修改者" />
        <el-table-column prop="update_time" label="修改时间" width="150" />
        <el-table-column
          v-permission="[]"
          label="操作"
          fixed="right"
          width="120px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudPoint, { changeActive } from '@/api/wms/sch/point'
import CRUD, { crud, form, header, presenter } 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 checkoutbill from '@/api/wms/st/core/outbill/checkoutbill'*/

const defaultForm = { device_point_type: null, point_id: null, is_host: null, point_code: null, point_name: null, area_type: null, point_type: '00', point_status: '00', lock_type: '00', vehicle_code: null, source_id: null, remark: null, is_used: null, is_delete: null, create_id: null, create_name: null, create_time: null, update_optid: null, update_optname: null, update_time: null }
export default {
  name: 'Point',
  dicts: ['sch_point_type', 'sch_area_type', 'sch_point_status', 'is_used', 'd_lock_type', 'SCH_TASK_TYPE_DTL', 'storagevehicle_type', 'IS_HOST', 'device_point_type'],
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '点位',
      url: 'api/point',
      idField: 'point_id',
      optShow: {
        add: true,
        edit: true,
        del: true,
        download: false,
        reset: true
      },

      crudMethod: { ...crudPoint }
    })
  },

  data() {
    return {
      syncLoading: false,
      invtypelist: [],
      options: [],
      reginoList: [],
      permission: {},
      rules: {
        point_code: [
          { required: true, message: '点位编码不能为空', trigger: 'blur' }
        ],
        point_name: [
          { required: true, message: '点位名称不能为空', trigger: 'blur' }
        ],
        point_type: [
          { required: true, message: '点位类型不能为空', trigger: 'blur' }
        ],
        region_id: [
          { required: true, message: '区域类型不能为空', trigger: 'blur' }
        ],
        point_status: [
          { required: true, message: '点位状态不能为空', trigger: 'blur' }
        ],
        lock_type: [
          { required: true, message: '锁定类型不能为空', trigger: 'blur' }
        ],
        is_host: [
          { required: true, message: '是否专机不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    crudPoint.getRegion().then(res => {
      this.reginoList = res
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    hand(value) {
      this.crud.toQuery()
    },
    taskdtl_typeFormat(row) {
      return this.dict.label.SCH_TASK_TYPE_DTL[row.taskdtl_type]
    },
    invtypeFormat(row) {
      for (const item of this.invtypelist) {
        if (item.code === row.inv_type) {
          return item.name
        }
      }
    },
    format_is_used(is_used) {
      return is_used === '1'
    },
    changeEnabled(data, val) {
      let msg = '此操作将停用点位，是否继续！'
      if (val !== '1') {
        msg = '此操作将启用点位，是否继续！'
      }
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        changeActive(data).then(res => {
          this.crud.toQuery()
          this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          data.is_used = !data.is_used
        })
      }).catch(() => {
      })
    }

  }
}
</script>

<style scoped>

</style>
