<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
              placeholder="编码/名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="所属区域">
            <el-select
              v-model="query.region_id"
              clearable
              filterable
              placeholder="所属区域"
              @clear="pointTypesList = null"
              @change="getPointStatusAndTypeList(query.region_id, 1)"
            >
              <el-option
                v-for="item in regionList"
                :key="item.region_id"
                :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
              placeholder="点位类型"
              @change="hand"
            >
              <el-option
                v-for="item in pointTypesList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="点位状态">
            <el-select
              v-model="query.point_status"
              clearable
              filterable
              placeholder="点位状态"
              @change="hand"
            >
              <el-option
                v-for="item in dict.point_status"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="锁定类型">
            <el-select
              v-model="query.lock_type"
              clearable
              filterable
              placeholder="锁定类型"
              @change="hand"
            >
              <el-option
                v-for="item in dict.lock_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </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
          v-if="crud.query.is_used == 0"
          slot="right"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-circle-check"
          :disabled="crud.selections.length === 0"
          @click="changeUsed(crud.selections, 1)"
        >
          启用
        </el-button>
        <el-button
          v-if="crud.query.is_used == 1"
          slot="right"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-circle-close"
          :disabled="crud.selections.length === 0"
          @click="changeUsed(crud.selections, 0)"
        >
          禁用
        </el-button>
        <el-button
          v-if="crud.query.lock_type == 1"
          slot="right"
          class="filter-item"
          size="mini"
          type="danger"
          icon="el-icon-circle-check"
          :disabled="crud.selections.length === 0"
          @click="changeLock(crud.selections, 2)"
        >
          锁定
        </el-button>
        <el-button
          v-if="crud.query.lock_type == 2"
          slot="right"
          class="filter-item"
          size="mini"
          type="danger"
          icon="el-icon-circle-close"
          :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="600px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="160px" label-suffix="：">
          <el-form-item label="点位编码" prop="point_code">
            <el-input v-model="form.point_code" :disabled="crud.status.edit" style="width: 300px" />
          </el-form-item>
          <el-form-item label="点位名称" prop="point_name">
            <el-input v-model="form.point_name" style="width: 300px" />
          </el-form-item>
          <el-form-item label="所属区域" prop="region_id">
            <el-select
              v-model="form.region_id"
              placeholder="请选择"
              style="width: 300px"
              clearable
              @change="getPointStatusAndTypeList(form.region_id, 2)"
              @clear="pointTypesDialogList = null"
            >
              <el-option
                v-for="item in regionList"
                :key="item.region_id"
                :label="item.region_name"
                :value="item.region_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="点位类型" prop="point_type">
            <el-select
              v-model="form.point_type"
              placeholder="点位类型"
              style="width: 300px"
            >
              <el-option
                v-for="item in pointTypesDialogList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-show="crud.status.edit" label="点位状态" prop="point_status">
            <el-select
              v-model="form.point_status"
              placeholder="点位状态"
              style="width: 300px"
              clearable
            >
              <el-option
                v-for="item in dict.point_status"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-show="crud.status.edit" label="锁定类型" prop="lock_type">
            <el-select
              v-model="form.lock_type"
              placeholder="锁定类型"
              style="width: 300px"
            >
              <el-option
                v-for="item in dict.lock_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-show="crud.status.edit" label="载具类型" prop="vehicle_type">
            <el-select
              v-model="form.vehicle_type"
              placeholder="载具类型"
              style="width: 300px"
              clearable
            >
              <el-option
                v-for="item in dict.vehicle_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-show="crud.status.edit" label="载具编码" prop="vehicle_code">
            <el-input v-model="form.vehicle_code" placeholder="载具编码" clearable style="width: 300px" />
          </el-form-item>
          <el-form-item label="关联设备" prop="device_code">
            <el-select
              v-model="form.device_code"
              placeholder="关联设备"
              style="width: 300px"
              clearable
            >
              <el-option
                v-for="item in deviceList"
                :key="item.device_id"
                :label="item.device_name"
                :value="item.device_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 300px" rows="2" type="textarea" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="info" @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="点位编码" :min-width="flexWidth('point_code',crud.data,'点位编码')" />
        <el-table-column prop="point_name" label="点位名称" :min-width="flexWidth('point_name',crud.data,'点位名称')" />
        <el-table-column prop="region_name" label="所属区域" :min-width="flexWidth('region_name',crud.data,'所属区域')" />
        <el-table-column prop="point_type_name" label="点位类型" :min-width="flexWidth('point_type_name',crud.data,'点位类型')" />
        <el-table-column prop="point_status" label="点位状态" :min-width="flexWidth('point_status',crud.data,'点位状态')">
          <template slot-scope="scope">
            {{ dict.label.point_status[scope.row.point_status] }}
          </template>
        </el-table-column>
        <el-table-column prop="lock_type" label="锁定类型" :min-width="flexWidth('lock_type',crud.data,'锁定类型')">
          <template slot-scope="scope">
            {{ dict.label.lock_type[scope.row.lock_type] }}
          </template>
        </el-table-column>
        <el-table-column prop="is_used" label="是否启用" :min-width="flexWidth('is_used',crud.data,'是否启用')">
          <template slot-scope="scope">
            {{ dict.label.true_or_false[scope.row.is_used] }}
          </template>
        </el-table-column>
        <el-table-column prop="vehicle_type" label="载具类型" :min-width="flexWidth('vehicle_type',crud.data,'载具类型')">
          <template slot-scope="scope">
            {{ dict.label.vehicle_type[scope.row.vehicle_type] }}
          </template>
        </el-table-column>
        <el-table-column prop="vehicle_code" label="载具编码" :min-width="flexWidth('vehicle_code',crud.data,'载具编码')" />
        <el-table-column prop="material_code" label="物料编码" :min-width="flexWidth('material_code',crud.data,'物料编码')" />
        <el-table-column prop="remark" label="备注" :min-width="flexWidth('remark',crud.data,'备注')" show-overflow-tooltip />
        <el-table-column prop="create_name" label="创建人" :min-width="flexWidth('create_name',crud.data,'创建人')" />
        <el-table-column prop="create_time" label="创建时间" :min-width="flexWidth('create_time',crud.data,'创建时间')" />
        <el-table-column prop="update_optname" label="修改人" :min-width="flexWidth('update_optname',crud.data,'修改人')" />
        <el-table-column prop="update_time" label="修改时间" :min-width="flexWidth('update_time',crud.data,'修改时间')" />
        <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>
    <MaterialDialog :dialog-show.sync="materialDialog" @tableChanged3="tableChanged" />
    <ViewDialog ref="viewDialog" />
  </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'
import ViewDialog from '@/views/wms/sch/point/ViewDialog'
import MaterialDialog from '@/views/wms/sch/point/MaterialDialog'

const defaultForm = {
  point_id: null,
  point_code: null,
  point_name: null,
  point_type: null,
  point_status: '0',
  lock_type: '1',
  vehicle_code: null,
  source_id: null,
  remark: null,
  is_light: '0',
  is_used: '1',
  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', 'lock_type', 'point_status', 'true_or_false'],
  components: { MaterialDialog, pagination, crudOperation, rrOperation, udOperation, ViewDialog },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '点位',
      url: 'api/point',
      idField: 'point_id',
      optShow: {
        add: true,
        edit: false,
        del: true,
        download: false,
        reset: true
      },
      crudMethod: { ...crudPoint }
    })
  },

  data() {
    return {
      syncLoading: false,
      materialDialog: false,
      choose: '物料',
      invtypelist: [],
      pointStatusList: [],
      pointStatusDialogList: [],
      pointTypesList: [],
      pointTypesDialogList: [],
      options: [],
      regionList: [],
      permission: {},
      deviceList: [],
      rules: {
        point_code: [
          { required: true, message: '点位编码不能为空', trigger: 'blur' }
        ],
        point_name: [
          { required: true, message: '点位名称不能为空', trigger: 'blur' }
        ],
        point_status: [
          { 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
    })
    crudPoint.getDevice().then(res => {
      this.deviceList = res
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterToCU]() {
      if (this.form.region_id) {
        this.getPointStatusAndTypeList(this.form.region_id, 2)
      }
    },
    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, flag) {
      if (id) {
        this.getPointTypeList(id, flag)
      }
      if (flag === 1) {
        this.crud.toQuery()
      }
    },
    getPointStatusList(id, flag) {
      crudRegion.getPointStatusSelectById(id).then(res => {
        if (flag === 1) {
          this.pointStatusList = res
        } else {
          this.pointStatusDialogList = res
        }
      })
    },
    getPointTypeList(id, flag) {
      crudRegion.getPointTypeSelectById(id).then(res => {
        if (flag === 1) {
          this.pointTypesList = res
        } else {
          this.pointTypesDialogList = 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()
      })
    },
    toView(row) {
      debugger
      if (row) {
        this.$refs.viewDialog.setParentData(row)
      }
    },
    tableChanged(row) {
      this.form.material_name = row.material_name
      this.form.material_id = row.material_id
    }
  }
}
</script>

<style scoped>

</style>
