<template>
  <div class="cmain">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="结算批次" prop="batchName">
        <el-input v-model="form.batchName" placeholder="请输入结算批次" clearable />
      </el-form-item>
      <el-form-item label="发票类型" prop="invoice">
        <el-select v-model="form.invoice" placeholder="请选择发票类型">
          <el-option v-for="dict in dict.type.invoice" :key="dict.value" :label="dict.label" :value="dict.value" clearable></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付渠道" prop="channel">
        <el-select v-model="form.channel" placeholder="请选择支付渠道">
          <el-option v-for="dict in dict.type.channel" :key="dict.value" :label="dict.label" :value="dict.value" clearable></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关联项目" prop="projectId">
        <projectSelect v-model="form.projectId"></projectSelect>
      </el-form-item>
      <el-form-item label="上传结算单">
        <el-upload ref="upload" :limit="1" action="#" accept=".xlsx, .xls" :auto-upload="false" :on-remove="remove" :on-change="change" drag>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将文件拖到此处，或
            <em>点击上传</em>
          </div>
          <!-- 上传提示 -->
          <div class="el-upload__tip" slot="tip" style="margin-top: 0px; line-height: 30px">
            <span>仅允许导入“xls”或“xlsx”格式文件,请</span>
            <a style="padding-left: 10px; color: #1890ff" @click="importTemplate()">下载结算模板</a>
            <span>，并编辑完后再上传</span>
          </div>
        </el-upload>
      </el-form-item>
    </el-form>
    <div class="mfooter">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="$layer.close(layerid)">取 消</el-button>
    </div>
  </div>
</template>

<script>
export default {
  dicts: ['invoice', 'channel'],
  data() {
    return {
      form: {},
      rules: {
        batchName: [{ required: true, message: '不能为空', trigger: 'blur' }],
        invoice: [{ required: true, message: '不能为空', trigger: 'blur' }],
        channel: [{ required: true, message: '不能为空', trigger: 'blur' }],
        projectId: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    };
  },
  props: {
    param: {
      type: Object,
      default: () => {
        return {};
      }
    },
    layerid: {
      type: String
    }
  },
  methods: {
    change(file, fileList) {
      if (file.name.indexOf('.xls') == -1) {
        this.msgError('仅允许导入“xls”或“xlsx”格式文件。');
        this.$refs.upload.clearFiles();
        return;
      }
      this.form.file = file.raw;
    },
    remove(file) {
      this.form.file = '';
    },
    importTemplate() {
      this.download('work/statement/make/download', {}, '结算单模版.xlsx');
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (!this.form.file) {
            this.$modal.msgError('请上传结算单');
            return;
          }
          let formData = new FormData();
          for (let key in this.form) {
            formData.append(key, this.form[key]);
          }
          this.ajax({ method: 'post', url: '/work/statement/make/add', data: formData }).then((response) => {
            this.$modal.msgSuccess('创建成功');
            this.$layer.close(this.layerid);
            this.$parent.getList();
          });
        }
      });
    }
  }
};
</script>
