<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-select
          v-model="query.stor_id"
          clearable
          class="filter-item"
          placeholder="所属仓库"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in stors"
            :key="item.stor_id"
            :label="item.stor_name"
            :value="item.stor_id"
          />
        </el-select>
        <el-input
          v-model="query.search"
          clearable
          class="filter-item"
          style="width: 200px"
          size="mini"
          placeholder="输入库区编码或名称"
          prefix-icon="el-icon-search"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </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-width="80px">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="库区编码" prop="sect_code">
                <el-input v-model="form.sect_code" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="库区名称" prop="sect_name">
                <el-input v-model="form.sect_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="stor_id">
                <el-select
                  v-model="form.stor_id"
                  placeholder=""
                  style="width: 200px"
                >
                  <el-option
                    v-for="item in stors"
                    :key="item.stor_id"
                    :label="item.stor_name"
                    :value="item.stor_id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="库区容量" prop="capacity">
                <el-input v-model="form.capacity" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="库区类型" prop="sect_type_attr">
                <el-select
                  v-model="form.sect_type_attr"
                  placeholder=""
                  style="width: 200px"
                >
                  <el-option
                    v-for="item in dict.st_sect_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="负责人">
                <el-input v-model="form.sect_manager_name" style="width: 200px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="mobile_no">
                <el-input v-model="form.mobile_no" style="width: 200px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="备注">
                <label slot="label">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注</label>
                <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 550px;" />
              </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 prop="sect_code" sortable label="库区编码" />
        <el-table-column prop="sect_name" label="库区名称" width="150" />
        <el-table-column prop="simple_name" label="库区简称" width="150" />
        <el-table-column prop="stor_name" label="仓库名称" width="150" />
        <el-table-column prop="capacity" label="容量" />
        <el-table-column prop="sect_manager_name" label="负责人" />
        <el-table-column prop="mobile_no" label="联系电话" width="100px" />
        <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="ext_id" label="外部标识" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column
          v-permission="['admin','sectattr:edit','sectattr:del']"
          label="操作"
          width="120px"
          fixed="right"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSectattr from '@/views/wms/basedata/st/sect/sectattr'
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 crudStorattr from '@/views/wms/basedata/st/stor/storattr'
import { isvalidPhone } from '@/utils/validate'

const defaultForm = {
  sect_id: null,
  sect_code: null,
  sect_name: null,
  simple_name: null,
  sect_type_attr: null,
  stor_id: null,
  stor_type: null,
  capacity: null,
  width: null,
  height: null,
  zdepth: null,
  xqty: null,
  yqty: null,
  zqty: null,
  sect_manager_name: null,
  mobile_no: null,
  remark: null,
  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,
  floor_no: null,
  is_used: '',
  ext_id: null
}
export default {
  name: 'Sectattr',
  dicts: ['st_sect_type', 'is_used'],
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '库区',
      optShow: { add: true, reset: true },
      url: 'api/sectattr',
      idField: 'sect_id',
      sort: 'sect_id,desc',
      crudMethod: { ...crudSectattr }
    })
  },
  data() {
    const validPhone = (rule, value, callback) => {
      if (value && !isvalidPhone(value)) {
        callback(new Error('请输入正确的11位手机号码'))
      } else {
        callback()
      }
    }
    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 {
      stors: [],
      permission: {
        add: ['admin', 'user:add'],
        edit: ['admin', 'user:edit'],
        del: ['admin', 'user:del']
      },
      rules: {
        sect_code: [
          { required: true, message: '库区编码不能为空', trigger: 'blur' }
        ],
        sect_name: [
          { required: true, message: '库区名称不能为空', trigger: 'blur' }
        ],
        sect_type_attr: [
          { required: true, message: '库区类型不能为空', trigger: 'blur' }
        ],
        stor_id: [
          { required: true, message: '仓库标识不能为空', trigger: 'blur' }
        ],
        capacity: [
          { required: false, message: '不能为空', trigger: 'blur' },
          { validator: numberOne }
        ],
        mobile_no: [
          { required: false, trigger: 'blur', validator: validPhone }
        ]
      }
    }
  },
  created() {
    crudStorattr.getStor().then(res => {
      this.stors = res.content
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 改变状态
    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(() => {
        crudSectattr.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>
