<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="reward_date">
                  <el-date-picker v-model="form.reward_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="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>
            <el-col :span="24">
              <el-col :span="12">
                <el-form-item label="企业名称" prop="company_name">
                  <el-input v-model="form.company_name" placeholder="请输入企业名称"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="企业编号" prop="company_num">
                  <el-input v-model="form.company_num" placeholder="请输入企业编号"></el-input>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col :span="24">
              <el-col :span="12">
                <el-form-item label="奖励人" prop="reward_personal">
                  <el-input v-model="form.reward_personal" placeholder="请输入奖励人"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="奖励结果" prop="reward_result">
                  <el-select v-model="form.reward_result" clearable placeholder="请选择" style="width: 100%">
                    <el-option label="通报表扬" value="通报表扬"></el-option>
                    <el-option label="物质奖励" value="物质奖励"></el-option>
                    <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="register_personal">
                  <el-input v-model="form.register_personal" placeholder="请输入登记人"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="登记时间" prop="register_date">
                  <el-date-picker
                    v-model="form.register_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="reward_grade">
                  <el-select v-model="form.reward_grade" clearable placeholder="请选择" style="width: 100%">
                    <el-option label="国家" value="国家"></el-option>
                    <el-option label="省级" value="省级"></el-option>
                    <el-option label="市级" value="市级"></el-option>
                    <el-option label="县级" value="县级"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="所属单位名称" prop="belonging_company">
                  <el-input v-model="form.belonging_company" placeholder="请输入所属单位名称"></el-input>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col :span="24">
              <el-col :span="24">
                <el-form-item label="奖励原因" prop="reward_reason">
                  <el-input v-model="form.reward_reason" 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="remark">
                  <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" :autosize="{ minRows: 4, maxRows: 6 }"></el-input>
                </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: enterprise_reward } = createNamespacedHelpers('enterprise_reward');
export default {
  name: 'add',
  props: {},
  components: {},
  data: function () {
    return {
      form: {},
      rules: {
        reward_date: [{ required: true, message: '请选择', trigger: 'change' }],
        industry_type: [{ required: true, message: '请选择', trigger: 'change' }],
        company_name: [{ required: true, message: '请输入企业名称', trigger: 'blur' }],
        company_num: [{ required: true, message: '请输入企业编码', trigger: 'blur' }],
        reward_personal: [{ required: true, message: '请输入奖励人', trigger: 'blur' }],
        reward_result: [{ required: true, message: '请选择', trigger: 'change' }],
        register_personal: [{ required: true, message: '请输入登记人', trigger: 'blur' }],
        register_date: [{ required: true, message: '请选择', trigger: 'change' }],
        reward_grade: [{ required: true, message: '请选择', trigger: 'change' }],
        belonging_company: [{ required: true, message: '请输入所属单位名称', trigger: 'blur' }],
        reward_reason: [{ required: true, message: '请输入奖励原因', trigger: 'blur' }],
        remark: [{ required: true, message: '请输入备注', trigger: 'blur' }],
      },
    };
  },
  async created() {
    if (this.id) await this.search();
  },
  methods: {
    ...enterprise_reward(['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/reward/qyjlgl' });
    },
  },
  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>
