<template>
  <el-dialog v-if="show" title="美团刷单报销" :visible.sync="show" width="500px">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="门店" prop="shop">
        <el-input v-model="form.shop" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="报销人" prop="createUser">
        <el-select v-model="form.createUser" placeholder="请选择" filterable clearable>
          <el-option v-for="dict in userOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="expenseDate">
        <el-date-picker v-model="form.expenseDate" value-format="yyyy-MM-dd" type="date" />
      </el-form-item>
      <el-form-item label="成交班型" prop="signClass">
        <el-input v-model="form.signClass" placeholder="请输入" />

        <!-- <el-select v-model="form.typeId" placeholder="请选择" filterable clearable>
          <el-option v-for="dict in classTypeOptions" :key="dict.typeId" :label="dict.schoolName +'-'+ dict.placeName +'-'+ dict.typeName" :value="dict.typeId" />
        </el-select> -->
      </el-form-item>
      <el-form-item label="成交价格" prop="signPrice">
        <el-input v-model="form.signPrice" placeholder="请输入" type="number" />
      </el-form-item>
      <el-form-item label="实际入账" prop="actualEntry">
        <el-input v-model="form.actualEntry" placeholder="请输入" type="number" />
      </el-form-item>
      <el-form-item label="手续费" prop="commission">
        <el-input v-model="form.commission" placeholder="请输入" type="number" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入" />
      </el-form-item>
    </el-form>

    <span slot="footer">
      <!--有权限 且 新增或修改时展示 -->
      <!-- <el-button type="primary" v-if="form.state==0 || form.state==3" v-hasPermi="['finance:expense:account:add', 'finance:expense:account:edit']" @click="handleSave(0)">保存</el-button> -->
      <el-button type="primary" v-if="form.state==0 || form.state==3" v-hasPermi="['finance:expense:account:add', 'finance:expense:account:edit']" @click="handleSave(1)">提交</el-button>
      <!--有权限 且 审核时展示 -->
      <el-button type="primary" v-if="form.state==1" v-hasPermi="['finance:expense:account:check']" @click="handleCheck(2)">通过</el-button>
      <el-button type="primary" v-if="form.state==1" v-hasPermi="['finance:expense:account:check']" @click="handleCheck(3)">驳回</el-button>
      <el-button @click="show = false">关闭</el-button>
    </span>
  </el-dialog>

</template>
  
  <script>

import { addExpense, updateExpense, checkExpense } from '@/api/finance/expense/meituanExpense'
import { getClassTypeTableList } from '@/api/sch/classType';


export default {
  props: {
    userOptions: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      userId: localStorage.getItem('userId'),
      show: false,
      showColumns: [],
      form: {
        createUser: undefined,
        expenseDate: undefined,
        shop: undefined,
        typeId: undefined,
        signPrice: undefined,
        actualEntry: undefined,
        commission: undefined,
        remark: undefined,
        state: 0
      },
      rules: {
        createUser: { required: true, message: '请选择', trigger: 'blur' },
        shop: { required: true, message: '请输入', trigger: 'blur' },
        expenseDate: { required: true, message: '请输入', trigger: 'blur' },
        signClass: { required: true, message: '请输入', trigger: 'blur' },
        signPrice: { required: true, message: '请输入', trigger: 'blur' },
        actualEntry: { required: true, message: '请输入', trigger: 'blur' },
        commission: { required: true, message: '请输入', trigger: 'blur' }
      },
      classTypeOptions: []
    };
  },
  methods: {
    init(data) {
      this.getClassTypes()
      this.$nextTick(() => {
        this.resetForm();
        if (data) {
          this.form = this.deepClone(data);
          // this.$refs['form'].resetFields();
        }
        this.show = true
      });
    },
    resetForm() {
      this.form = {
        createUser: this.userId,
        expenseDate: undefined,
        shop: undefined,
        typeId: undefined,
        signPrice: undefined,
        actualEntry: undefined,
        commission: undefined,
        remark: undefined,
        state: 0
      }
    },
    handleSave(state) {
      this.form.state = state;
      this.$refs.form.validate(async valid => {
        if (valid) {

          if (state == 1) {
            this.$confirm('提交后将进入审核阶段，是否提交确认提交?', '警告', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            })
              .then((res) => {
                this.handleExpenseSave();
              })
              .catch(function () { });
          } else {
            this.handleExpenseSave();
          }
        }
      });
    },
    handleExpenseSave() {
      // 提交申请
      if (this.form.expenseId) {
        updateExpense(this.form).then(resp => {
          if (resp.code == 200) {
            this.$message.success('修改成功');
            this.$emit('refreshDataList');
            this.show = false;
          }
        })
      } else {
        addExpense(this.form).then(resp => {
          if (resp.code == 200) {
            this.$message.success('新增成功');
            this.$emit('refreshDataList');
            this.show = false;
          }
        })
      }
    },

    handleCheck(state) {
      this.form.state = state;
      checkExpense(this.form).then(resp => {
        if (resp.code == 200) {
          this.$message.success('审核成功');
          this.$emit('refreshDataList');
          this.show = false;
        }
      })
    },
    getClassTypes() {
      getClassTypeTableList({}).then(resp => {
        if (resp.code == 200) {
          this.classTypeOptions = resp.rows;
        }
      })
    }
  }
};
  </script>
  
  <style lang="scss" scoped></style>
  