<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-04-14 09:50:16
 * @LastEditors: 无尘
 * @LastEditTime: 2020-05-14 14:39:42
 -->
<!--
  <select-group :materialIds="materialIds"  @closeGroup="closeGroup" @submitGroup="submitGroup"></select-group>
  import selectGroup from '@/components/set/select-group.vue';
 -->
<template>
  <el-dialog title="批量操作-移至分组" :visible.sync="dialogVisible" width="485px" :before-close="handleClose">
    <div class="">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分组" prop="categoryId">
          <el-select v-model="ruleForm.categoryId" placeholder="请选择">
            <el-option label="未分组" value="-1"></el-option>
            <el-option v-for="item in groupData" :key="item.categoryId" :label="item.categoryName" :value="item.categoryId"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="toCancel">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import limitInput from '@/components/limit-input.vue';
import { _debounce } from '@/common/js/public';
import { postRequest } from '@/api/api';
import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
export default {
  props: {
    materialIds: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  components: {
    limitInput
  },
  data() {
    return {
      wxEnterpriseId: localStorage.getItem('userInfos') ? JSON.parse(localStorage.getItem('userInfos')).wxEnterpriseId : '',
      dialogVisible: true,
      groupData: [],
      ruleForm: {
        categoryId: ''
      },
      rules: {
        categoryId: [{ required: true, message: '请选择分组名称', trigger: 'change' }]
      }
    };
  },
  methods: {
    toCancel() {
      const that = this;
      that.$emit('closeGroup');
      that.$refs['ruleForm'].resetFields();
    },
    handleClose(done) {
      const that = this;
      that.$emit('closeGroup');
      that.$refs['ruleForm'].resetFields();
    },
    submitForm: _debounce(function(formName) {
      const that = this;
      that.$refs[formName].validate(valid => {
        if (valid) {
          that.postSave();
        }
      });
    }, 300),
    postSave() {
      const that = this;
      const data = {
        categoryId: that.ruleForm.categoryId,
        materialIds: that.materialIds
      };
      postRequest('/haoban-manage3-web/material-batch', data)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('操作成功', 'success');
            that.$refs['ruleForm'].resetFields();
            that.$emit('submitGroup');
          } else {
            errMsg.errorMsg(resData);
          }
        })
        .catch(error => {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 获取数据
     */
    getGroupData(val) {
      const that = this;
      let para = {
        wxEnterpriseId: that.wxEnterpriseId
      };
      postRequest('/haoban-manage3-web/category-list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.groupData = resData.result || [];
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {},
  mounted() {
    const that = this;
    that.getGroupData();
  }
};
</script>
<style lang="less" scoped>
.m-b-20 {
  margin-bottom: 20px;
}
.p-l-18 {
  padding-left: 18px;
}
</style>
