<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="computedSearchLabelWidth"
          label-suffix=":"
        >
          <el-form-item :label="$t('Point.search.workshop_code')">
            <el-select
              v-model="query.workshop_code"
              clearable
              size="mini"
              :placeholder="$t('Point.search.workshop_code')"
              class="filter-item"
            >
              <el-option
                v-for="item in workShopList"
                :label="item.workshop_name"
                :value="item.workshop_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('Point.search.blurry')">
            <el-input
              v-model="query.blurry"
              clearable
              size="mini"
              :placeholder="$t('Point.search.p1')"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('Point.search.region_code')">
            <el-select
              v-model="query.region_code"
              clearable
              filterable
              size="mini"
              :placeholder="$t('Point.search.region_code')"
              class="filter-item"
              @clear="handleClear"
              @change="getPointStatusAndTypeList(query.region_code, 1)"
            >
              <el-option
                v-for="item in regionList"
                :label="item.region_name"
                :value="item.region_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('Point.search.point_type')">
            <el-select
              v-model="query.point_type"
              clearable
              size="mini"
              :placeholder="$t('Point.search.point_type')"
              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="$t('Point.search.point_status')">
            <el-select
              v-model="query.point_status"
              clearable
              filterable
              size="mini"
              :placeholder="$t('Point.search.point_status')"
              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="$t('Point.search.lock_type')">
            <el-select
              v-model="query.lock_type"
              clearable
              filterable
              size="mini"
              :placeholder="$t('Point.search.lock_type')"
              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="$t('Point.search.is_used')">
            <el-switch
              v-model="query.is_used"
              active-value="true"
              inactive-value="false"
              active-color="#409EFF"
              inactive-color="#C0CCDA"
              @change="hand"
            />
          </el-form-item>
          <rrOperation />
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <el-button
          v-if="crud.query.is_used == 'false'"
          slot="right"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-circle-check"
          :disabled="crud.selections.length === 0"
          @click="changeUsed(crud.selections, true)"
        >
          {{ $t('Point.search.enable') }}
        </el-button>
        <el-button
          v-if="crud.query.is_used == 'true'"
          slot="right"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-circle-close"
          :disabled="crud.selections.length === 0"
          @click="changeUsed(crud.selections, false)"
        >
          {{ $t('Point.search.disable') }}
        </el-button>
        <el-button
          v-if="false"
          slot="right"
          class="filter-item"
          size="mini"
          type="danger"
          icon="el-icon-circle-check"
          :disabled="crud.selections.length === 0"
          @click="changeLock(crud.selections)"
        >
          锁定
        </el-button>
        <el-button
          v-if="false"
          slot="right"
          class="filter-item"
          size="mini"
          type="danger"
          icon="el-icon-circle-close"
          :disabled="crud.selections.length === 0"
          @click="changeLock(crud.selections)"
        >
          解锁
        </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="computedLabelWidth"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="mini"
          :label-width="computedFormLabelWidth"
          style="border: 1px solid #cfe0df;margin-top: 10px;padding-top: 10px;"
        >
          <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="$t('Point.dialog.workshop_code')">
            <el-select
              v-model="form.workshop_code"
              :placeholder="$t('common.Please_select')"
              style="width: 370px;"
            >
              <el-option
                v-for="item in workShopList"
                :label="item.workshop_name"
                :value="item.workshop_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('Point.dialog.region_code')" prop="region_code">
            <el-select
              v-model="form.region_code"
              :placeholder="$t('common.Please_select')"
              style="width: 370px;"
              @change="getPointStatusAndTypeList(form.region_code, 2)"
            >
              <el-option
                v-for="item in regionList"
                :label="item.region_name"
                :value="item.region_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('Point.dialog.point_code')" prop="point_code">
            <el-input v-model="form.point_code" style="width: 370px;" />
          </el-form-item>
          <el-form-item :label="$t('Point.dialog.point_name')" prop="point_name">
            <el-input v-model="form.point_name" style="width: 370px;" />
          </el-form-item>
          <el-form-item v-if="pointStatusDialogList.length > 0" :label="$t('Point.dialog.point_status')" prop="point_status">
            <el-select
              v-model="form.point_status"
              size="mini"
              :placeholder="$t('Point.dialog.point_status')"
              class="filter-item"
              style="width: 370px;"
              clearable
            >
              <el-option
                v-for="item in pointStatusDialogList"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-show="pointTypesDialogList.length > 0" :label="$t('Point.dialog.point_type')" prop="device_point_type">
            <el-select
              v-model="form.point_type"
              size="mini"
              :placeholder="$t('Point.dialog.point_type')"
              class="filter-item"
              style="width: 370px;"
            >
              <el-option
                v-for="item in pointTypesDialogList"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('Point.dialog.parent_point_code')" prop="vehicle_code">
            <el-input v-model="form.parent_point_code" clearable style="width: 370px;" @focus="getParentPoint" />
          </el-form-item>
          <el-form-item v-if="form.point_status !== '1'" :label="$t('Point.dialog.vehicle_code')" prop="vehicle_code">
            <el-input v-model="form.vehicle_code" clearable style="width: 370px;" />
          </el-form-item>
          <el-form-item v-if="form.point_status !== '1'" :label="$t('Point.dialog.vehicle_type')" prop="vehicle_code">
            <el-select v-model="form.vehicle_type" placeholder="请选择" clearable style="width: 370px;">
              <el-option
                v-for="item in dict.vehicle_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('Point.dialog.can_vehicle_type')" prop="can_vehicle_types">
            <el-select v-model="form.can_vehicle_types" multiple placeholder="请选择" clearable style="width: 370px;">
              <el-option
                v-for="item in dict.vehicle_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('Point.dialog.vehicle_qty')" prop="vehicle_qty">
            <el-input-number v-model="form.vehicle_qty" style="width: 370px;" :controls="false" class="clear-number-input" :min="0" :precision="0" :max="99999" label="载具数量" size="mini" />
          </el-form-item>
          <el-form-item :label="$t('Point.dialog.is_has_workder')">
            <el-radio-group v-model="form.is_has_workder" size="mini">
              <el-radio label="true">{{ $t('common.Yes') }}</el-radio>
              <el-radio label="false">{{ $t('common.No') }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item :label="$t('Point.dialog.is_auto')">
            <el-radio-group v-model="form.is_auto" size="mini">
              <el-radio label="true">{{ $t('common.Yes') }}</el-radio>
              <el-radio label="false">{{ $t('common.No') }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item :label="$t('Point.dialog.remark')">
            <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">{{ $t('common.Cancel') }}</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">{{ $t('common.Confirm') }}</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="workshop_code"
          :label="$t('Point.table.workshop_code')"
          :min-width="flexWidth('workshop_code',crud.data,$t('Point.table.workshop_code'))"
        />
        <el-table-column
          prop="point_code"
          :label="$t('Point.table.point_code')"
          :min-width="flexWidth('point_code',crud.data,$t('Point.table.point_code'))"
        />
        <el-table-column
          prop="point_name"
          :label="$t('Point.table.point_name')"
          :min-width="flexWidth('point_name',crud.data,$t('Point.table.point_name'))"
        />
        <el-table-column
          prop="region_code"
          :label="$t('Point.table.region_code')"
          :min-width="flexWidth('region_code',crud.data,$t('Point.table.region_code'))"
        />
        <el-table-column
          prop="region_name"
          :label="$t('Point.table.region_name')"
          :min-width="flexWidth('region_name',crud.data, $t('Point.table.region_name'))"
        />
        <el-table-column
          prop="point_type_name"
          :label="$t('Point.table.point_type')"
          :min-width="flexWidth('point_type_name',crud.data,$t('Point.table.point_type'))"
        />
        <el-table-column
          prop="point_status_name"
          :label="$t('Point.table.point_status')"
          :min-width="flexWidth('point_status_name',crud.data,$t('Point.table.point_status'))"
        />
        <el-table-column
          prop="vehicle_type"
          :label="$t('Point.table.vehicle_type')"
          :min-width="flexWidth('vehicle_type',crud.data,$t('Point.table.vehicle_type'), 30)"
        >
          <template slot-scope="scope">
            {{ dict.label.vehicle_type[scope.row.vehicle_type] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="vehicle_code"
          :label="$t('Point.table.vehicle_code')"
          :min-width="flexWidth('vehicle_code',crud.data,$t('Point.table.vehicle_code'))"
        />
        <el-table-column
          prop="vehicle_qty"
          :label="$t('Point.table.vehicle_qty')"
          :min-width="flexWidth('vehicle_qty',crud.data,$t('Point.table.vehicle_qty'))"
        />
        <el-table-column
          :label="$t('Point.table.lock_type')"
          :min-width="flexWidth('lock_type',crud.data,$t('Point.table.lock_type'))"
        >
          <template slot-scope="scope">
            {{ dict.label.d_lock_type[scope.row.lock_type] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="parent_point_code"
          :label="$t('Point.table.parent_point_code')"
          :min-width="flexWidth('parent_point_code',crud.data,$t('Point.table.parent_point_code'))"
        />
        <el-table-column
          prop="ing_task_code"
          :label="$t('Point.table.ing_task_code')"
          :min-width="flexWidth('ing_task_code',crud.data,$t('Point.table.ing_task_code'))"
        />
        <el-table-column
          prop="is_has_workder"
          :label="$t('Point.table.is_has_workder')"
          :min-width="flexWidth('is_has_workder',crud.data,$t('Point.table.is_has_workder'))"
        >
          <template slot-scope="scope">
            {{ scope.row.is_has_workder ? $t('common.Yes') : $t('common.No') }}
          </template>
        </el-table-column>
        <el-table-column
          prop="is_auto"
          :label="$t('Point.table.is_auto')"
          :min-width="flexWidth('is_auto',crud.data,$t('Point.table.is_auto'))"
        >
          <template slot-scope="scope">
            {{ scope.row.is_auto ? $t('common.Yes') : $t('common.No') }}
          </template>
        </el-table-column>
        <el-table-column
          prop="remark"
          :label="$t('Point.table.remark')"
          :min-width="flexWidth('remark',crud.data,$t('Point.table.remark'))"
        />
        <el-table-column
          prop="is_used"
          :label="$t('Point.table.is_used')"
          :min-width="flexWidth('is_used',crud.data,$t('Point.table.is_used'))"
        >
          <template slot-scope="scope">
            {{ scope.row.is_used ? $t('common.Yes') : $t('common.No') }}
          </template>
        </el-table-column>
        <el-table-column
          prop="create_name"
          :label="$t('Point.table.create_name')"
          :min-width="flexWidth('create_name',crud.data,$t('Point.table.create_name'))"
        />
        <el-table-column
          prop="create_time"
          :label="$t('Point.table.create_time')"
          :min-width="flexWidth('create_time',crud.data,$t('Point.table.create_time'))"
        />
        <el-table-column
          prop="update_name"
          :label="$t('Point.table.update_name')"
          :min-width="flexWidth('update_name',crud.data,$t('Point.table.update_name'))"
        />
        <el-table-column
          prop="update_time"
          :label="$t('Point.table.update_time')"
          :min-width="flexWidth('update_time',crud.data,$t('Point.table.update_time'))"
        />
        <el-table-column v-permission="[]" :label="$t('common.Operate')" width="180px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              style="display: inline"
              :data="scope.row"
              :permission="permission"
            />
            <el-button
              v-if="false"
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="toView(scope.row)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <ViewDialog ref="viewDialog" />
    <PointDialog :dialog-show.sync="pointDialog" :is-single="false" @tableChanged="tableChanged" />
  </div>
</template>

<script>
import crudSchBasePoint from './schBasePoint'
import crudSchBaseRegion from '../region/schBaseRegion'
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 crudMdBaseWorkShop from '@/views/wms/basedata/workshop/mdBaseWorkshop'
import ViewDialog from '@/views/wms/sch/point/ViewDialog.vue'
import PointDialog from '@/views/wms/sch/point/PointDialog.vue'
import i18n from '@/i18n'

const defaultForm = {
  point_code: null,
  point_name: null,
  region_code: null,
  region_name: null,
  point_type: null,
  point_status: null,
  can_material_type: null,
  can_vehicle_type: null,
  vehicle_max_qty: null,
  vehicle_type: null,
  vehicle_code: null,
  vehicle_qty: 0,
  block_num: null,
  row_num: null,
  col_num: null,
  layer_num: null,
  in_order_seq: null,
  out_order_seq: null,
  in_empty_seq: null,
  out_empty_seq: null,
  parent_point_code: null,
  ext_point_code: null,
  ing_task_code: null,
  is_has_workder: 'true',
  workshop_code: null,
  is_auto: 'true',
  remark: null,
  is_used: 'true',
  can_vehicle_types: null
}
export default {
  name: 'SchBasePoint',
  dicts: ['vehicle_type', 'TrueOrFalse', 'd_lock_type'],
  components: { PointDialog, ViewDialog, pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: i18n.t('Point.title'),
      url: 'api/schBasePoint',
      idField: 'point_code',
      sort: 'point_code,desc',
      crudMethod: { ...crudSchBasePoint },
      optShow: {
        add: true,
        edit: true,
        del: true,
        download: false,
        reset: true
      },
      query: {
        is_used: 'true'
      }
    })
  },
  data() {
    return {
      permission: {},
      rules: {
        point_code: [
          { required: true, message: i18n.t('Point.rules.r1'), trigger: 'blur' }
        ],
        point_name: [
          { required: true, message: i18n.t('Point.rules.r2'), trigger: 'blur' }
        ],
        point_type: [
          { required: true, message: i18n.t('Point.rules.r3'), trigger: 'blur' }
        ],
        region_code: [
          { required: true, message: i18n.t('Point.rules.r4'), trigger: 'blur' }
        ]
      },
      workShopList: [],
      regionList: [],
      pointTypesList: [],
      pointStatusList: [],
      pointStatusDialogList: [],
      pointTypesDialogList: [],
      pointDialog: false
    }
  },
  computed: {
    computedSearchLabelWidth() {
      const item = localStorage.getItem('lang')
      if (item === 'zh') {
        return `90px`
      }
      return `150px`
    },
    computedLabelWidth() {
      const item = localStorage.getItem('lang')
      if (item === 'zh') {
        return `540px`
      }
      return `600px`
    },
    computedFormLabelWidth() {
      const item = localStorage.getItem('lang')
      if (item === 'zh') {
        return `110px`
      }
      if (item === 'in') {
        return `160px`
      }
      return `150px`
    }
  },
  created() {
    this.getWorkShopList()
    this.getRegionList()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToCU]() {
      this.form.is_has_workder = this.form.is_has_workder.toString()
      this.form.is_auto = this.form.is_auto.toString()
    },
    [CRUD.HOOK.afterToCU]() {
      if (this.form.region_code) {
        this.getPointStatusAndTypeList(this.form.region_code, 2)
      }
    },
    hand(value) {
      this.crud.toQuery()
    },
    getWorkShopList() { // 获取车间列表
      crudMdBaseWorkShop.getWorkShopList().then(res => {
        this.workShopList = res
      })
    },
    getRegionList() {
      crudSchBaseRegion.getRegionList().then(res => {
        this.regionList = res
      })
    },
    getPointStatusAndTypeList(id, flag) {
      if (id) {
        this.getPointStatusList(id, flag)
        this.getPointTypeList(id, flag)
      }
      if (flag === 1) {
        this.crud.toQuery()
      }
    },
    getPointStatusList(id, flag) {
      crudSchBaseRegion.getPointStatusSelectById(id).then(res => {
        if (flag === 1) {
          this.pointStatusList = res
        } else {
          this.pointStatusDialogList = res
        }
      })
    },
    getPointTypeList(id, flag) {
      crudSchBaseRegion.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
      crudSchBasePoint.changeUsed(param).then(res => {
        this.crud.notify(i18n.t('common.Operation_success'), CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    },
    showButton(point_status) {
      if (point_status && (point_status === '2' || point_status === '3')) {
        return true
      }
      return false
    },
    toView(row) {
      if (row) {
        this.$refs.viewDialog.setParentData(row)
      }
    },
    tableChanged(row) {
      this.form.parent_point_code = row.map(point => point.point_code).join(',')
    },
    getParentPoint() {
      this.pointDialog = true
    },
    handleClear() {
      this.crud.query.region_code = null
      this.crud.query.point_type = null
      this.crud.query.point_status = null
      this.hand()
    },
    changeLock(data) {
      const param = {}
      param.data = data
      param.lock_type = this.crud.query.lock_type
      crudSchBasePoint.changeLock(param).then(res => {
        this.crud.notify(i18n.t('common.Operation_success'), CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    }
  }
}
</script>

<style scoped>

</style>
