<template>
  <div class="testdiv">
    <div class="divborder newSearchDiv">
      <!-- 搜索条件--程序号 -->
      <div class="filter">
        <label>主程序号：</label>
        <Input class="searchInput" v-model="programNo" />
      </div>
      <!-- 搜索条件--产品名称 -->
      <div class="filter">
        <label>产品名称：</label>
        <Select v-model="productId" clearable filterable class="searchSelect">
          <Option v-for="v in productList" :value="v.id" :key="v.id"
            >{{ v.name }}({{ v.code }})
          </Option>
        </Select>
      </div>
      <!-- 搜索条件--审核状态 -->
      <div class="filter">
        <label>审核状态：</label>
        <Select v-model="auditStatus" clearable class="searchSelect">
          <Option :value="1">待审核</Option>
          <Option :value="2">已审核</Option>
          <Option :value="3">被拒绝</Option>
        </Select>
      </div>
      <!-- 按钮组 -->
      <div class="searchBtn">
        <!-- 查询 -->
        <Button
          type="primary"
          class="twoWord"
          icon="ios-search"
          @click="search(true)"
        >
          查询
        </Button>
        <!-- 审核 -->
        <Button
          v-if="btnShow"
          type="primary"
          class="twoWord"
          icon="md-add-circle"
          @click="audit()"
        >
          审核
        </Button>
        <!-- 下载 -->
        <Button
          type="primary"
          class="twoWord"
          icon="md-download"
          @click="download()"
        >
          下载
        </Button>
      </div>
    </div>
    <div class="divborder newTableDiv">
      <!-- 表格 -->
      <Table
        highlight-row
        class="tableClass"
        :data="tableData"
        :columns="tableColumns"
        @on-current-change="handleRowChange"
      >
      </Table>
      <!-- 分页器 -->
      <div class="pageDiv">
        <div class="pageDirection">
          <Page
            :total="total"
            :current.sync="page"
            :page-size.sync="pagesize"
            show-elevator
            show-sizer
            show-total
            @on-change="handlePage"
            @on-page-size-change="handlePageSize"
            :page-size-opts="[10, 20, 30, 40, 100]"
          >
          </Page>
        </div>
      </div>
    </div>
    <!-- 程序预览弹框 -->
    <Modal
      class="newModalClass echo_wrap"
      v-model="previewModal"
      :width="900"
      footer-hide
    >
      <p slot="header">
        <span> 程序预览 </span>
      </p>
      <div class="modal_body" v-if="refresh">
        <Tabs value="0" type="card">
          <TabPane
            v-for="(v, i) in previewList"
            :label="
              v.program_type == 1
                ? `主程序（${v.program_code}）`
                : `子程序（${v.program_code}）`
            "
            :name="`${i}`"
            :key="i"
          >
            <textarea class="echo_textarea" :value="v.file_content"></textarea>
          </TabPane>
        </Tabs>
      </div>
    </Modal>
    <!-- 审核弹框 -->
    <Modal class="newModalClass echo_wrap" v-model="auditModal" :width="500">
      <p slot="header">
        <span>审核</span>
      </p>
      <div class="modal_body">
        <div class="echo_row">
          <p class="echo_tip" v-if="checkStatus == 1">
            请确认程序内容是否符合通过条件，审核通过后，程序可以下载至NC使用。
          </p>
          <p class="echo_tip" v-else>
            该程序号存在已审核记录，审核通过后老的记录将被覆盖，请确认是否审核通过。
          </p>
        </div>
        <div class="echo_row">
          <span class="echo_label">备注：</span>
          <Input v-model="auditRemark" />
        </div>
      </div>
      <div slot="footer">
        <Button size="large" @click="confirmAudit(2)">不通过</Button>
        <Button size="large" type="primary" @click="confirmAudit(1)">
          通过
        </Button>
      </div>
    </Modal>
  </div>
</template>

<script>
export default {
  data() {
    return {
      btnShow: false,
      // 搜索条件--程序号
      programNo: "",
      // 搜索条件--产品名称
      productId: "",
      // 产品下拉列表
      productList: [],
      // 审核人员下拉列表
      userList: [],
      // 搜索条件--审核状态
      auditStatus: "",
      // 页码，页距，总计
      page: 1,
      pagesize: 10,
      total: 0,
      // 表格数据
      tableData: [],
      // 当前选中行
      row: {},
      previewList: [],
      // 表格列字段
      tableColumns: [
        {
          type: "sort",
          title: "序号",
          render: (h, params) => {
            return h(
              "span",
              params.index + (this.page - 1) * this.pagesize + 1
            );
          },
          width: 75,
          align: "center",
        },
        {
          title: "操作",
          width: 80,
          align: "center",
          render: (h, params) => {
            const _this = this;
            return h(
              "div",
              {
                style: {
                  textAlign: "center",
                  color: "#2D8CF0",
                  cursor: "pointer",
                },
                on: {
                  click: () => {
                    let row = params.row;
                    if (!row.program_version) {
                      _this.$Message.error("程序版本号不能为空！");
                      return false;
                    }
                    _this.axios
                      .request({
                        url: "/dnc/DncPgCreate/GetAllContent",
                        params: {
                          programId: row.id,
                        },
                        method: "get",
                      })
                      .then((res) => {
                        let result = res.data;
                        if (result.ret == 1) {
                          _this.previewList = [];
                          let data = result.data;
                          if (data && Array.isArray(data) && data.length) {
                            _this.previewList = data;
                          }
                          _this.previewModal = true;
                          _this.refresh = false;
                          _this.$nextTick(() => {
                            _this.refresh = true;
                          });
                        } else {
                          _this.$Message.error(_this.$t(result.msg));
                        }
                      });
                  },
                },
              },
              "预览"
            );
          },
        },
        {
          title: "主程序号",
          key: "program_code",
        },
        {
          title: "产品名称",
          key: "productname",
        },
        {
          title: "产品图纸号",
          key: "code",
        },
        {
          title: "工序名称",
          key: "process_name",
        },
        {
          title: "主程序文件名",
          key: "file_name",
          width: 120,
          align: "center",
        },
        {
          title: "创建人",
          key: "createname",
          width: 85,
          align: "center",
        },
        {
          title: "创建时间",
          key: "createtime",
          width: 150,
          align: "center",
        },
        {
          title: "创建备注",
          key: "create_remark",
          width: 90,
          align: "center",
        },
        {
          title: "审核人",
          key: "checkname",
          width: 85,
          align: "center",
        },
        {
          title: "审核时间",
          key: "checktime",
          width: 150,
          align: "center",
        },
        {
          title: "审核结果",
          key: "checkinfo",
          width: 90,
          align: "center",
          render: (h, params) => {
            let status = params.row.checkinfo;
            let text;
            switch (status) {
              case 0:
                text = "未审核";
                break;
              case 1:
                text = "通过";
                break;
              case 2:
                text = "不通过";
                break;
            }
            return h(
              "div",
              {
                style: {
                  textAlign: "center",
                },
              },
              text
            );
          },
        },
        {
          title: "审核备注",
          key: "check_remark",
          width: 90,
          align: "center",
        },
        {
          title: "子程序个数",
          key: "program_number",
          align: "center",
        },
        {
          title: "版本号",
          key: "program_version",
        },
        {
          title: "状态",
          align: "center",
          width: 90,
          render: (h, params) => {
            let bg, text;
            let status = params.row.checkstatus;
            switch (status) {
              case 1:
                text = "待审核";
                bg = "#FFFF00";
                break;
              case 2:
                text = "已审核";
                bg = "#00FF00";
                break;
              case 3:
                text = "被拒绝";
                bg = "#FF0000";
                break;
            }
            return h("div", {
              domProps: {
                innerHTML: text,
              },
              style: {
                backgroundColor: bg,
                width: "100%",
                height: "36px",
                lineHeight: "36px",
                textAlign: "center",
              },
            });
          },
        },
      ],
      // 预览弹框是否弹出
      previewModal: false,
      // 审核弹框是否弹出
      auditModal: false,
      // 审核备注
      auditRemark: "",
      // 当前行审核状态
      checkStatus: 1,
      refresh: true,
    };
  },
  mounted() {
    // 格式化表格样式
    this.tableColumns = this.$time.initTableTitle(this.tableColumns);
    // 获取产品下拉列表
    this.getProductList();
    // 获取审核人员下拉列表
    this.getUserList();
    // 初始化查询
    this.search();
    // 获取按钮组权限
    this.getBtnPermission();
  },
  methods: {
    // 获取按钮组权限
    getBtnPermission() {
      const _this = this;
      _this.axios
        .request({
          url: "/dnc/DncPgCreate/GetCheckbuttons",
          method: "get",
        })
        .then((res) => {
          let result = res.data;
          if (result.ret == 1) {
            let permission = result.data;
            if (permission.checkpower == 1 || permission.allpower == 1) {
              _this.btnShow = true;
            }
          }
        });
    },
    // 获取产品下拉列表
    getProductList() {
      const _this = this;
      _this.axios
        .request({
          url: "/config/ProjectProduct/GetProductsByCode?code=&name=&count=50",
          method: "get",
        })
        .then((res) => {
          let result = res.data;
          if (result.ret == 1) {
            let data = result.data;
            _this.productList = [];
            if (data && Array.isArray(data) && data.length) {
              _this.productList = data;
            }
          } else {
            _this.$Message.error(_this.$t(result.msg));
          }
        });
    },
    // 查询
    search(isInit) {
      const _this = this;
      if (isInit) {
        _this.page = 1;
        _this.pagesize = 10;
      }
      let { programNo, productId, auditStatus, page, pagesize } = this;
      let params = {
        productId: productId,
        programCode: programNo,
        checkStatus: auditStatus ? auditStatus : 0,
        page,
        pagesize,
      };
      _this.axios
        .request({
          url: "/dnc/DncPgCreate/GetProgramsLists",
          params,
          method: "get",
        })
        .then((res) => {
          let result = res.data;
          if (result.ret == 1) {
            _this.total = result.total;
            _this.tableData = [];
            let data = result.data;
            if (data && Array.isArray(data) && data.length) {
              _this.tableData = data;
            }
          } else {
            _this.$Message.error(_this.$t(result.msg));
          }
        });
    },
    // 监听表格当前选中行
    handleRowChange(cur) {
      this.row = cur;
    },
    // 监听页码变化
    handlePage(v) {
      this.page = v;
      this.search();
    },
    // 监听页距变化
    handlePageSize(v) {
      this.page = 1;
      this.pagesize = v;
      this.search();
    },
    // 获取审核人员下拉列表
    getUserList() {
      const _this = this;
      _this.axios
        .request({
          url: "/dnc/DncPgCreate/GetCheckUsers",
          method: "get",
        })
        .then((res) => {
          let result = res.data;
          if (result.ret == 1) {
            let data = result.data;
            _this.userList = [];
            if (data && Array.isArray(data) && data.length) {
              _this.userList = data;
            }
          } else {
            _this.$Message.error(_this.$t(result.msg));
          }
        });
    },
    // 点击下载
    download() {
      const _this = this;
      let { row, $t } = this;
      if (JSON.stringify(row) == "{}") {
        _this.$Message.error($t(1043));
        return false;
      }
      if (!row.file_url) {
        _this.$Message.error($t(9000147));
        return false;
      }
      _this.axios
        .request({
          url: `/dnc/DncPgCreate/DownloadZipById?programId=${row.id}`,
          data: {
            programId: row.id,
          },
          method: "post",
        })
        .then((res) => {
          let result = res.data;
          if (result.ret == 1) {
            window.location.href = _this.axios.publicPath + result.data;
          } else {
            _this.$Message.error(_this.$t(result.msg));
          }
        });
    },
    // 点击审核
    audit() {
      const _this = this;
      let { row, $t } = this;
      if (JSON.stringify(row) == "{}") {
        _this.$Message.error($t(1043));
        return false;
      }
      if (row.checkstatus == 1) {
        let checkStatus = row.checkstatus;
        _this.checkStatus = checkStatus;
        _this.auditModal = true;
        _this.auditRemark = "";
      } else {
        _this.$Message.error("不允许重复进行审核！");
      }
    },
    // 确认审核
    confirmAudit(isAgree) {
      const _this = this;
      let { row, auditRemark, $t } = this;

      _this.axios
        .request({
          url: "/dnc/DncPgCreate/CheckprogramCodeMain",
          params: {
            programcode: row.program_code,
            programId: row.id,
          },
          method: "get",
        })
        .then((res) => {
          let result = res.data;
          if (result.ret == 1) {
            // 没有重复主程序号，直接审核
            _this.axios
              .request({
                url: "/dnc/DncPgCreate/CheckMainProgram",
                data: {
                  programids: row.id,
                  check_remark: auditRemark,
                  num: isAgree,
                },
                method: "post",
              })
              .then((res) => {
                let result = res.data;
                if (result.ret == 1) {
                  _this.$Message.success(_this.$t(result.msg));
                  _this.auditRemark = "";
                  _this.auditModal = false;
                  _this.search();
                } else {
                  _this.$Message.error(_this.$t(result.msg));
                }
              });
          } else {
            // 有重复主程序号，弹框提示是否覆盖审核记录
            _this.$Modal.confirm({
              title: $t(result.msg),
              content: "",
              width: 500,
              closable: true,
              okText: $t(1011),
              onOk: () => {
                _this.axios
                  .request({
                    url: "/dnc/DncPgCreate/CheckMainProgram",
                    data: {
                      programids: row.id,
                      check_remark: auditRemark,
                      num: isAgree,
                    },
                    method: "post",
                  })
                  .then((res) => {
                    let result = res.data;
                    if (result.ret == 1) {
                      _this.$Message.success(_this.$t(result.msg));
                      _this.auditRemark = "";
                      _this.auditModal = false;
                      _this.search();
                    } else {
                      _this.$Message.error(_this.$t(result.msg));
                    }
                  });
              },
            });
          }
        });
    },
  },
};
</script>

<style lang="less" scoped>
.echo_label {
  font-size: 14px;
  margin-bottom: 5px;
  display: inline-block;
}
.echo_tip {
  font-size: 14px;
  margin-bottom: 12px;
}
.echo_textarea::-webkit-scrollbar {
  width: auto;
}
.echo_textarea {
  font-size: 14px;
  white-space: nowrap;
  overflow: auto;
  resize: none;
  width: 100%;
  height: 444px;
}
.demo-spin-icon-load {
  animation: ani-demo-spin 1s linear infinite;
}
.ivu-input-word-count {
  text-align: center;
  position: absolute;
  right: 7px;
  bottom: 2px;
  padding-left: 7px;
  background: #fff;
  z-index: 1;
  display: inline-flex;
  color: #808695;
  font-size: 12px;
}
</style>
