<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-cascader
              placeholder="所属库区"
              :options="sects"
              :props="{ checkStrictly: true }"
              clearable
              class="filter-item"
              @change="sectQueryChange"
            />
          </el-form-item>
          <el-form-item label="锁定类型">
            <el-select
              v-model="query.lock_type"
              clearable
              size="mini"
              placeholder="锁定类型"
              class="filter-item"
              @change="crud.toQuery"
            >
              <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-input
              v-model="query.search"
              clearable
              size="mini"
              placeholder="仓位编码、名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <rrOperation />
        </el-form>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />

      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="700px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-suffix=":" label-width="90px">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="仓位编码" prop="struct_code">
                <el-input v-model="form.struct_code" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="仓位名称" prop="struct_name">
                <el-input v-model="form.struct_name" style="width: 200px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="仓位简称">
                <el-input v-model="form.simple_name" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="归属库区" prop="sect_id">
                <el-cascader
                  v-model="form.cascader"
                  style="width: 200px;"
                  :options="sects"
                  clearable
                  @change="sectChange"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="载具号">
                <label slot="label">载&nbsp;&nbsp;具&nbsp;号:</label>
                <el-input v-model="form.storagevehicle_code" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="锁定类型" prop="lock_type">
                <el-select
                  v-model="form.lock_type"
                  style="width: 200px;"
                  placeholder=""
                >
                  <el-option
                    v-for="item in dict.d_lock_type"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>

              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="容量" prop="capacity">
                <label slot="label">容&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;量:</label>
                <el-input v-model="form.capacity" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="承受重量" prop="weight">
                <el-input v-model="form.weight" style="width: 200px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="宽度" prop="width">
                <label slot="label">宽&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;度:</label>
                <el-input v-model="form.width" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="深度" prop="zdepth">
                <label slot="label">深&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;度:</label>
                <el-input v-model="form.zdepth" style="width: 200px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="高度" prop="height">
                <label slot="label">高&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;度:</label>
                <el-input v-model="form.height" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="放置类型" prop="placement_type">
                <el-select
                  v-model="form.placement_type"
                  placeholder=""
                  style="width: 200px"
                >
                  <el-option
                    v-for="item in dict.placement_type"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="是否临时" prop="is_tempstruct">
                <el-radio v-model="form.is_tempstruct" label="1">是</el-radio>
                <el-radio v-model="form.is_tempstruct" label="0">否</el-radio>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="备注">
                <label slot="label">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注:</label>
                <el-input v-model="form.remark" style="width: 550px;" rows="2" type="textarea" />
              </el-form-item>
            </el-col>
          </el-row>
        </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="struct_code" label="仓位编码" :min-width="flexWidth('struct_code',crud.data,'仓位编码')" />
        <el-table-column prop="struct_name" label="仓位名称" :min-width="flexWidth('struct_name',crud.data,'仓位名称')" />
        <el-table-column prop="simple_name" label="仓位简称" :min-width="flexWidth('simple_name',crud.data,'仓位简称')" />
        <el-table-column prop="sect_name" label="所属库区" width="150" />
        <el-table-column prop="stor_name" label="所属仓库" width="150" />
        <el-table-column prop="storagevehicle_code" label="载具号" :min-width="flexWidth('storagevehicle_code',crud.data,'载具号')" />
        <el-table-column prop="lock_type" label="锁定类型" min-width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ dict.label.d_lock_type[scope.row.lock_type] }}
          </template>
        </el-table-column>
        <el-table-column prop="sect_name" label="库区名称" />
        <el-table-column prop="placement_type" label="放置类型" min-width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ dict.label.placement_type[scope.row.placement_type] }}
          </template>
        </el-table-column>
        <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="150" :formatter="taskdtl_typeFormat" />
        <el-table-column prop="task_code" label="锁定任务编码" width="150" />
        <el-table-column prop="inv_code" label="锁定单据编码" width="150" />
        <el-table-column prop="inv_type" label="锁定单据类型" width="150" :formatter="invtypeFormat" />
        <el-table-column prop="remark" label="备注" show-overflow-tooltip />
        <el-table-column
          v-permission="['admin','structattr:edit','structattr:del']"
          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 crudStructattr from '@/views/wms/basedata/st/struct/structattr'
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 crudSectattr from '@/views/wms/basedata/st/sect/sectattr'
import crudUserStor, { getSect } from '@/views/wms/basedata/st/userStor/userStor'
/* import checkoutbill from "@/api/wms/st/core/outbill/checkoutbill";*/

const defaultForm = {
  struct_id: null,
  cascader: null,
  struct_code: null,
  struct_name: null,
  simple_name: null,
  sect_id: null,
  sect_code: null,
  sect_name: null,
  stor_id: null,
  stor_code: null,
  stor_name: null,
  stor_type: null,
  capacity: null,
  width: null,
  height: null,
  zdepth: null,
  weight: null,
  xqty: null,
  yqty: null,
  zqty: null,
  is_tempstruct: '0',
  create_id: null,
  create_name: null,
  create_time: null,
  update_optid: null,
  update_optname: null,
  update_time: null,
  is_delete: null,
  back_ground_color: null,
  front_ground_color: null,
  back_ground_pic: null,
  font_direction_scode: null,
  is_used: null,
  is_zdepth: null,
  storagevehicle_id: null,
  storagevehicle_code: null,
  storagevehicle_type: null,
  is_emptyvehicle: null,
  storagevehicle_qty: null,
  lock_type: null,
  material_height_type: null,
  ext_id: null,
  remark: null
}
export default {
  name: 'Structattr',
  dicts: ['ST_HEIGHT_TYPE', 'is_used', 'd_lock_type', 'SCH_TASK_TYPE_DTL','placement_type'],
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '仓位',
      optShow: { add: true, reset: true },
      url: 'api/structattr',
      idField: 'struct_id',
      crudMethod: { ...crudStructattr }
    })
  },
  data() {
    const numberOne = (rule, value, callback) => {
      const numReg = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/
      const numRe = new RegExp(numReg)
      if (value) {
        if (!numRe.test(value)) {
          callback(new Error('只能输入数字'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
      sects: [],
      invtypelist: [],
      permission: {},
      rules: {
        struct_id: [
          { required: true, message: '仓位标识不能为空', trigger: 'blur' }
        ],
        struct_code: [
          { required: true, message: '仓位编码不能为空', trigger: 'blur' }
        ],
        struct_name: [
          { required: true, message: '仓位名称不能为空', trigger: 'blur' }
        ],
        sect_id: [
          { required: true, message: '库区标识不能为空', trigger: 'blur' }
        ],
        stor_id: [
          { required: true, message: '仓库标识不能为空', trigger: 'blur' }
        ],
        capacity: [
          { required: false, message: '不能为空', trigger: 'blur' },
          { validator: numberOne }
        ],
        weight: [
          { required: false, message: '不能为空', trigger: 'blur' },
          { validator: numberOne }
        ],
        width: [
          { required: false, message: '不能为空', trigger: 'blur' },
          { validator: numberOne }
        ],
        height: [
          { required: false, message: '不能为空', trigger: 'blur' },
          { validator: numberOne }
        ],
        zdepth: [
          { required: false, message: '不能为空', trigger: 'blur' },
          { validator: numberOne }
        ],
        material_height_type: [
          { required: true, message: '物料高度类型不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    crudUserStor.getSect({ 'stor_id': '' }).then(res => {
      this.sects = res.content
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterToEdit](crud, form) {
      const val = []
      val[0] = form.stor_id
      val[1] = form.sect_id
      form.cascader = val
    },
    sectChange(val) {
      this.form.sect_id = val[1]
    },
    invtypeFormat(row) {
      for (const item of this.invtypelist) {
        if (item.code === row.inv_type) {
          return item.name
        }
      }
    },
    taskdtl_typeFormat(row) {
      return this.dict.label.SCH_TASK_TYPE_DTL[row.taskdtl_type]
    },
    sectQueryChange(val) {
      if (val.length === 1) {
        this.query.stor_id = val[0]
        this.query.sect_id = ''
      }
      if (val.length === 0) {
        this.query.sect_id = ''
        this.query.stor_id = ''
      }
      if (val.length === 2) {
        this.query.stor_id = val[0]
        this.query.sect_id = val[1]
      }
      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(() => {
        crudStructattr.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>
