<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">菜品名称</label>
        <el-input v-model="query.foodName" clearable placeholder="菜品名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">结算卡号</label>
        <el-input v-model="query.cardCode" clearable placeholder="结算卡号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">下单时间</label>
        <date-range-picker
          v-model="query.orderTime"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          class="date-item"
        />
        <rrOperation :crud="crud" />
      </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="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="菜品名称" prop="foodName">
            <el-input v-model="form.foodName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="结算卡号" prop="cardCode">
            <el-input v-model="form.cardCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="菜品单价" prop="foodPrice">
            <el-input v-model="form.foodPrice" style="width: 370px;" @input="getTotalPrice" />
          </el-form-item>
          <el-form-item label="点餐数量" prop="foodNum">
            <el-input v-model="form.foodNum" style="width: 370px;" @input="getTotalPrice" />
          </el-form-item>
          <el-form-item label="合计" prop="totalPrice">
            <el-input v-model="form.totalPrice" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="点餐时间" prop="orderTime">
            <el-date-picker v-model="form.orderTime" type="datetime" 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.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="bh" label="订单编号" />
        <el-table-column prop="foodName" label="菜品名称" />
        <el-table-column prop="foodPrice" label="菜品单价" />
        <el-table-column prop="foodNum" label="点餐数量" />
        <el-table-column prop="dmUser.name" label="用户名称" />
        <el-table-column prop="totalPrice" label="合计" />
        <el-table-column prop="orderTime" label="点餐时间" />
        <el-table-column prop="cardCode" label="结算卡号" />
        <!--        <el-table-column prop="createBy" label="创建者" />-->
        <!--        <el-table-column prop="updateBy" label="更新者" />-->
        <el-table-column prop="createTime" label="创建日期" />
        <!--        <el-table-column prop="updateTime" label="更新时间" />-->

        <el-table-column v-if="checkPer(['admin','dmSettlement:edit','dmSettlement:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDmSettlement from '@/api/dm/settlement/dmSettlement'
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 DateRangePicker from '@/components/DateRangePicker'
import { validatePrice, validateNum } from '@/utils/validate'

const defaultForm = { id: null, foodName: null, foodPrice: null, foodNum: null, totalPrice: null, orderTime: null, createBy: null, updateBy: null, createTime: null, updateTime: null, cardCode: null }
export default {
  name: 'DmSettlement',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '结算记录', url: 'api/dmSettlement', idField: 'id', sort: 'id,desc', crudMethod: { ...crudDmSettlement },
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: true,
        reset: true
      }})
  },
  data() {
    const validPrice = async(rule, value, callback) => {
      if (!value) {
        callback(new Error('数据不能为空'))
      } else if (validatePrice(value)) {
        callback()
      } else {
        callback(new Error('格式错误,只能输入含有两位小数内的10位正数'))
      }
    }
    const validNum = async(rule, value, callback) => {
      if (!value) {
        callback(new Error('数据不能为空'))
      } else if (validateNum(value)) {
        callback()
      } else {
        callback(new Error('格式错误,只能输入正整数'))
      }
    }
    return {
      permission: {
        add: ['admin', 'dmSettlement:add'],
        edit: ['admin', 'dmSettlement:edit'],
        del: ['admin', 'dmSettlement:del']
      },
      rules: {
        id: [
          { required: true, message: '主键不能为空', trigger: 'blur' }
        ],
        foodName: [
          { required: true, message: '菜品名称不能为空', trigger: 'blur' }
        ],
        foodPrice: [
          { required: true, validator: validPrice, trigger: 'blur' }
        ],
        foodNum: [
          { required: true, validator: validNum, trigger: 'blur' }
        ],
        totalPrice: [
          { required: true, validator: validPrice, trigger: 'blur' }
        ],
        orderTime: [
          { required: true, message: '点餐时间不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'foodName', display_name: '菜品名称' },
        { key: 'foodPrice', display_name: '菜品单价' },
        { key: 'foodNum', display_name: '点餐数量' },
        { key: 'totalPrice', display_name: '小计(点餐价格合计)' },
        { key: 'cardCode', display_name: '结算卡号' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getTotalPrice() {
      console.info('getTotalPrice ')
      if (!this.form.foodPrice || !this.form.foodNum) {
        this.form.totalPrice = 0
      } else {
        this.form.totalPrice = this.form.foodPrice * this.form.foodNum
      }
    }

  }
}
</script>

<style scoped>

</style>
