<template>
  <el-dialog v-if="show" title="报名点报销" :visible.sync="show" width="600px">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="检索">
        <el-autocomplete v-model="searchValue" :fetch-suggestions="querySearchAsync" style="width: 100%;" placeholder="请输入姓名/联系方式进行学员检索" @select="handleSelect">
          <template slot-scope="{ item }">
            <span class="name">{{ item.name }}</span>
            <span class="addr">{{ item.phone }}</span>
          </template>
        </el-autocomplete>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入" disabled />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入" disabled />
      </el-form-item>
      <el-form-item label="报名时间" prop="dealDate">
        <el-date-picker v-model="form.dealDate" value-format="yyyy-MM-dd" type="date" disabled></el-date-picker>
      </el-form-item>
      <el-form-item label="成交价格" prop="signPrice">
        <el-input v-model="form.signPrice" placeholder="请输入" disabled />
      </el-form-item>
      <el-form-item label="报名驾校" prop="schoolName">
        <el-input v-model="form.schoolName" placeholder="请输入" disabled />
      </el-form-item>
      <el-form-item label="班型" prop="className">
        <el-input v-model="form.className" placeholder="请输入" type="number" disabled />
      </el-form-item>
      <el-form-item label="利润" prop="profit">
        <el-input v-model="form.profit" placeholder="请输入" type="number" disabled />
      </el-form-item>
      <el-form-item label="额外支出项目" prop="expenseType">
        <el-select v-model="form.expenseType" placeholder="请选择" filterable clearable disabled>
          <el-option v-for="dict in extraOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="额外支出金额" prop="money">
        <el-input v-model="form.money" placeholder="请输入" type="number" />
      </el-form-item>
      <el-form-item label="学员归属" prop="followUser">
        <el-select v-model="form.followUser" placeholder="请选择" filterable clearable disabled>
          <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="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/signExpense'
import { listSign } from '@/api/zs/placeSign'

export default {
  props: {
    userOptions: {
      type: Array,
      default: []
    },
    extraOptions: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      show: false,
      showColumns: [],
      searchValue: undefined,
      form: {
        signId: undefined,
        expenseType: undefined,
        money: undefined,
        remark: undefined,
        state: 0,
        // 登记信息
        name: undefined,
        phone: undefined,
        dealDate: undefined,
        signPrice: undefined,
        schoolName: undefined,
        className: undefined,
        profit: undefined

      },
      rules: {
        name: { required: true, message: '请输入', trigger: 'blur' },
        phone: { required: true, message: '请输入', trigger: 'blur' },
        expenseType: { required: true, message: '请输入', trigger: 'blur' },
        money: { required: true, message: '请输入', trigger: 'blur' },
      },
      restaurants: [],
    };
  },
  methods: {
    init(data) {
      this.$nextTick(() => {
        this.resetForm();
        if (data) {
          this.form = this.deepClone(data);
          // this.$refs['form'].resetFields();
        }
        this.show = true
      });
    },
    resetForm() {
      this.form = {
        signId: undefined,
        expenseType: undefined,
        money: undefined,
        remark: undefined,
        state: 0,
        name: undefined,
        phone: undefined,
        dealDate: undefined,
        signPrice: undefined,
        schoolName: undefined,
        className: undefined,
        profit: undefined

      }
    },
    //检索查询登记信息
    async querySearchAsync(queryString, cb) {
      if (queryString) {
        const resp = listSign({
          name: queryString
        });
        cb(resp.rows);
      }
    },
    //选择对应的查询结果
    handleSelect(item) {
      this.$set(this.form, "signId", item.signId);
      this.$set(this.form, "name", item.name);
      this.$set(this.form, "phone", item.phone);
      this.$set(this.form, "dealDate", item.dealDate);
      this.$set(this.form, "signPrice", item.signPrice);
      this.$set(this.form, "schoolName", item.schoolName);
      this.$set(this.form, "className", item.className);
      this.$set(this.form, "profit", item.profit);
    },
    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;
        }
      })
    },

  }
};
  </script>
  
  <style lang="scss" scoped></style>
  