<template>
  <el-dialog
    v-loading="loading"
    :title="'设置订单用途'"
    :center="true"
    align="left"
    top="18vh"
    width="500px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="showModelThis = false"
  >
    <el-card style="margin: -20px 0 0 0">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            label-width="80px"
            class="demo-ruleForm"
          >
            <el-form-item label="订单用途" prop="useid">
              <el-select
                v-model="ruleForm.useid"
                filterable
                style="width: 100%"
                clearable
                placeholder="订单用途"
              >
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.order_use"
                  :value="item.id"
                  :disabled="item.status !== '1'"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <span class="fl" v-if="code">已选{{ code.length }}个订单</span>
          <el-button type="primary" @click="submitForm">保 存 </el-button>
          <el-button @click="showModelThis = false">取 消</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/sellOut/salesOrder";
import resToken from "@/mixins/resToken";
export default {
  name: "brand",
  props: ["showModel", "code"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      title: "添加单位",
      showModelThis: this.showModel,
      options: [],
      ruleForm: {
        orderCode: [],
        useid: "",
      },
      rulesThis: this.rules,
      rules: {
        useid: [
          { required: true, message: "请选择订单用途", trigger: "change" },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    async initForm() {
      this.loading = true;
      this.rulesThis = this.rules;
      this.options = [];
      await this.resetForm();
      await this.get_options();
      this.loading = false;
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();

          this.ruleForm = {
            orderCode: this.code || [],
            useid: "",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          let res = (res = await asyncRequest.saleuse(model));

          this.loading = false;
          if (res && res.code === 0) {
            this.$notify.success({
              title: "设置成功！",
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async get_options() {
      const { code, data, message } = await asyncRequest.uselist({
        page: 1,
        size: 10000,
      });
      if (code === 0) {
        const { list } = data;
        this.options = list;
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.options = [];
        this.$message.warning(message);
      }
    },
  },
};
</script>

   <style lang="scss" scoped>
.brand {
}
</style>
