<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="90px"
          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.region_id"
              clearable
              filterable
              size="mini"
              placeholder="区域类型"
              class="filter-item"
              @change="getPointStatusAndTypeList"
            >
              <el-option
                v-for="item in regionList"
                :label="item.region_name"
                :value="item.region_id"
              />
            </el-select>
          </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 pointTypesList"
                :label="item.label"
                :value="item.value"
              />
            </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 pointStatusList"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="块" v-if="(query.region_id=='1557539288307077120' || query.region_id=='1582909373552922624')">
            <el-input
              v-model="query.block_num"
              clearable
              size="mini"
              placeholder="块"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="排" v-if="(query.region_id=='1557539288307077120' || query.region_id=='1582909373552922624' || query.region_id=='1557538851726168064')">
            <el-input
              v-model="query.row_num"
              clearable
              size="mini"
              placeholder="排"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="列" v-if="(query.region_id=='1557539288307077120' || query.region_id=='1582909373552922624' || query.region_id=='1557538851726168064')">
            <el-input
              v-model="query.col_num"
              clearable
              size="mini"
              placeholder="列"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="层" v-if="(query.region_id=='1557539288307077120' || query.region_id=='1582909373552922624' || query.region_id=='1557538851726168064')">
            <el-input
              v-model="query.layer_num"
              clearable
              size="mini"
              placeholder="层"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="锁定类型">
            <el-switch
              v-model="query.lock_type"
              active-value="2"
              inactive-value="1"
              active-color="#409EFF"
              inactive-color="#C0CCDA"
              @change="hand"
            />
          </el-form-item>

          <el-form-item label="是否启用">
            <el-switch
              v-model="query.is_used"
              active-value="0"
              inactive-value="1"
              active-color="#C0CCDA"
              inactive-color="#409EFF"
              @change="hand"
            />
          </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-circle-check"
          v-if="crud.query.is_used == 0"
          :disabled="crud.selections.length === 0"
          @click="changeUsed(crud.selections, 1)"
        >
          启用
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-circle-close"
          v-if="crud.query.is_used == 1"
          :disabled="crud.selections.length === 0"
          @click="changeUsed(crud.selections, 0)"
        >
          禁用
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="danger"
          icon="el-icon-circle-check"
          v-if="crud.query.lock_type == 1"
          :disabled="crud.selections.length === 0"
          @click="changeLock(crud.selections, 2)"
        >
          锁定
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="danger"
          icon="el-icon-circle-close"
          v-if="crud.query.lock_type == 2"
          :disabled="crud.selections.length === 0"
          @click="changeLock(crud.selections, 1)"
        >
          解锁
        </el-button>
      </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="region_id">
            <el-select
              v-model="form.region_id"
              placeholder="请选择"
              style="width: 370px;"
              @change="getPointStatusAndTypeList"
            >
              <el-option
                v-for="item in regionList"
                :label="item.region_name"
                :value="item.region_id"
              />
            </el-select>
          </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;"
              clearable
            >
              <el-option
                v-for="item in pointStatusList"
                :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.point_type"
              size="mini"
              placeholder="设备点位类型"
              class="filter-item"
              style="width: 370px;"
            >
              <el-option
                v-for="item in pointTypesList"
                :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="备注">
            <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="region_name" label="区域名称" min-width="120" 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="vehicle_type" label="载具类型" min-width="120">
          <template slot-scope="scope">
            {{ dict.label.vehicle_type[scope.row.vehicle_type] }}
          </template>
        </el-table-column>
        <el-table-column prop="vehicle_code" label="载具编码" />
        <el-table-column prop="vehicle_qty" label="载具数量" />
        <el-table-column prop="remark" label="备注" min-width="100" show-overflow-tooltip/>
        <el-table-column prop="is_used" label="是否启用">
          <template slot-scope="scope">
            {{ scope.row.is_used=='1' ? '是' : '否' }}
          </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 crudRegion from '@/api/wms/sch/region'
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'

const defaultForm = { point_id: null, point_code: null, point_name: null, point_type: null, point_status: null, lock_type: '1', 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: ['vehicle_type', 'd_lock_type', 'SCH_TASK_TYPE_DTL', 'vehicle_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: [],
      pointStatusList: [],
      pointTypesList: [],
      options: [],
      regionList: [],
      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' }
        ],
        lock_type: [
          { required: true, message: '锁定类型不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    crudPoint.getRegion().then(res => {
      this.regionList = res
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterToCU]() {
      if (this.form.region_id) {
        this.getPointStatusAndTypeList(this.form.region_id)
      }
    },
    hand(value) {
      this.crud.toQuery()
    },
    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(() => {
      })
    },
    getPointStatusAndTypeList(id) {
      if (id) {
        this.getPointStatusList(id)
        this.getPointTypeList(id)
        this.crud.toQuery()
      }
    },
    getPointStatusList(id) {
      crudRegion.getPointStatusSelectById(id).then(res => {
        this.pointStatusList = res
      })
    },
    getPointTypeList(id) {
      crudRegion.getPointTypeSelectById(id).then(res => {
        this.pointTypesList = res
      })
    },
    changeUsed(data, flag) { // 更改启用状态
      const param = {}
      param.data = data
      param.used = flag
      crudPoint.changeUsed(param).then(res => {
        this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    },
    changeLock(data, flag) { // 更改锁定状态
      const param = {}
      param.data = data
      param.lock_type = flag
      crudPoint.changeLock(param).then(res => {
        this.crud.notify('操作成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    }

  }
}
</script>

<style scoped>

</style>
