<template>
  <div id="add">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <el-button type="primary" size="mini" @click="back()">返回</el-button>
        </el-col>
        <el-col :span="24" class="two">
          <el-form :model="form" :rules="rules" ref="form" label-width="130px">
            <el-col :span="24">
              <el-col :span="12">
                <el-form-item label="行业类别" prop="industry_type">
                  <el-select v-model="form.industry_type" clearable placeholder="请选择" style="width: 100%">
                    <el-option label="保安监管" value="保安监管"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="接受区域" prop="receive_region">
                  <el-select v-model="form.receive_region" clearable placeholder="请选择" style="width: 100%">
                    <el-option label="公安机关" value="公安机关"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col :span="24">
              <el-col :span="12">
                <el-form-item label="接收单位" prop="receive_company">
                  <el-input v-model="form.receive_company" placeholder="请输入接收单位"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="发布人" prop="release_personal">
                  <el-input v-model="form.release_personal" placeholder="请输入发布人"></el-input>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col :span="24">
              <el-col :span="12">
                <el-form-item label="发布时间" prop="release_date">
                  <el-date-picker
                    v-model="form.release_date"
                    placeholder="请选择"
                    value-format="yyyy-MM-dd"
                    format="yyyy-MM-dd"
                    type="date"
                    style="width: 100%"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="截止时间" prop="stop_date">
                  <el-date-picker v-model="form.stop_date" placeholder="请选择" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" style="width: 100%">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col :span="24">
              <el-col :span="12">
                <el-form-item label="发布单位" prop="release_company">
                  <el-input v-model="form.release_company" placeholder="请输入发布单位"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="标题" prop="title">
                  <el-input v-model="form.title" placeholder="请输入标题"></el-input>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col :span="24">
              <el-col :span="24">
                <el-form-item label="通知通报内容" prop="content">
                  <el-input v-model="form.content" placeholder="请输入通知通报内容" type="textarea" :autosize="{ minRows: 4, maxRows: 6 }"></el-input>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col :span="24">
              <el-col :span="24">
                <el-form-item label="附件" prop="file_url">
                  <s-upload
                    :limit="1"
                    :data="form.file_url"
                    type="file_url"
                    :url="'/files/baoan/fileurl/upload'"
                    listType=""
                    @upload="uplSuc"
                    @delete="uplDel"
                  ></s-upload>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col :span="24" class="btn">
              <el-button type="danger" size="small" @click="back">取消保存</el-button>
              <el-button type="primary" size="small" @click="onSubmit('form')">提交保存</el-button>
            </el-col>
          </el-form>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: assistance_investigation } = createNamespacedHelpers('assistance_investigation');
export default {
  name: 'add',
  props: {},
  components: {},
  data: function () {
    return {
      form: {},
      rules: {
        industry_type: [{ required: true, message: '请选择', trigger: 'change' }],
        receive_region: [{ required: true, message: '请选择', trigger: 'change' }],
        stop_date: [{ required: true, message: '请选择', trigger: 'change' }],
        receive_company: [{ required: true, message: '请输入接收单位', trigger: 'blur' }],
        release_personal: [{ required: true, message: '请输入发布人', trigger: 'blur' }],
        release_date: [{ required: true, message: '请选择', trigger: 'change' }],
        release_company: [{ required: true, message: '请输入发布单位', trigger: 'blur' }],
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入通知通报内容', trigger: 'blur' }],
        file_url: [{ required: true, message: '请选择', trigger: 'change' }],
      },
    };
  },
  async created() {
    if (this.id) await this.search();
  },
  methods: {
    ...assistance_investigation(['fetch', 'create', 'update']),
    async search() {
      let res = await this.fetch({ id: this.id });
      if (this.$checkRes(res)) {
        this.$set(this, `form`, res.data);
      }
    },
    // 提交
    onSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let data = this.form;
          if (data.id) {
            let res = await this.update(data);
            if (this.$checkRes(res)) {
              this.$message({ type: `success`, message: `操作成功` });
              this.back();
            } else {
              this.$message({ type: `success`, message: `${res.errmsg}` });
            }
          } else {
            let res = await this.create(data);
            if (this.$checkRes(res)) {
              this.$message({ type: `success`, message: `操作成功` });
              this.back();
            } else {
              this.$message({ type: `success`, message: `${res.errmsg}` });
            }
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 返回
    back() {
      this.$router.push({ path: '/adminPolice/notification/xctbgl' });
    },
    // 上传图片
    uplSuc({ type, data }) {
      this.$set(this.form, `${type}`, data.uri);
    },
    uplDel({ type }) {
      this.$set(this.form, `${type}`, null);
    },
  },
  computed: {
    ...mapState(['user']),
    id() {
      return this.$route.query.id;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    text-align: right;
    margin: 10px 0;
  }
  .two {
    .btn {
      text-align: center;
    }
    // .longlabel {
    //   /deep/.el-form-item__label {
    //     line-height: 20px;
    //     padding: 0 12px;
    //   }
    // }
  }
}
</style>
