<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-select
              v-model="query.workshop_code"
              clearable
              size="mini"
              placeholder="所属车间"
              class="filter-item"
              @change="hand"
            >
              <el-option
                v-for="item in workShopList"
                :label="item.workshop_name"
                :value="item.workshop_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="模糊搜索">
            <el-input
              v-model="query.blurry"
              clearable
              size="mini"
              placeholder="编码名称"
              @keyup.enter.native="crud.toQuery"
            />
          </el-form-item>
          <el-form-item label="是否建工单">
            <el-switch
              v-model="query.is_has_workder"
              active-color="#409EFF"
              inactive-color="#C0CCDA"
              @change="hand"
            />
          </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="520px">
        <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="100px">
          <el-form-item label="所属车间">
            <el-select
              v-model="form.workshop_code"
              placeholder="请选择"
              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="区域编码">
            <el-input v-model="form.region_code" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="区域名称">
            <el-input v-model="form.region_name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="点位类型说明">
            <el-input v-model="form.point_type_explain" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="点位状态说明">
            <el-input v-model="form.point_status_explain" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="是否创建工单">
            <el-select
              v-model="form.is_has_workder"
              size="mini"
              placeholder="是否创建工单"
              class="filter-item"
              style="width: 370px;"
            >
              <el-option
                v-for="item in dict.TrueOrFalse"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;" />
          </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="region_code" label="区域编码" :min-width="flexWidth('region_code',crud.data,'区域编码')" />
        <el-table-column prop="region_name" label="区域名称" :min-width="flexWidth('region_name',crud.data,'区域名称')" />
        <el-table-column prop="point_type_explain" label="点位类型说明" :min-width="flexWidth('point_type_explain',crud.data,'点位类型说明')" />
        <el-table-column prop="point_status_explain" label="点位状态说明" :min-width="flexWidth('point_status_explain',crud.data,'点位状态说明')" />
        <el-table-column prop="is_has_workder" label="是否创建工单" :min-width="flexWidth('is_has_workder',crud.data,'是否创建工单')">
          <template slot-scope="scope">
            {{scope.row.is_has_workder?'是':'否'}}
          </template>
        </el-table-column>
        <el-table-column prop="workshop_code" label="车间编码" :min-width="flexWidth('workshop_code',crud.data,'车间编码')" />
        <el-table-column prop="remark" label="备注" :min-width="flexWidth('remark',crud.data,'备注')" />
        <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_name" label="修改人" :min-width="flexWidth('update_name',crud.data,'修改人')" />
        <el-table-column prop="update_time" label="修改时间" :min-width="flexWidth('update_time',crud.data,'修改时间')" />
        <el-table-column v-permission="[]" label="操作" width="120px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSchBaseRegion from './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'

const defaultForm = {
  region_code: null,
  region_name: null,
  point_type_explain: null,
  point_status_explain: null,
  is_has_workder: 'true',
  workshop_code: null,
  remark: null
}
export default {
  name: 'Region',
  dicts: ['TrueOrFalse'],
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '区域管理',
      url: 'api/schBaseRegion',
      idField: 'region_code',
      sort: 'region_code,desc',
      crudMethod: { ...crudSchBaseRegion },
      query: {
        is_has_workder: true
      }
    })
  },
  created() {
    this.getWorkShopList()
  },
  data() {
    return {
      permission: {
      },
      rules: {
      },
      workShopList: []
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToCU]() {
      this.form.is_has_workder = this.form.is_has_workder.toString()
    },
    hand(value) {
      this.crud.toQuery()
    },
    getWorkShopList() { // 获取车间列表
      crudMdBaseWorkShop.getWorkShopList().then(res => {
        this.workShopList = res
      })
    }
  }
}
</script>

<style scoped>

</style>
