<template>
  <el-form
    ref="ruleForm"
    :model="ruleForm"
    status-icon
    :size="'mini'"
    :rules="rulesThis"
    label-width="90px"
    class="demo-ruleForm"
  >
    <el-row>
      <el-col :span="7">
        <el-form-item label="客户是否接受工差" prop="is_act" label-width="135px">
          <el-select
            v-model="ruleForm.is_act"
            style="width: 100%"
            :size="'mini'"
            :disabled="!(status === '1' && ppowers.some((i) => i == '1'))"
            placeholder="客户是否接受工差"
          >
            <el-option
              v-for="item in is_actOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="客户意愿"
          prop="customer_remark"
          v-show="ruleForm.is_act === '1'"
        >
          <el-select
            v-model="ruleForm.customer_remark"
            style="width: 100%"
            :size="'mini'"
            :disabled="!(status === '1' && ppowers.some((i) => i == '1'))"
            placeholder="客户意愿"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="17">
        <el-form-item label="审核备注" prop="remark">
          <el-input
            type="textarea"
            :rows="2"
            :size="'mini'"
            :disabled="!(status === '1' && ppowers.some((i) => i == '1'))"
            placeholder="业务审核备注"
            maxlength="250"
            show-word-limit
            v-model="ruleForm.remark"
          />
        </el-form-item>
        <div
          style="width: 100%; padding: 0 0 12px 0"
          class="tr"
          v-if="status === '1' && ppowers.some((i) => i == '1')"
        >
          <el-button type="primary" :size="'mini'" @click="submitForm">保 存 </el-button>
        </div>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import asyncRequest from "@/apis/service/sellOut/zxDiffOrder";
import resToken from "@/mixins/resToken";

export default {
  name: "Account",
  props: ["newTime", "sitem"],
  mixins: [resToken],
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "zxDiffOrderDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find((i) => i.process_type === "XSGCD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      loading: false,
      status: "",
      options: [
        {
          value: "1",
          label: "竞价订单减工差",
        },
        {
          value: "2",
          label: "竞价订单不减工差",
        },
      ],
      is_actOptions: [
        {
          value: "1",
          label: "接受工差",
        },
        {
          value: "2",
          label: "不接受工差-退回重做",
        },
      ],

      ruleForm: {
        id: "",
        status: "",
        is_act: "1",
        customer_remark: "1",
        remark: "",
      },
      rulesThis: this.rules,
      rules: {
        is_act: [
          {
            required: true,
            message: "请选择客户是否接受工差！",
            trigger: "change",
          },
        ],
        customer_remark: [
          {
            required: true,
            message: "请选择客户意愿！",
            trigger: "change",
          },
        ],
        remark: [
          {
            required: true,
            message: "审核备注不能为空！",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  mounted() {
    this.initForm();
  },
  methods: {
    async supplierChange(e) {
      if (e && e.id) {
        this.ruleForm.supplierNo = [e.code];
      } else {
        this.ruleForm.supplierNo = [];
      }
      this.$refs.ruleForm.validateField("supplierNo");
    },

    async initForm() {
      this.loading = true;
      this.status = "";
      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 { is_act, customer_remark, id, remark, status } = this.sitem;
          this.status = status;
          this.ruleForm = {
            id: id || "",
            status: "",
            is_act: is_act === "1" || is_act === "2" ? is_act : "1",
            customer_remark:
              customer_remark === "1" || customer_remark === "2" ? customer_remark : "1",
            remark: remark || "",
          };
        }
      });
    },
    addrRefresh(e) {
      const { list } = e;
      this.addrForm.addrlist.push(...list);
    },
    openHouseModal(index) {
      let findex = this.addrForm.addrlist.findIndex((v) => v.edit === true);
      if (findex !== -1) {
        this.$message.warning("当前已有地址在编辑，请保存后再试！");
        return;
      } else {
        if (index === -1) {
          this.addrForm.addrlist.push({
            edit: true,
            arrive_time: "",
            receipt_quantity: "",
            contactor: "",
            mobile: "",
            addr_code: [],
            addr: "",
          });
        } else {
          this.addrForm.addrlist[index].edit = true;
        }
      }
    },
    //省市区选择
    select_area_change(e, index) {
      this.addrForm.addrlist[index].addr_code = e;
    },
    //省市区保存某一行
    checkRow(rowIndex) {
      this.$refs.addrForm.validate((valid) => {
        if (valid) {
          this.addrForm.addrlist[rowIndex].edit = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 省市区删除行操作
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const model = JSON.parse(JSON.stringify(this.ruleForm));
          model.status = "2";
          let res = await asyncRequest.status(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;
        }
      });
    },
  },
};
</script>

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