<template>
  <el-dialog :visible.sync="ishow" append-to-body title="盘点计划" width="88%" >
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <!--表单组件-->
      <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="库区"  prop="area">
            <el-select v-model="form.area" @focus="getArea" value-key="id" filterable placeholder="请选择库区" style="width: 200px;">
              <el-option
                v-for="item in areas"
                :key="item.id"
                :label="item.code"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>

      <el-form :inline="true" ref="form2" :model="form2" :rules="rules" size="small" label-width="60px" style="width: 80%">
        <el-form-item label="单号">
          <el-input v-model="form2.code" style="width: 120px;" disabled />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form2.type" filterable placeholder="请选择" style="width: 120px;" disabled >
            <el-option
              v-for="item in dict.count_type"
              :key="item.id"
              :label="item.label"
              :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form2.remark" style="width: 120px;" />
        </el-form-item>
      </el-form>
      <crudOperation :permission="permission" :tableKey="this.$options.name">
      <el-button
        slot="right"
        v-permission="permission.checkCountPlan"
        class="filter-item"
        size="mini"
        type="primary"
        icon="el-icon-bottom-right"
        @click="checkCountPlan()"
      > 审核
      </el-button>
      </crudOperation>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column :show-overflow-tooltip="true" prop="areaCode" label="库区编码">
          <template slot-scope="scope">
            <div>{{ scope.row.area.code }}</div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="areaName" label="库区名称">
          <template slot-scope="scope">
            <div>{{ scope.row.area.name }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="planQty" label="计划数量" />
        <el-table-column prop="countQty" label="盘点数量" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="createBy" label="创建人" />
        <el-table-column prop="createTime" label="创建时间" width="140"/>
        <el-table-column v-if="checkPer(['admin','countDetail:edit','countDetail:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </el-dialog>
</template>

<script>
import crudCountDetail, { checkCountPlan, getCountDetail } from '@/api/countDetail'
import CRUD, { presenter, header, form, crud } 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 {getAres} from "@/api/area"
import DonMessage from "@/utils/message";
import crudAsnDetail from '@/api/asnDetail'
const defaultForm = { id: null, itemId: null, areaId: null, warehouseId: null, invId: null, planQty: 0, countQty: 0, remark: null, deptId: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'CountDetail',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['count_type', 'count_status'],
  cruds() {
    return CRUD({ title: '明细', url: 'api/countDetail', idField: 'id', sort: 'id,desc', queryOnPresenterCreated:false,crudMethod: { ...crudCountDetail },
      optShow: {
        add: true,
        edit: false,
        del: false,
        reset: false,
        download: false
      },
      queryOnPresenterCreated: true})
  },
  data() {
    return {
      form2: {id:null,remark: null,code: null,type:null},
      ishow:false,
      permission: {
        add: ['admin', 'countDetail:add'],
        edit: ['admin', 'countDetail:edit'],
        del: ['admin', 'countDetail:del']
      },
      rules: {
      }    }
  },
  mounted() {
    this.getArea()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getArea() {
      getAres({ }).then(res => {
        this.areas= res.content.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    doInit(parent) {
      this.crud.data = null;
      getCountDetail(parent.id).then(res => {
        this.crud.data = res;
        this.form2.id=parent.id
        this.form2.remark=parent.remark
        this.form2.code=parent.code
        this.form2.type=parent.type
        this.form.plan=parent

      })
    },
    checkCountPlan2() {
      DonMessage.success('收货失败！'+ this.form2.id);
    },
    checkCountPlan() {
        const ids = []
        ids.push(this.form2.id)
        crudCountDetail.checkCountPlan(ids).then(res => {
          DonMessage.success('审核成功！');
          this.crud.toQuery()
        }).catch(() => {
        DonMessage.success('审核失败！');
      })
    },
  }
}
</script>

<style scoped>

</style>
