<template>
  <div id="m-upload">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <data-table :fields="fields" :opera="opera" :data="list" :usePage="false" @view="toView">
            <template slot="btn">
              <el-button v-if="!noUpload" type="primary" size="mini" @click="show = true">添加</el-button>
            </template>
          </data-table>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog
      class="dialog"
      title="电子资料信息管理"
      :visible.sync="show"
      width="30%"
      :before-close="close"
      :close-on-click-modal="false"
      :append-to-body="true"
    >
      <el-form :model="form" :rules="rules" ref="form" label-width="130px">
        <el-form-item label="电子资料类型" prop="type">
          <el-input v-model="form.type" placeholder="请输入电子资料类型"></el-input>
        </el-form-item>
        <el-form-item label="电子资料名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入电子资料名称"></el-input>
        </el-form-item>
        <el-form-item label="电子资料文件" prop="el_img">
          <s-upload :limit="1" :data="form.el_img" type="el_img" :url="uri" @upload="uplSuc" @delete="uplDel"></s-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form')">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: digital_files } = createNamespacedHelpers('digital_files');
export default {
  name: 'm-upload',
  props: {
    basic_id: { type: String },
    uri: { type: String, default: '/files/baoan/elimg/upload' },
    noUpload: { type: Boolean, default: false },
  },
  components: {},
  data: function () {
    return {
      // 列表
      fields: [
        { label: '电子材料类型', model: 'type' },
        { label: '电子材料名称', model: 'name' },
        { label: '文件地址', model: 'el_img' },
      ],
      opera: [{ label: '下载', method: 'view' }],
      list: [],
      // 添加
      show: false,
      form: {},
      rules: {
        type: [{ required: false, message: '请输入电子资料类型', trigger: 'blur' }],
        name: [{ required: false, message: '请输入电子资料名称', trigger: 'blur' }],
        el_img: [{ required: false, message: '请选择', trigger: 'change' }],
      },
    };
  },
  created() {},
  methods: {
    ...digital_files(['query', 'create', 'update']),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      info.main_id = this.basic_id;
      let res = await this.query({ skip, limit, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let data = this.form;
          let res;
          if (data.id) {
            res = await this.update(data);
          } else {
            data.main_id = this.basic_id;
            res = await this.create(data);
          }
          if (this.$checkRes(res, '操作成功', res.errmsg || '操作失败')) {
            this.search();
            this.show = false;
            this.form = {};
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 查看
    toView({ data }) {
      window.open(`${process.env.VUE_APP_HOST}${data.el_img}`, '_blank');
    },
    // 取消添加
    close() {
      this.form = {};
      this.show = false;
    },
    // 上传图片
    uplSuc({ type, data }) {
      this.$set(this.form, `${type}`, data.uri);
    },
    uplDel({ type }) {
      this.$set(this.form, `${type}`, null);
    },
  },
  watch: {
    basic_id: {
      deep: true,
      immediate: true,
      handler(val) {
        if (val) this.search();
      },
    },
  },
};
</script>

<style lang="less" scoped></style>
