<template>
  <el-row class="addForm" v-loading="loading">
    <el-col :span="24">
      <el-form
        :model="tableForm"
        :rules="tableFormThis"
        ref="tableForm"
        :size="'mini'"
        class="demo-tableForm product_go"
      >
        <el-table :data="tableForm.product_go" border :size="'mini'" row-key="key">
          <el-table-column label="异常数量" prop="anomalous_numbers" />
          <el-table-column label="异常原因" prop="anomalous_reason" />
          <el-table-column label="异常备注" prop="anomalous_remark" />
          <el-table-column label="审核情况" prop="check_value">
            <template slot-scope="scope">
              <el-form-item
                :style="!scope.row.edit ? 'margin:0' : ''"
                :size="'mini'"
                :prop="'product_go.' + scope.$index + '.check_value'"
                :rules="tableFormThis.check_value"
              >
                <el-select
                  :disabled="!scope.row.edit"
                  style="width: 100%"
                  v-model="scope.row.check_value"
                  placeholder="审核情况"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="审核备注" prop="check_remark">
            <template slot-scope="scope">
              <el-form-item
                :style="!scope.row.edit ? 'margin:0' : ''"
                :size="'mini'"
                :prop="'product_go.' + scope.$index + '.check_remark'"
                :rules="tableFormThis.check_remark"
              >
                <el-input
                  :disabled="!scope.row.edit"
                  v-model="scope.row.check_remark"
                  maxlength="100"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            label="操作"
            width="80"
            v-if="status === '2' && ppowers.some((i) => i == '2')"
          >
            <template slot-scope="scope">
              <el-tooltip
                effect="dark"
                content="编辑"
                v-if="!scope.row.edit"
                placement="top"
              >
                <i class="el-icon-edit tb-icon" @click="editRow(scope.$index)"></i>
              </el-tooltip>

              <el-tooltip
                effect="dark"
                content="保存"
                v-if="scope.row.edit"
                placement="top"
              >
                <i
                  class="el-icon-circle-check tb-icon"
                  @click="checkRow(scope.$index)"
                ></i>
              </el-tooltip>

              <el-tooltip effect="dark" content="重置" placement="top">
                <i
                  class="el-icon-refresh-left tb-icon"
                  @click="resetRow(scope.$index)"
                ></i>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <el-col
          :span="24"
          style="text-align: right; padding: 30px 0 10px 0"
          v-if="status === '2' && ppowers.some((i) => i == '2')"
        >
          <el-button type="primary" :size="'mini'" @click="submitForm">保 存 </el-button>
        </el-col>
      </el-form>
    </el-col>
  </el-row>
</template>
<script>
import asyncRequest from "@/apis/service/sellOut/returnOrder";
import resToken from "@/mixins/resToken";
import config from "./columns"; //表格列参数
export default {
  name: "returnCheck",
  props: ["id", "newTime", "sitem"],

  mixins: [resToken],
  computed: {
    ppowers() {
      const tran =
        this.$store.getters.roleProcess.find((i) => i.process_type === "CKTHD") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      thNo: "",
      options: [
        // {
        //   value: "0",
        //   label: "待审核",
        // },
        {
          value: "1",
          label: "同意",
        },
        {
          value: "2",
          label: "拒绝",
        },
      ],
      status: "",
      loading: false,
      tableForm: {
        product_go: [],
      },
      tableFormThis: config.tableFormThis,
      voList: [],
    };
  },
  watch: {
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },

  mounted() {
    this.initForm();
  },
  methods: {
    async initForm() {
      this.loading = true;
      await this.resetForm();
      this.loading = false;
    },
    async resetForm() {
      await this.$nextTick(() => {
        if (this.$refs.tableForm) {
          this.voList = [];
          this.tableForm = {
            product_go: [],
          };
          this.$refs.tableForm.resetFields();
          this.$refs.tableForm.clearValidate();
          let { child, thNo, status } = this.sitem;
          this.status = status;
          this.thNo = thNo;

          child.map((ele) => {
            let obj = {
              id: ele.id,
              anomalous_numbers: ele.error_num,
              anomalous_reason: ele.error_code,
              anomalous_reason_code: ele.error_code, //异常原因code
              anomalous_remark: ele.error_remark,
              check_value: ele.status === "0" ? "1" : ele.status,
              check_remark: ele.exam_remark,
              is_del: "0",
            };
            this.voList.push(obj);
          });
          let list = JSON.parse(JSON.stringify(this.voList));
          list.forEach((v) => {
            v.edit = false;
            this.tableForm.product_go.push(v);
          });
        }
      });
    },

    async submitForm() {
      if (!this.loading) {
        this.loading = true;

        let index = -1;
        this.tableForm.product_go.forEach((v, i) => {
          if (v.edit && index === -1) {
            index = i;
          }
        });
        if (index !== -1) {
          this.$message.warning("当前已有商品正在编辑！");
          this.loading = false;
          return;
        }
        let arr = [];
        let model = JSON.parse(JSON.stringify(this.tableForm));
        model.product_go.map((ele) => {
          let obj = {
            id: ele.id,
            remark: ele.anomalous_remark,
            status: ele.check_value,
          };
          arr.push(obj);
        });
        let _model = {
          thNo: this.thNo,
          errorlist: arr,
          status: "3", //1待验货2待验货审核3待业务审核4完成 this.status + 1
        };
        const res = await asyncRequest.check(_model);
        this.loading = false;
        if (res && res.code === 0) {
          this.$notify.success({
            title: res.message,
            message: "",
          });
          this.$emit("refresh");
        } else if (res && res.code >= 100 && res.code <= 104) {
          await this.logout();
        } else {
          this.$message.warning(res.message);
        }
      }
    },
    //异常原因筛选
    //保存某一行
    checkRow(rowIndex) {
      const { check_value, check_remark } = this.tableForm.product_go[rowIndex];
      if (check_value !== "1" && check_value !== "2") {
        this.$message.warning("审核情况不能为空!");
        return;
      }
      if (check_value === "2" && check_remark === "") {
        this.$message.warning("审核备注不能为空!");
        return;
      }
      if (check_value === "1") {
        this.tableForm.product_go[rowIndex].edit = false;
      }
      if (check_value === "2" && check_remark !== "") {
        this.tableForm.product_go[rowIndex].edit = false;
      }
    },
    //编辑某一行
    editRow(rowIndex) {
      let index = this.tableForm.product_go.findIndex((v) => v.edit);
      if (index !== -1) {
        this.$message.warning("请完成其他行的编辑！");
        return;
      } else {
        this.tableForm.product_go[rowIndex].edit = true;
      }
    },
    resetRow(rowIndex) {
      this.tableForm.product_go[rowIndex].check_remark = "";
      this.tableForm.product_go[rowIndex].check_value = "1";
    },
  },
};
</script>
<style lang="scss" scoped></style>
