<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :size="1000"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-container v-loading="loading">
      <el-main style="padding: 0 20px 20px 20px">
        <el-form
          ref="dialogForm"
          :model="form"
          :rules="rules"
          label-width="120px"
        >
          <el-row>
            <el-col :md="12" :sm="24">
              <el-form-item label="任务编号" prop="number">
                <el-input v-model="form.number" placeholder="任务编号"/>
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="任务数">
                <el-input v-model="form.count" placeholder="任务数"/>
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="工序" prop="process">
                <el-select
                  v-model="form.process"
                  placeholder="工序"
                  clearable
                  style="width:100%"
                >
                  <el-option
                  v-for="item in processOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="产品" prop="material">
                <el-select
                  v-model="form.material"
                  placeholder="产品"
                  clearable
                  style="width:100%"
                >
                  <el-option
                  v-for="item in materialOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="计划开工日期">
                <el-date-picker
                  v-model="form.start_date"
                  type="date"
                  placeholder="计划开工日期"
                  value-format="YYYY-MM-DD"
                  style="width:100%"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="计划完工日期">
                <el-date-picker
                  v-model="form.end_date"
                  type="date"
                  placeholder="计划完工日期"
                  value-format="YYYY-MM-DD"
                  style="width:100%"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-main>
      <el-footer>
        <el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
        <el-button @click="visible = false">取消</el-button>
      </el-footer>
    </el-container>
  </el-dialog>
</template>
<script>
export default {
  emits: ["success", "closed"],
  data() {
    return {
      loading: false,
      mode: "add",
      titleMap: {
        add: "新增仓库",
        edit: "编辑仓库",
        show: "查看仓库",
      },
      form: {},
      rules: {
          name: [{required: true, message: "请输入仓库名称", trigger: "blur"}],
          number: [{required: true, message: "请输入仓库编号", trigger: "blur"}],
          place: [{required: true, message: "请输入仓库地点", trigger: "blur"}]
        },
      visible: false,
      isSaveing: false,
      keeperOptions: [],
      depOptions: [],
      selectionFilters: [],
      setFiltersVisible: false,
      belong_dept_options: [],
      group: [],
    };
  },
  mounted() {
  },
  methods: {
    //显示
    open(mode = "add",type) {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    //提交
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          try {
            var res;
            if (this.mode == "add") {
              res = await this.$API.pm.mtask.create.req(this.form);
            } else if (this.mode == "edit") {
              res = await this.$API.pm.mtask.update.req(this.form.id,this.form);
            }
            this.isSaveing = false;
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          } catch (err) {
            //可以处理校验错误
            this.isSaveing = false;
            return err;
          }
        }
      });
    },
    //表单注入数据
    setData(data) {
      Object.assign(this.form, data);
    },
    //设置过滤项
    setFilters(filters) {
      this.selectionFilters = filters;
      this.setFiltersVisible = true;
    },
  },
};
</script>
<style>
</style>
