<template>
  <el-dialog
    :title="crud.status.title"
    append-to-body
    :before-close="crud.cancelCU"
    :visible.sync="crud.status.cu > 0 || crud.status.view > 0"
    fullscreen
    width="1200px"
    @open="open"
    @close="close"
  >
    <el-row v-show="crud.status.cu > 0" :gutter="20">
      <el-col :span="20" style="border: 1px solid white">
        <span />
      </el-col>
      <el-col :span="4">
        <span>
          <el-button icon="el-icon-check" size="mini" :loading="crud.cu === 2" type="primary" @click="crud.submitCU">保存</el-button>
          <el-button icon="el-icon-close" size="mini" type="info" @click="crud.cancelCU">关闭</el-button>
        </span>
      </el-col>
    </el-row>

    <el-form ref="form" style="border: 1px solid #cfe0df;margin-top: 10px;padding-top: 10px;" :inline="true" :model="form" :rules="rules" size="mini" label-width="85px" label-suffix=":">
      <el-form-item label="单据编号" prop="bill_id">
        <label slot="label">单&nbsp;&nbsp;据&nbsp;号:</label>
        <el-input v-model.trim="form.bill_id" :disabled="crud.status.edit > 0 || crud.status.view > 0" placeholder="单据编号" clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="订单号" prop="order_no">
        <el-input v-model.trim="form.order_no" :disabled="crud.status.view > 0" placeholder="订单号" clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="订单类型" prop="order_type">
        <el-input v-model.trim="form.order_type" :disabled="crud.status.view > 0" placeholder="订单类型" clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="供应商编码" prop="supplier_code">
        <el-input v-model.trim="form.supplier_code" :disabled="crud.status.view > 0" placeholder="供应商编码" clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplier_name">
        <el-input v-model.trim="form.supplier_name" :disabled="crud.status.view > 0" placeholder="供应商名称" clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="单据状态" prop="bill_status">
        <el-select v-model="form.bill_status" clearable style="width: 210px" placeholder="单据状态" class="filter-item" disabled>
          <el-option v-for="item in billStatusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="明细数" prop="dtl_count">
        <label slot="label">明&nbsp;&nbsp;细&nbsp;数:</label>
        <el-input v-model.trim="form.dtl_count" size="mini" disabled style="width: 210px" />
      </el-form-item>
    </el-form>

    <div class="crud-opts2" style="margin-bottom: 5px;">
      <span class="crud-opts-right2">
        <slot name="left" />
        <el-button
          v-if="crud.status.cu > 0"
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="insertEvent"
        >
          添加明细行
        </el-button>
      </span>
    </div>

    <el-table
      ref="table"
      :data="form.tableData"
      style="width: 100%;"
      border
      :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
    >
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column show-overflow-tooltip width="120" prop="item_no" label="行号">
        <template scope="scope">
          <el-input v-if="crud.status.cu > 0" v-model="scope.row.item_no" size="mini" />
          <span v-else>{{ scope.row.item_no }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip width="120" prop="category_code" label="分类编码">
        <template scope="scope">
          <el-input v-if="crud.status.cu > 0" v-model="scope.row.category_code" size="mini" />
          <span v-else>{{ scope.row.category_code }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip width="120" prop="category_name" label="分类名称">
        <template scope="scope">
          <el-input v-if="crud.status.cu > 0" v-model="scope.row.category_name" size="mini" />
          <span v-else>{{ scope.row.category_name }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip width="120" prop="sku_code" label="物料编码">
        <template scope="scope">
          <el-input v-if="crud.status.cu > 0" v-model="scope.row.sku_code" size="mini" />
          <span v-else>{{ scope.row.sku_code }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip width="140" prop="sku_name" label="物料名称">
        <template scope="scope">
          <el-input v-if="crud.status.cu > 0" v-model="scope.row.sku_name" size="mini" />
          <span v-else>{{ scope.row.sku_name }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip width="100" prop="model" label="规格型号">
        <template scope="scope">
          <el-input v-if="crud.status.cu > 0" v-model="scope.row.model" size="mini" />
          <span v-else>{{ scope.row.model }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip width="100" prop="house_code" label="仓库编码">
        <template scope="scope">
          <el-input v-if="crud.status.cu > 0" v-model="scope.row.house_code" size="mini" />
          <span v-else>{{ scope.row.house_code }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip width="120" prop="batch_no" label="批次号">
        <template scope="scope">
          <el-input v-if="crud.status.cu > 0" v-model="scope.row.batch_no" size="mini" />
          <span v-else>{{ scope.row.batch_no }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip width="100" prop="qty" label="数量">
        <template scope="scope">
          <el-input-number v-if="crud.status.cu > 0" v-model="scope.row.qty" :controls="false" :min="0" size="mini" style="width: 80px" />
          <span v-else>{{ scope.row.qty }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip width="80" prop="unit" label="单位">
        <template scope="scope">
          <el-input v-if="crud.status.cu > 0" v-model="scope.row.unit" size="mini" />
          <span v-else>{{ scope.row.unit }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip width="120" prop="track_no" label="跟踪号">
        <template scope="scope">
          <el-input v-if="crud.status.cu > 0" v-model="scope.row.track_no" size="mini" />
          <span v-else>{{ scope.row.track_no }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="crud.status.cu > 0" align="center" label="操作" width="80" fixed="right">
        <template scope="scope">
          <el-button type="danger" class="filter-item" size="mini" icon="el-icon-delete" @click.native.prevent="deleteRow(scope.$index, form.tableData)" />
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script>
import CRUD, { crud, form } from '@crud/crud'
import crudPurchase from '@/views/wms/st/purchase/purchase'

const defaultForm = {
  id: '',
  bill_id: '',
  order_no: '',
  order_type: '',
  supplier_code: '',
  supplier_name: '',
  creator: '',
  create_time: null,
  modify_date: null,
  status: 0,
  red: 0,
  bill_status: '0',
  audit_status: '',
  audit_msg: '',
  dtl_count: '0',
  tableData: []
}

export default {
  name: 'AddDialog',
  mixins: [crud(), form(defaultForm)],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      billStatusOptions: [
        { value: '0', label: '已创建' },
        { value: '1', label: '执行中' },
        { value: '2', label: '已完成' },
        { value: '3', label: '审核成功' },
        { value: '4', label: '审核失败' }
      ],
      rules: {
        bill_id: [
          { required: true, message: '单据编号不能为空', trigger: 'blur' }
        ],
        order_no: [
          { required: true, message: '订单号不能为空', trigger: 'blur' }
        ],
        supplier_code: [
          { required: true, message: '供应商编码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    open() {
    },
    close() {
      this.$emit('AddChanged')
    },
    [CRUD.HOOK.afterToEdit]() {
      crudPurchase.get(this.form.id).then(res => {
        if (res.data && res.data.tableData) {
          this.form.tableData = res.data.tableData
        }
      })
    },
    [CRUD.HOOK.afterToView]() {
      crudPurchase.get(this.form.id).then(res => {
        if (res.data && res.data.tableData) {
          this.form.tableData = res.data.tableData
        }
      })
    },
    [CRUD.HOOK.beforeSubmit]() {
      if (this.form.tableData.length === 0) {
        this.crud.notify('请至少添加一条明细', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
    },
    insertEvent() {
      this.form.tableData.push({
        item_no: String(this.form.tableData.length + 1),
        category_code: '',
        category_name: '',
        sku_code: '',
        sku_name: '',
        model: '',
        house_code: '',
        batch_no: '',
        qty: 0,
        unit_code: '',
        unit: '',
        track_no: ''
      })
      this.form.dtl_count = String(this.form.tableData.length)
    },
    deleteRow(index, rows) {
      rows.splice(index, 1)
      this.form.dtl_count = String(this.form.tableData.length)
    }
  }
}
</script>

<style>
.crud-opts2 {
  padding: 4px 0;
  display: -webkit-flex;
  display: flex;
  align-items: center;
}
.crud-opts2 .crud-opts-right2 {
  margin-left: auto;
}
</style>
