<template>
  <el-form
    ref="ruleForm"
    :loading="loading"
    :model="ruleForm"
    status-icon
    :rules="rulesThis"
    label-width="100px"
    style="width: 100%"
    class="demo-ruleForm"
  >
    <el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否同意" prop="is_th">
            <el-select
              style="width: 100%"
              :disabled="
                !(status == '1' && powers.some((item) => item == '046'))
              "
              v-model="ruleForm.is_th"
              placeholder="是否同意退回"
            >
              <el-option
                v-for="item in is_th_options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="反馈备注" prop="remark">
            <el-input
              type="textarea"
              placeholder="反馈备注"
              :disabled="
                !(status == '1' && powers.some((item) => item == '046'))
              "
              v-model="ruleForm.remark"
              maxlength="250"
              show-word-limit
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="ruleForm.is_th === '1'">
        <el-col :span="12">
          <el-form-item label="收件人" prop="contactor">
            <el-input
              :disabled="
                !(status == '1' && powers.some((item) => item == '046'))
              "
              v-model="ruleForm.contactor"
              placeholder="收件人"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收件人电话" prop="mobile">
            <el-input
              :disabled="
                !(status == '1' && powers.some((item) => item == '046'))
              "
              v-model="ruleForm.mobile"
              placeholder="收件人电话"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址省市区" prop="addr_code">
            <select-area
              :placeholder="'省市区'"
              :disabled="
                !(status == '1' && powers.some((item) => item == '046'))
              "
              :value="ruleForm.addr_code"
              :is-detail="id !== 'add'"
              @selectChange="selectAreaAddr_code"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="详细地址" prop="addr">
            <el-input
              v-model="ruleForm.addr"
              :disabled="
                !(status == '1' && powers.some((item) => item == '046'))
              "
              placeholder="详细地址"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-col
        :span="24"
        style="text-align: right;padding:0 0 20px 0;"
        v-if="status == '1' && powers.some((item) => item == '046')"
      >
        <el-button type="primary" :size="'mini'" @click="submitForm"
          >保 存</el-button
        >
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { isMobile } from "@/utils/validate";
import asyncRequest from "@/apis/service/sellOut/zixunOrder";
import resToken from "@/mixins/resToken";
export default {
  name: "zixunOrderDetail",
  props: ["id", "showModelThis", "newTime", "sitem"],
  mixins: [resToken],
  data() {
    const validatemobile = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("手机号不能为空！"));
      } else {
        if (!isMobile(value)) {
          callback(new Error("手机号格式不正确！"));
        } else {
          callback();
        }
      }
    };
    return {
      loading: false,
      status: "",
      showModelThis: this.showModel,
      ruleForm: {
        is_th: "1", //是否同意退回
        remark: "", //反馈备注
        addr_code: [], //退货地址
        addr: "", //退货详细地址
        contactor: "", //收件人
        mobile: "", //收件人电话
        wsm_code: "-",
      },
      rulesThis: this.rules,
      // 验证规则
      rules: {
        is_th: [
          {
            required: true,
            trigger: "change",
            message: "请选择是否同意退回",
          },
        ],
        remark: [
          {
            required: true,
            message: "请输入反馈备注",
            trigger: "blur",
          },
        ],
        addr_code: [
          {
            type: "array",
            required: true,
            message: "请选择退回地址所在省/市/区",
            trigger: "change",
          },
        ],
        addr: [
          {
            required: true,
            message: "请输入详细退货地址",
            trigger: "blur",
          },
        ],
        contactor: [
          {
            required: true,
            message: "请输入收件人",
            trigger: "blur",
          },
        ],
        mobile: [
          {
            required: true,
            trigger: "blur",
            validator: validatemobile,
          },
        ],
      },
      is_th_options: [
        {
          value: "1",
          label: "同意退回",
        },
        {
          value: "0",
          label: "拒绝退回",
        },
      ],
    };
  },
  computed: {
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "zixunOrderDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  mounted() {
    this.initForm();
  },
  watch: {
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },
  methods: {
    closeAdd() {
      this.showModelThis = false;
    },
    async initForm() {
      this.status = "";
      this.loading = true;
      this.rulesThis = this.rules;
      await this.resetForm();
      this.loading = false;
    },

    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const {
            contactor,
            is_th,
            mobile,
            addr_code,
            addr,
            returnCode,
            gys_remark,
            status,
          } = this.sitem;
          let list = [];
          this.status = status;
          if (
            addr_code &&
            addr_code.area_code &&
            addr_code.city_code &&
            addr_code.provice_code
          ) {
            list.push(addr_code.provice_code);
            list.push(addr_code.city_code);
            list.push(addr_code.area_code);
          }

          this.ruleForm = {
            returnCode: returnCode || "",
            is_th: is_th || "1", //是否同意退回
            remark: gys_remark || "", //反馈备注
            addr_code: list.length === 3 ? list : [], //退货地址
            addr: addr || "", //退货详细地址
            contactor: contactor || "", //收件人
            mobile: mobile || "", //收件人电话
            wsm_code: "-",
          };
          console.log(this.ruleForm);
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          this.loading = true;

          let res = await asyncRequest.aftergys(model);
          this.loading = false;
          if (res && res.code === 0) {
            this.$notify.success({
              title: "提交成功！",
              message: "",
            });
            this.$emit("refresh", false);
          } 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;
        }
      });
    },

    //退货省市区
    selectAreaAddr_code(e) {
      this.ruleForm.addr_code = e;
      this.$refs.ruleForm.validateField("addr_code");
    },
  },
};
</script>