<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="12vh"
    width="1024px"
    :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="closeModel"
  >
    <el-card class="add" style="margin-top: -25px">
      <el-row>
        <el-col :span="18">
          <el-form
            ref="ruleForm"
            size="small"
            :model="ruleForm"
            status-icon
            :rules="rule"
            label-width="100px"
          >
            <el-row>
              <el-col :span="8">
                <el-form-item size="small" label="审核状态:" prop="status">
                  <el-radio-group
                    v-model="ruleForm.status"
                    @change="radioChange"
                  >
                    <el-radio :label="'2'">通过</el-radio>
                    <el-radio :label="'3'">驳回</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="16">
                <el-form-item size="small" label="审核备注:" prop="remark">
                  <el-input
                    v-model="ruleForm.remark"
                    placeholder="请输入审核备注"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-button
            type="primary"
            class="fl"
            size="small"
            style="margin: 0 0 0 10px"
            @click="batchSet"
            >批量审核</el-button
          >
          <el-button type="primary" class="fr" size="small" @click="saveSet"
            >保存审核结果</el-button
          >
        </el-col>
      </el-row>
      <el-table
        v-if="list && list.length > 0"
        :data="list"
        stripe
        border
        v-on="$listeners"
        row-key="cgdNO"
        size="mini"
        max-height="450px"
        style="width: 100%"
        ref="multipleTable"
        tooltip-effect="dark"
        @selection-change="handleSelectionChange"
        ><el-table-column
          type="selection"
          width="50"
          fixed="left"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="cgdNO"
          show-overflow-tooltip
          label="采购单编号"
          width="150"
        />
        <el-table-column
          prop="name"
          show-overflow-tooltip
          label="供货商"
          width="180"
        />
        <el-table-column
          prop="contector"
          show-overflow-tooltip
          label="联系人"
          width="70"
        />
        <!-- <el-table-column
          prop="purchasePrice"
          show-overflow-tooltip
          label="货款"
          width="110"
        /> -->
        <el-table-column label="开票信息">
          <el-table-column
            prop="companyNo"
            show-overflow-tooltip
            label="编码"
            width="170"
          />
          <el-table-column
            prop="company_name"
            show-overflow-tooltip
            label="公司名称"
            width="170"
          />
        </el-table-column>
        <el-table-column label="审核结果">
          <el-table-column label="状态" width="100"
            ><template slot-scope="scope">
              <el-tag
                size="mini"
                v-if="scope.row.status !== ''"
                :type="scope.row.status === '2' ? 'success' : 'danger'"
              >
                {{ scope.row.status === "2" ? "通过" : "驳回" }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="remark"
            show-overflow-tooltip
            label="备注"
            width="170"
          />
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="50">
          <template slot-scope="scope">
            <el-tooltip
               
              effect="dark"
              content="删除"
              placement="top"
            >
              <i
                class="el-icon-delete tb-icon"
                @click="deleteRow(scope.$index, list)"
              ></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/purchase/porder";
import resToken from "@/mixins/resToken";
export default {
  name: "supplierPay",
  props: ["showModel", "tableData"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      title: "批量设置开票信息",
      selectLoading: false,
      activeOptions: [],
      code: [],
      list: [],
      ruleForm: {
        status: "",
        remark: "",
      },
      rule: {},
      rules: {
        status: [
          {
            required: true,
            message: "请选择审核状态",
            trigger: "change",
          },
        ],
        remark: [
          {
            required: false,
            message: "请输入审核备注",
            trigger: "blur",
          },
        ],
      },
      multipleSelection: [],
      showModelThis: this.showModel,
    };
  },
  watch: {
    showModel: function (val) {
      if (val) {
        this.initForm();
      }
      this.showModelThis = val;
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
    },
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    radioChange() {
      console.log(this.ruleForm.status);
      if (this.ruleForm.status === "3") {
        this.rules.remark[0].required = true;
      } else {
        this.rules.remark[0].required = false;
      }
    },
    async initForm() {
      this.loading = true;
      this.multipleSelection = [];
      this.rule = this.rules;
      await this.resetForm();
      this.$nextTick(() => {
        this.list = [];
        // setTimeout(() => {
        console.log(this.tableData);
        if (this.tableData && this.tableData.length > 0) {
          this.list = JSON.parse(JSON.stringify(this.tableData));
          this.list.map((v1) => {
            v1.companyNo = v1.inv_company;
            v1.company_name = v1.inv_company_name;
            v1.status = "";
            v1.remark = "";
            return v1;
          });
          console.log(this.list);
        }
        // }, 10000);
      });
      console.log(this.list);
      this.loading = false;
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            status: "2",
            remark: "",
          };
        }
      });
    },
    async batchSet() {
      await this.$refs.ruleForm.validate(async (valid) => {
        console.log(valid);
        if (valid) {
          if (this.multipleSelection && this.multipleSelection.length === 0) {
            this.$message.warning("请选择采购单！");
            return;
          } else {
            this.loading = true;
            this.multipleSelection.forEach((v1) => {
              let index = this.list.findIndex(
                (v2) => v2.cgdNO === v1.cgdNO
              );

              if (index !== -1) {
                this.list[index].status = this.ruleForm.status;
                this.list[index].remark =
                  this.ruleForm.status === "3" ? this.ruleForm.remark : "";
                this.$set(this.list, index, this.list[index]);
              }
            });
            this.loading = false;
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async saveSet() {
      let isok = true;
      let model = {
        cgdinv: [],
      };
      this.list.forEach((v1) => {
        let items = {
          invid: v1.sequenceNo,
          status: v1.status,
          remark: v1.remark,
        };
        if (v1.status === "") {
          isok = false;
        }
        model.cgdinv.push(items);
      });
      console.log(isok);
      if (!isok) {
        this.$message.warning("部分采购单未设置审核信息！");
        return;
      } else {
        this.loading = true;
        const res = await asyncRequest.exam(model);
        if (res && res.code === 0 && res.data) {
          const title = "审核提交成功！";
          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);
        }
        this.loading = false;
      }
    },
  },
};
</script>

   <style lang="scss" scoped>
.supplierPay {
  .add {
    .survey-title {
      line-height: 35px;
      font-size: 18px;
      font-weight: bold;
    }
    .el-row {
      margin-bottom: 20px;
      &:last-child {
        margin-bottom: 0;
      }
    }
    .lh40 {
      line-height: 40px;
      font-size: 16px;
    }
    .el-col {
      border-radius: 4px;
    }
    .bg-purple-dark {
      background: #99a9bf;
    }
    .bg-purple {
      background: #d3dce6;
      padding: 30px;
    }
    .bg-purple-light {
      background: #e5e9f2;
    }
    .grid-content {
      border-radius: 4px;
      min-height: 36px;
    }
    .row-bg {
      padding: 10px 0;
      background-color: #f9fafc;
    }
  }
  .setWidth {
    width: 100% !important;
    // display: flex;
    // display: block !important;
    // .el-input__inner {
    //   display: flex;
    //   width: 1000px !important;
    //   display: block !important;
    // }
  }
}
</style>
