<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="$t('common.point_code')">
            <el-input
              v-model="query.name"
              clearable
              size="mini"
              :placeholder="$t('common.inputCodeOrName')"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('sch_base_region.region_name')">
            <el-select
              v-model="query.region_id"
              clearable
              filterable
              size="mini"
              :placeholder="$t('common.Please_select')"
              class="filter-item"
              @change="getPointStatusAndTypeList(query.region_id, 1)"
            >
              <el-option
                v-for="item in regionList"
                :label="item.region_name"
                :value="item.region_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('common.point_type')">
            <el-select
              v-model="query.point_type"
              clearable
              size="mini"
              :placeholder="$t('common.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('common.point_status')">
            <el-select
              v-model="query.point_status"
              clearable
              filterable
              size="mini"
              :placeholder="$t('common.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('sch_base_point.lock_type')">
            <el-select
              v-model="query.lock_type"
              clearable
              filterable
              size="mini"
              :placeholder="$t('sch_base_point.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('common.is_used')">
            <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)"
        >
          {{ $t('common.is_used') }}
        </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)"
        >
          {{ $t('common.disable') }}
        </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)"
        >
          {{ $t('common.lock') }}
        </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)"
        >
          {{ $t('common.un_lock') }}
        </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="$t('sch_base_point.point_id')" prop="point_id">
            <el-input v-model="form.point_id" style="width: 370px;" />
          </el-form-item>
          <el-form-item :label="$t('sch_base_region.region_name')" prop="region_id">
            <el-select
              v-model="form.region_id"
              :placeholder="$t('common.Please_select')"
              style="width: 370px;"
              @change="getPointStatusAndTypeList(form.region_id, 2)"
            >
              <el-option
                v-for="item in regionList"
                :label="item.region_name"
                :value="item.region_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('common.point_code')" 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="$t('sch_base_point.point_name')" 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="$t('common.point_status')" prop="point_status">
            <el-select
              v-model="form.point_status"
              size="mini"
              :placeholder="$t('common.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 :label="$t('sch_base_point.lock_type')" prop="lock_type">
            <el-select
              v-model="form.lock_type"
              size="mini"
              :placeholder="$t('sch_base_point.lock_type')"
              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="$t('common.point_type')" prop="point_type">
            <el-select
              v-model="form.point_type"
              size="mini"
              :placeholder="$t('common.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('common.vehicle_code')" prop="vehicle_code">
            <el-input v-model="form.vehicle_code" clearable style="width: 370px;" />
          </el-form-item>
          <el-form-item :label="$t('sch_base_point.vehicle_qty')" prop="vehicle_qty">
            <el-input-number v-model="form.vehicle_qty" :precision="0" :controls="false" :min="0" style="width: 370px;" />
          </el-form-item>
          <el-form-item :label="$t('common.material_code')" prop="material_code">
            <el-input v-model="form.material_code" clearable style="width: 370px;" />
          </el-form-item>

          <el-form-item :label="$t('common.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="point_code" :label="$t('common.point_code')" sortable width="100" />
        <el-table-column prop="point_name" :label="$t('sch_base_point.point_name')" width="120" sortable show-overflow-tooltip />
        <el-table-column prop="region_name" :label="$t('sch_base_region.region_name')" min-width="120" show-overflow-tooltip />
        <el-table-column prop="point_type_name" :label="$t('common.point_type')" />
        <el-table-column prop="point_status_name" :label="$t('common.point_status')" />
        <el-table-column prop="lock_type_name" :label="$t('sch_base_point.lock_type')" />
        <!--     <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="$t('common.vehicle_code')"
          :min-width="flexWidth('vehicle_code',crud.data,$t('common.vehicle_code'))"
        />
        <el-table-column
          prop="material_code"
          :label="$t('common.material_code')"
          :min-width="flexWidth('material_code',crud.data,$t('common.material_code'))"
        />
        <el-table-column prop="vehicle_qty" :label="$t('sch_base_point.vehicle_qty')" />
        <el-table-column prop="product_area" :label="$t('common.product_area')" />
        <el-table-column prop="remark" :label="$t('common.remark')" min-width="100" show-overflow-tooltip />
        <el-table-column prop="is_used" :label="$t('common.is_used')">
          <template slot-scope="scope">
            {{ scope.row.is_used == '1' ? $t('common.Yes') : $t('common.No') }}
          </template>
        </el-table-column>
        <el-table-column prop="update_optname" :label="$t('common.update_optname')" />
        <el-table-column prop="update_time" :label="$t('common.update_time')" width="150" />
        <el-table-column
          v-permission="[]"
          :label="$t('common.Operate')"
          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 '@/views/wms/sch/region/region'
import crudPoint, { changeActive, changeActives } from '@/views/wms/sch/point/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 { download } from '@/api/data'
import { downloadFile } from '@/utils'

const defaultForm = {
  point_id: null,
  point_code: null,
  material_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 {
      sync_flag: false,
      syncLoading: false,
      invtypelist: [],
      pointStatusList: [],
      pointStatusDialogList: [],
      pointTypesList: [],
      pointTypesDialogList: [],
      options: [],
      regionList: [],
      permission: {},
      rules: {
        point_code: [
          { required: true, message: this.$t('common.pointCodeRequired'), trigger: 'blur' }
        ],
        point_name: [
          { required: true, message: this.$t('common.pointNameRequired'), trigger: 'blur' }
        ],
        point_type: [
          { required: true, message: this.$t('common.pointTypeRequired'), trigger: 'blur' }
        ],
        point_status: [
          { required: true, message: this.$t('common.pointStatusRequired'), trigger: 'blur' }
        ],
        region_id: [
          { required: true, message: this.$t('common.regionRequired'), trigger: 'blur' }
        ],
        lock_type: [
          { required: true, message: this.$t('common.lockTypeRequired'), trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    crudPoint.getRegion().then(res => {
      this.regionList = res
    })
    this.$set(this.query, 'region_id', '1582991348217286656')
    this.crud.toQuery()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterToCU]() {
      if (this.form.region_id) {
        this.getPointStatusAndTypeList(this.form.region_id, 2)
      }
    },
    downdtl() {
      if (this.currentRow !== null) {
        crud.downloadLoading = true
        download('/api/point/download', this.crud.query).then(result => {
          // debugger
          downloadFile(result, '库存', 'xlsx')
          crud.downloadLoading = false
        }).catch(() => {
          crud.downloadLoading = false
        })
      }
    },
    hand(value) {
      this.crud.toQuery()
    },
    format_is_used(is_used) {
      return is_used === '1'
    },
    changeEnabled(data, val) {
      let msg = this.$t('common.confirmDisable')
      if (val !== '1') {
        msg = this.$t('common.confirmEnable')
      }
      this.$confirm(msg, this.$t('common.tip'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning'
      }).then(() => {
        changeActive(data).then(res => {
          this.crud.toQuery()
          this.crud.notify(this.$t('common.operationSuccess'), CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch(() => {
          data.is_used = !data.is_used
        })
      }).catch(() => {
      })
    },
    getPointStatusAndTypeList(id, flag) {
      // debugger
      if (id) {
        this.getPointStatusList(id, flag)
        this.getPointTypeList(id, flag)
      }
      if (flag === 1) {
        this.crud.toQuery()
      }
    },
    getPointStatusList(id, flag) {
      // debugger
      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) { // 更改启用状态
      crudPoint.changeActives(data).then(res => {
        this.crud.notify(this.$t('common.operationSuccess'), 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(this.$t('common.operationSuccess'), CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      })
    },
    sync() {
      this.sync_flag = true
      crudPoint.sync().then(res => {
        this.crud.notify(this.$t('common.syncSuccess'), CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.sync_flag = false
      }).catch(() => {
        this.sync_flag = false
      })
    }

  }
}
</script>

<style scoped>

</style>
