<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :table-key="this.$options.name">
        <el-button
            slot="left"
            v-permission="permission.add"
            class="filter-item"
            type="primary"
            size="mini"
            icon="el-icon-document-add"
            @click="startAsn(crud.selections[0].id)"
        >
          开始收货
        </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="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="订单来源">
            <el-input v-model="form.orderOrigin" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="货主">
            <el-input v-model="form.structure" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="系统单号">
            <el-input v-model="form.relatedBill1" style="width: 370px;" />
          </el-form-item>
          <!--          <el-form-item label="订单状态">
            <el-input v-model="form.status" style="width: 370px;" />
          </el-form-item>-->
          <el-form-item label="订单状态">
            <el-select
              v-model="form.status"
              filterable
              placeholder="订单状态"
              style="width: 370px;"
            >
              <el-option
                v-for="item in dict.asn_status"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="单据类型" prop="billType">
            <el-select v-model="form.billType" value-key="id" filterable placeholder="请选择单据类型"
                       style="width: 370px;">
              <el-option
                  v-for="item in billTypeList"
                  :key="item.id"
                  :label="item.name"
                  :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-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 prop="dept.name" label="仓库" />
        <el-table-column prop="billType.name" label="单据类型" />
        <el-table-column prop="orderOrigin" label="订单来源" />
        <el-table-column prop="structure" label="货主" />
        <el-table-column prop="erTime" label="操作时间" />
        <el-table-column prop="relatedBill1" label="系统单号" />
        <el-table-column prop="status" label="订单状态">
          <template slot-scope="scope">
            {{ dict.label.asn_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="orderDate" label="订单时间" />
        <el-table-column v-if="checkPer(['admin','asn:edit','asn:del'])" label="操作" width="350px" align="center">
          <template slot-scope="scope">
            <!--            <udOperation
                :data="scope.row"
                :permission="permission"
            >
            </udOperation>-->
            <el-button
              slot="left"
              v-permission="permission.add"
              class="filter-item"
              type="primary"
              size="mini"
              icon="el-icon-document-add"
              @click="jumpAsnDetail(scope.row.relatedBill1)"
            >
              查看明细
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudAsn from '@/api/asn'
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 { getItems } from '@/api/item'
import { getBillType } from '@/api/billType'
import crudAsnDetail from "@/api/asnDetail";
import DonMessage from "@/utils/message";

const defaultForm = { id: null, dept: null, billType: null, orderOrigin: null, structure: null, erTime: null, relatedBill1: null, relatedBill2: null, sourceName: null, status: null, address: null, createBy: null, updateBy: null, createTime: null, updateTime: null, orderDate: null }
export default {
  name: 'Asn',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['asn_status'],
  cruds() {
    return CRUD({ title: 'asn', url: 'api/asn', idField: 'id', sort: 'id,desc', crudMethod: { ...crudAsn }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'asn:add'],
        edit: ['admin', 'asn:edit'],
        del: ['admin', 'asn:del']
      },
      billTypeList: [],
      rules: {
      }
    }
  },
  mounted() {
    this.getBillType()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    jumpAsnDetail(relatedBill1) {
      //  路径/home对应我在router目录下index.js中定义的path属性值
      this.$router.push({ path: '/business-data/asnDetail/asnDetail', query: { relatedBill1: relatedBill1 }})
    },
    startAsn(id) {
      crudAsn.startAsn(id).then(res => {
        console.log('res:' + res)
        if (res.status === 200) {
          DonMessage.success(res.message)
        } else {
          DonMessage.error(res.message)
        }
        this.crud.toQuery()
      })
    },
    getBillType() {
      getBillType({}).then(res => {
        this.billTypeList = res.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
